DATA STRUCTURE PROGRAMS USING C



Data Structure Using C 



/*STACK USING LIST*/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct stack
{
int no;
struct stack *next;
};
struct stack *p,*q,*temp,*head=NULL;
void main()
{
void push();
void pop();
void display();
int ch;//oice;
clrscr();
while(1)
{
printf("\n\t1.push\n\t2.pop\n\t3.display\n\t4.exit\n\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void push()
{
temp=(struct stack*)malloc(sizeof(struct stack));
printf("\nenter no");
scanf("%d",&temp->no);
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
printf("\nelement %d is pushed in to the stack",temp->no);
}
void pop()
{
p=head;
if(head=NULL)
printf("stack is empty");
else
{
head=head->next;
printf("element %d is popped from the stack",p->no);
free(p);
}
}
void display()
{
if(head==NULL)
printf("\nstack empty");
else
{
p=head;
printf("\nthe elements present in the stack:\n");
while(p!=NULL)
{
printf("\n%d",p->no);
p=p->next;
}
}
}
 

/*STACK USING ARRAY*/
#incflude<stdio.h>
#include<conio.h>
int s[20];
int top=-1;
int val;
void main()
{
int a,n;
void push(int,int);
void pop();
void disp();
clrscr();
printf("\tEnter the number of elements in a stack:");
scanf("%d",&n);
do{
printf("\n\t1.PUSH\n\t2.POP\n\t3.DISP\n\t4.EXIT\n");
printf("\tENTER YOUR CHOICE:");
scanf("%d",&a);
switch (a){
case 1:
  push(val,n);
  break;
case 2:
  pop();
  break;
case 3:
  disp();
  break;
}
getch();
}
while (a!=4);
}/*PUSH*/
void push(int val,int n)
{
if(top<n-1)
{
printf("\t enter the value to be pushed:");
scanf("%d",&val);
top=top+1;
s[top]=val;
}
else
{
printf("stack overflow");
}
}/*POP*/
void pop()
{
int val;
if(top>-1)
{
val=s[top];
printf("the poped value is %d",val);
top=top-1;
}
else
{
printf("\n STACK EMPTY");
}
}/*display*/
void disp()
{
int i;
if(top<0)
{
printf("\n stack is empty");
}
else
{
printf(" the elements in stack are :\n");
for(i=0;i<=top;i++)
{
printf("%d\n\n",s[i]);
}
}
getch();
}

/*QUEUE USING ARRAY*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],rear=0,front=0;
void main()
{
int ch;
void ins();
void del();
void disp();
clrscr();
while(1)
{
printf("1.insert 2.delete 3.display 4.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf("invalid choice");
}
}
getch();
}

void ins()
{
int n;
if(rear==SIZE&&front==0)
{
printf("full");
}
else
{
printf("enter no:");
scanf("%d",&n);
q[rear]=n;
rear++;
}
}

void del()
{
int n,i;
if(front==rear)
{
printf("empty");
}
else
{
n=q[front];
front++;
printf("delete item %d",n);
}
}

void disp()
{
int i;
if(front==rear)
printf("empty");
else
{
printf("elements");
for(i=front;i<rear;i++)
printf("%d",q[i]);
}
}

/*LINKED LIST*/
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
}*head,*temp,*prev,*cur;
void display()
{
printf("The elements are:\n");
temp=head;
if(temp==NULL)
{
printf("\n list is empty");
return;
}
while(temp!=NULL)
{
printf("%d",temp->info);
temp=temp->next;
}
}
void create(int x)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nover flow");
return;
}
else
{
temp->info=x;
temp->next=NULL;
}
if(head==NULL)
{
head=temp;
return;
}
else
printf("\nlist already exist");
}
void insert(int x)
{
int pos,opt,count;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nover flow");
return;
}
else
{
temp->info=x;
temp->next=NULL;
}
if(head==NULL)
{
printf("\nshould create list");
return;
}
else
{
printf("\nopertions for insertion");
printf("\n1.Insert in head\n2.Insert in tail\n3.Insert in mid");
printf("\nEnter the option:");
scanf("%d",&opt);
switch(opt)
{
case 1:
            temp->next=head;
            head=temp;
            break;
case 2:
            prev=head;
            cur=head->next;
            while(cur!=NULL)
            {
            prev=cur;
            cur=cur->next;
            }
            prev->next=temp;
            break;
case 3:
            printf("\nEnter the position:");
            scanf("%d",&pos);
            count=1;
            prev=head;
            cur=head->next;
            while(cur!=NULL)
            if(count==pos-1)
            {
            prev->next=temp;
            temp->next=cur;
            return;
            }
            else
            {
            prev=cur;
            cur=cur->next;
            count++;
            }
            break;
            }
            }
            }
