As i found many students face problems regarding Data Structure programs though they are well known about the respective algorithms,
here are some simple Data Structure programs totally based on algorithms........
*Program of stack:
#include"stdio.h"
#include"conio.h"
#define max 5
int stack[max],top=-1;
void push(int );
void pop();
void display();
int overflow();
int underflow();
void main()
{
int n,op;
char ch;
do
{
clrscr();
printf("\n 1-push 2-pop 3-display 4-exit\n");
printf("\n enter ur option\n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
do
{
clrscr();
printf("\n enter element to insert\n");
scanf("%d",&n);
push(n);
printf("\n would u like to push another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 2 :
{
do
{
clrscr();
pop();
printf("\n would u like to pop another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 3 :
{
clrscr();
display();
break;
}
case 4 :
exit(0);
default :
{
printf("invalid option");
}
};
printf("\n would u like to continue (y/n)\n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
int overflow()
{
if(top==max-1)
return 1;
else
return 0;
}
int underflow()
{
if(top==-1)
return 1;
else
return 0;
}
void push(int n)
{
if(overflow())
printf("\nstack is full\n");
else
{
top++;
stack[top]=n;
printf("\n%d element is pushed in to stack\n",n);
}
}
void pop()
{
int m;
if(underflow())
printf("\nstack is empty\n");
else
{
m=stack[top];
top--;
printf("\n%d element is deleted\n",m);
}
}
void display()
{
int i;
if(underflow())
printf("\nstack is empty\n");
else
{
for(i=top;i>=0;i--)
{
printf("\n %d\t",stack[i]);
}
}
}
*Program of queue:
#include"stdio.h"
#include"conio.h"
#define max 5
int queue[max],f=-1,r=-1;
void insert(int );
void delete();
void display();
int overflow();
int underflow();
void main()
{
int n,op;
char ch;
do
{
clrscr();
printf("\n 1-insert 2-delete 3-display 4-exit\n");
printf("\n enter ur option\n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
do
{
clrscr();
printf("\n enter element to insert\n");
scanf("%d",&n);
insert(n);
printf("\n would u like to insert another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 2 :
{
do
{
clrscr();
delete();
printf("\n would u like to delete another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 3 :
{
clrscr();
display();
break;
}
case 4 :
exit(0);
default :
{
printf("invalid option");
}
};
printf("\n would u like to continue (y/n)\n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
int overflow()
{
if(r>=max-1)
return 1;
else
return 0;
}
int underflow()
{
if(f==-1)
return 1;
else
return 0;
}
void insert(int n)
{
if(overflow())
printf("\n queue is full\n");
else
{
r++;
queue[r]=n;
if(f==-1)
f=0;
printf("\n%d element is inserted in to queue\n",n);
}
}
void delete()
{
int m;
if(underflow())
printf("\nqueue is empty\n");
else
{
m=queue[f];
if(f==r)
f=r=-1;
else
f++;
printf("\n%d element is deleted\n",m);
}
}
void display()
{
int i;
if(underflow())
printf("\n queue is empty\n");
else
{
for(i=f;i<=r;i++)
{ printf("\n %d\t",queue[i]);
} } }
*Program of circular queue:
#include"stdio.h"
#include"conio.h"
#define max 5
int queue[max],f=-1,r=-1;
void insert(int );
void delete();
void display();
int overflow();
int underflow();
void main()
{
int n,op;
char ch;
clrscr();
printf("\n************Program for implementation of cqueue using array**************\n\n");
do
{
printf("\n 1-insert 2-delete 3-display 4-exit\n");
printf("\n enter ur option\n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
do
{
printf("\n enter element to insert\n");
scanf("%d",&n);
insert(n);
printf("\n would u like to insert another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 2 :
{
do
{
delete();
printf("\n would u like to delete another element (y/n)\n");
fflush(stdin);
ch=getchar();
}
while(ch=='y' ch=='Y');
break;
}
case 3 :
{
display();
break;
}
case 4 :
exit(0);
default :
{
printf("invalid option");
}
};
printf("\n would u like to continue (y/n)\n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
int overflow()
{
if(f==0 && r>=max-1 f==r+1)
return 1;
else
return 0;
}
int underflow()
{
if(f==-1 && r==-1)
return 1;
else
return 0;
}
void insert(int n)
{
if(overflow())
printf("\n cqueue is full\n");
else
{
if(r==max-1)
r=-1;
else
r++;
queue[r]=n;
if(f==-1)
f=0;
printf("\n%d element is inserted in to queue\n",n);
}
}
void delete()
{
int m;
if(underflow())
printf("\n cqueue is empty\n");
else
{
m=queue[f];
if(f==r)
f=r=-1;
else if(f==max-1)
f=-1;
else
f++;
printf("\n%d element is deleted\n",m);
}
}
void display()
{
int i;
if(underflow())
printf("\n cqueue is empty\n");
else
{
for(i=f;i<=r;i++)
{
printf("%d\t",queue[i]);
} } }
*Program for Single linked list:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *link;
};
char ch;
void main()
{
struct node *first,*newnode,*temp,*temp1;
int op;
do
{
clrscr();
printf("\n 1-create list 2-display 3-insert at first 4-insert in middle \n");
printf("\n 5-insert at last 6-delete from first 7-delete from middle 8-delete from last \n");
printf("\n enter ur option \n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
first=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data\n");
fflush(stdin);
scanf("%d \n", &first->info);
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
temp=first;
while(ch=='y' ch=='Y')
{
temp->link=(struct node *)malloc(sizeof(struct node));
temp=temp->link;
printf("\n enter the data \n");
scanf("\n %d",&temp->info);
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
}
temp->link=0;
break;
}
case 2 :
{
temp=first;
while(temp!=0)
{
printf("\n %u->%d->%u \n",temp,temp->info,temp->link);
temp=temp->link;
}
break;
}
case 3 :
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->link=first;
first=newnode;
break;
}
case 4 :
{
int x,f=0;
printf("\n enter da position where the new element to b insert b4 that position\n");
scanf("\n%d",&x);
temp=first;
while(temp->link!=0)
{
if(temp->link->info==x)
{
f=1;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->link=temp->link;
temp->link=newnode;
break;
}
temp=temp->link;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 5:
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
temp=first;
while(temp->link!=0)
temp=temp->link;
temp->link=newnode;
newnode->link=0;
break;
}
case 6 :
{
temp1=first;
first=first->link;
free(temp1);
break;
}
case 7 :
{
int x,f=0;
printf("\n enter the data to b deleted\n");
scanf("\n%d",&x);
temp=first;
while(temp!=0)
{
if(temp->link->info==x)
{
f=1;
temp1=temp->link;
temp->link=temp1->link;
free(temp1);
}
temp=temp->link;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 8 :
{
temp=first;
while(temp->link!=0)
{
temp1=temp;
temp=temp->link;
}
temp1->link=0;
break;
}
}
printf("\n would u like to continue (y/n) \n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
*Program for circular linked list:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *link;
};
char ch;
void main()
{
struct node *head,*first,*newnode,*temp,*temp1;
int op;
do
{
clrscr();
printf("\n 1-create list 2-display 3-insert at first 4-insert in middle \n");
printf("\n 5-insert at last 6-delete from first 7-delete from middle 8-delete from last \n");
printf("\n enter ur option \n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
head=(struct node *)malloc(sizeof(struct node));
first=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data\n");
fflush(stdin);
scanf("%d \n", &first->info);
head->link=first;
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
temp=first;
while(ch=='y' ch=='Y')
{
temp->link=(struct node *)malloc(sizeof(struct node));
temp=temp->link;
printf("\n enter the data \n");
scanf("\n %d",&temp->info);
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
}
temp->link=head;
break;
}
case 2 :
{
printf("\n head is at %u\n",head);
temp=first;
while(temp!=head)
{
printf("\n %u->%d->%u \n",temp,temp->info,temp->link);
temp=temp->link;
}
break;
}
case 3 :
{
head->link=first;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->link=head->link;
head->link=newnode;
first=newnode;
break;
}
case 4 :
{
int x,f=0;
printf("\n enter da position where the new element to b insert b4 that position\n");
scanf("\n%d",&x);
temp=first;
while(temp->link!=head)
{
if(temp->link->info==x)
{
f=1;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->link=temp->link;
temp->link=newnode;
break;
}
temp=temp->link;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 5:
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
temp=first;
while(temp->link!=head)
temp=temp->link;
temp->link=newnode;
newnode->link=head;
break;
}
case 6 :
{
temp1=first;
first=first->link;
free(temp1);
break;
}
case 7 :
{
int x,f=0;
printf("\n enter the data to b deleted\n");
scanf("\n%d",&x);
temp=first;
while(temp!=head)
{
if(temp->link->info==x)
{
f=1;
temp1=temp->link;
temp->link=temp1->link;
free(temp1);
}
temp=temp->link;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 8 :
{
temp=first;
while(temp->link!=head)
{
temp1=temp;
temp=temp->link;
}
temp1->link=head;
break;
}
}
printf("\n would u like to continue (y/n) \n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
*Program for Double linked list:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *prev,*next;
};
char ch;
void main()
{
struct node *first,*newnode,*temp,*temp1;
int op;
do
{
clrscr();
printf("\n 1-create list 2-display 3-insert at first 4-insert in middle \n");
printf("\n 5-insert at last 6-delete from first 7-delete from middle 8-delete from last \n");
printf("\n enter ur option \n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
first=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data\n");
fflush(stdin);
scanf("%d \n", &first->info);
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
temp=first;
while(ch=='y' ch=='Y')
{
temp1=temp;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
printf("\n enter the data \n");
scanf("\n %d",&temp->info);
temp->prev=temp1;
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
}
temp->next=0;
first->prev=0;
break;
}
case 2 :
{
temp=first;
printf("\n nodeadd prev info next \n");
while(temp!=0)
{
printf("\n\t %u->%u->%d->%u \n",temp,temp->prev,temp->info,temp->next);
temp=temp->next;
}
break;
}
case 3 :
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->prev=0;
newnode->next=first;
first->prev=newnode;
first=newnode;
break;
}
case 4 :
{
int x,f=0;
printf("\n enter da position where the new element to b insert b4 that position\n");
scanf("\n%d",&x);
temp=first;
while(temp->next!=0)
{
if(temp->next->info==x)
{
f=1;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
break;
}
temp=temp->next;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 5:
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
temp=first;
while(temp->next!=0)
temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
newnode->next=0;
break;
}
case 6 :
{
temp1=first;
first->next->prev=0;
first=first->next;
free(temp1);
break;
}
case 7 :
{
int x,f=0;
printf("\n enter the data to b deleted\n");
scanf("\n%d",&x);
temp=first;
while(temp->next!=0)
{
if(temp->next->info==x)
{
f=1;
temp1=temp->next;
temp->next=temp1->next;
temp1->next->prev=temp;
free(temp1);
}
temp=temp->next;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 8 :
{
temp=first;
while(temp->next!=0)
{
temp1=temp;
temp=temp->next;
}
temp1->next=0;
break;
}
}
printf("\n would u like to continue (y/n) \n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
*Program of circular double LL:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *prev,*next;
};
char ch;
void main()
{
struct node *head,*first,*newnode,*temp,*temp1;
int op;
do
{
clrscr();
printf("\n 1-create list 2-display 3-insert at first 4-insert in middle \n");
printf("\n 5-insert at last 6-delete from first 7-delete from middle 8-delete from last \n");
printf("\n enter ur option \n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
head=(struct node *)malloc(sizeof(struct node));
first=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data\n");
fflush(stdin);
scanf("%d \n", &first->info);
head->prev=0;
head->next=first;
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
temp=first;
while(ch=='y' ch=='Y')
{
temp1=temp;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
printf("\n enter the data \n");
scanf("\n %d",&temp->info);
temp->prev=temp1;
printf("\n would u like to add another(y/n) \n");
fflush(stdin);
ch=getchar();
}
temp->next=head;
first->prev=head;
break;
}
case 2 :
{
printf("\n head is at %u\n",head);
temp=first;
printf("\n nodeadd->prev->info->next \n");
while(temp!=head)
{
printf("\n %u->%u->%d->%u \n",temp,temp->prev,temp->info,temp->next);
temp=temp->next;
}
break;
}
case 3 :
{
head->next=first;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->prev=head;
newnode->next=first;
first->prev=newnode;
first=newnode;
break;
}
case 4 :
{
int x,f=0;
printf("\n enter da position where the new element to b insert b4 that position\n");
scanf("\n%d",&x);
temp=first;
while(temp->next!=0)
{
if(temp->next->info==x)
{
f=1;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
break;
}
temp=temp->next;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 5:
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data \n");
scanf("\n %d",&newnode->info);
temp=first;
while(temp->next!=0)
temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
newnode->next=0;
break;
}
case 6 :
{
temp1=first;
first->next->prev=0;
first=first->next;
free(temp1);
break;
}
case 7 :
{
int x,f=0;
printf("\n enter the data to b deleted\n");
scanf("\n%d",&x);
temp=first;
while(temp->next!=0)
{
if(temp->next->info==x)
{
f=1;
temp1=temp->next;
temp->next=temp1->next;
temp1->next->prev=temp;
free(temp1);
}
temp=temp->next;
}
if(f==0)
printf("\n node not found\n");
break;
}
case 8 :
{
temp=first;
while(temp->next!=0)
{
temp1=temp;
temp=temp->next;
}
temp1->next=0;
break;
}
}
printf("\n would u like to continue (y/n) \n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
*Program for Binary search tree:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct tree
{
int data;
struct tree *lchild,*rchild;
};
void create(struct tree **,int);
void preorder(struct tree *);
void inorder(struct tree *);
void postorder(struct tree *);
void search(struct tree *,int);
void insert(struct tree *,int);
void main()
{
int n,i,x,ch,a;
struct tree *root;
clrscr();
root=NULL;
printf("\n***********************Binary Search Tree*******************\n");
printf("\n\nEnter number of nodes to insert\n");
scanf("%d",&n);
printf("\nEnter elements to create a Binary Search Tree ",n);
for(i=0;i
temp->lchild=temp->rchild=NULL;
*n=temp;
}
else
{
if(x<(*n)->data)
create(&((*n)->lchild),x);
else
create(&((*n)->rchild),x);
}
}
void preorder(struct tree *n)
{
if(n!=NULL)
{
printf("\t%d",n->data);
preorder(n->lchild);
preorder(n->rchild);
}
}
void inorder(struct tree *n)
{
if(n!=NULL)
{
inorder(n->lchild);
printf("\t%d",n->data);
inorder(n->rchild);
}
}
void postorder(struct tree *n)
{
if(n!=NULL)
{
postorder(n->lchild);
postorder(n->rchild);
printf("\t%d",n->data);
}
}
void search(struct tree *n,int x)
{
int found=0;
struct tree *temp;
temp=n;
while(temp!=0 && !found)
{
if(x==(temp->data))
found=1;
else if(x<(temp->data))
temp=temp->lchild;
else
temp=temp->rchild;
}
if(!found)
printf("\n%d number not found",x);
else
printf("\n%d number found at %u position ",x,temp);
}
void insert(struct tree *n,int x)
{
int found=0;
struct tree *temp,*pred;
temp=n;
while(temp!=0 && !found)
{
pred=temp;
if(x==(temp->data))
found=1;
else if(x<(temp->data))
temp=temp->lchild;
else
temp=temp->rchild;
}
if(!found)
{
temp=(struct tree *)malloc(sizeof(struct tree));
temp->lchild=temp->rchild=0;
temp->data=x;
if(n!=0)
{
if(x<(pred->data))
pred->lchild=temp;
else
pred->rchild=temp;
}
else
n=temp;
}
else
printf("\n%d number already exists ",x);
}
*Program for stack using linked list:
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *link;
};
char ch;
int tp=0;
void main()
{
struct node *first,*newnode,*top,*temp;
int op;
clrscr();
printf("\n ********** Program for implementation of stack using linked list *********");
do
{
printf("\n\n\n 1-push 2-pop 3-display 4-exit \n");
printf("\n enter ur option \n\n");
scanf("%d",&op);
switch(op)
{
case 1 :
{
first=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data\n");
scanf("%d",&first->info);
printf("\n would u like to push another(y/n) \n");
fflush(stdin);
ch=getchar();
top=first;
while(ch=='y' ch=='Y')
{
tp++;
if(tp<=5) { top->link=(struct node *)malloc(sizeof(struct node));
top=top->link;
printf("\n enter the data \n");
scanf("\n %d",&top->info);
}
else
{
printf("stack is full,u cant push");
break;
}
printf("\n would u like to push another(y/n) \n");
fflush(stdin);
ch=getchar();
}
top->link=0;
break;
}
case 2 :
{
if(tp==0)
{
printf("\n stack is empty \n");
break;
}
top=first;
while(top->link!=0)
{
temp=top;
top=top->link;
}
temp->link=0;
tp--;
printf("\n poped element is %d \n",top->info);
break;
}
case 3 :
{
if(tp==0)
{
printf("\n stack is empty \n");
}
else
{
top=first;
while(top!=0)
{
printf(" %d \t",top->info);
top=top->link;
}
}
break;
}
case 4 :
{
exit(0);
break;
}
}
printf("\n\n would u like to continue (y/n) \n\n");
fflush(stdin);
ch=getchar();
}while(ch=='y' ch=='Y');
getch();
}
If U hav any queries regarding C,C++, & DS............mail me at
No comments:
Post a Comment