void deletion()
{
int pos,count;
if(head==NULL)
{
 printf("\n LIST EMPTY");
 return;
}
printf("\nEnter the position to be deleted:");
scanf("%d",&pos);
count=1;
if(count==pos)
{
head=head->next;
display();
return;
}
prev=head;
cur=head->next;
while(cur!=NULL)
{
count++;
if(count==pos)
{
prev->next=cur->next;
display();
return;
}
prev=cur;
cur=cur->next;
}
}
void main()
{
int ch,choice,data;
clrscr();
head=temp=prev=cur=NULL;
printf("\n\tProgram to implement singly linked list");
printf("\noptions");
do
{
printf("\n1.Create\n2.Insert\n3.Delete\n4.Display");
printf("\n Enter ur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
     printf("\n Enter the first element:");
     scanf("%d",&data);
     create(data);
     display();
     break;
case 2:
     printf("\nEnter the element to be inserted:");
     scanf("%d",&data);
     insert(data);
     display();
     break;
case 3:
     deletion();
     break;
case 4:
     display();
     break;
}
printf("\nDo you want to continue?(0/1):");
scanf("%d",&ch);
}
while(ch==1);
getch();
}

/*BINARY TREE*/
#include<stdio.h>
#include<conio.h>
struct treenode
{
int info;
struct treenode* left;
struct treenode* right;
};
typedef treenode TREENODE
TREENODE* find(TREENODE* root,int item)
{
if(root==0)
{
printf("\ntree is empty");
return 0;
}
parent_info=0;
while(root!=0&&root->info!=item)
{
parent_info=root->info;
root=root->left;
}
else
{
parent_info=root->info;
root=root->right;
}
}
if(root!=0)
{
printf("\nthe item %d is present",item);
printf("\nthe parent of the item is%d\n",parent_info);
return root;
}
else
{
printf("/n item %d is not present",item);
return 0;
getch();
}
 

/*QUICK SORT*/
#include<stdio.h>
#include<conio.h>
int a[50];
void qsort(int,int);
int split(int,int);
void main()
{
int i,n;//,a[50];
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
qsort(0,n);
for(i=1;i<=n;i++)
printf("%d",a[i]);
getch();
}
void qsort(int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(lower,upper);
qsort(lower,i-1);
qsort(i+1,upper);
}
}
int split(int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return q;
}

/*HEAP SORT*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[50],m;
void heapsort(int);
void heapify(int);
void adjust(int ,int);
void heapsort(int n)
{
int t,l,i;
heapify(n);
for(l=n;l>1;l--)
{
t=a[l];
a[l]=a[1];
a[1]=t;
adjust(1,l-1);
}
return;
}
void heapify(int n)
{
int i;
for(i=n/2;i>1;i--)
adjust(i,n);
return;
}
void adjust(int i,int n)
{
int item,j,k,s;
j=2*i;
item=a[i];
while(j<=n)
{
if(j<n&&a[j]<a[j+1])
j=j+1;
if(item>=a[j])
break;
a[abs(j/2)]=a[j];
j=2*j;
}
a[abs(j/2)]=item;
for(k=1;k<=m;k++)
return ;
}

void qsort(int m,int n)
{
int i,j,k,t;
if(m<n)
{
i=m;
j=n+1;
k=a[m];
while(1)
{
do{
i=i+1;
}
while(a[i]<k);
do{
j=j-1;
}
while(a[j]>k);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
break;
}
t=a[m];
a[m]=a[j];
a[j]=t;
qsort(m,j-1);
qsort(j+1,n);
}
return;
}
void main()
{
void qsort(int,int);
int i,ch,n;
clrscr();
do{
printf("1.heap\n2.quick\n3.exit");
printf("?");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the no.of elements");
scanf("%d",&n);
printf("\nEnter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
m=n;
heapsort(n);
printf("sorted");
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
break;
case 2:
printf("Enter the no.of elements");
scanf("%d",&n);
printf("\nEnter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(0,n-1);
printf("sorted");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
break;
case 3:
exit(0);
}
}
while(ch<3);
}




 


 






Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

BOOKS DETAILS USING C STRUCTURE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE