Programs of Data Structure
1. WAP to find factorial of a number using factorial.
#include<stdio.h>
#include<conio.h>
void main()
{
long int num;
clrscr();
printf("\nentere the value for factorial");
scanf("%ld",&num);
printf("the factorial of %ld is ---> %ld",num,fact(num));
getch();
}
fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
2. WAP to implement queue linked list.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*front =NULL,*rear=NULL;
void insert(int);
void del();
void display();
void main()
{
int choice, val;
clrscr();
printf("\n Queue implementation by link list using pointer\n");
while(1)
{
printf("*******MENU*******");
printf("\n1.ADD\n2.REMOVE\n3.SHOW\n4.EXIT\n");
printf("\t\tENTER YOUR CHOICE\t\n");
scanf("%d",&choice) ;
switch(choice)
{
case 1:
printf("\nENTER ELEMENT\n");
scanf("\t%d",&val);
insert(val);
break;
case 2: del(); break;
case 3: display(); break ;
case 4: exit(0); break ;
default : printf("\n WRONG CHOICE\n");
}
}
getch();
}
void insert(int num)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=num;
newnode->next=NULL;
if(front==NULL)
{front =rear=newnode;}
else
{
rear->next=newnode;
rear=newnode;
}
printf("\nINSERTION COMPLETE\n");
}
void del()
{
if(front==NULL)
printf("\nQUEUE EMPTY\n");
else
{
struct node *temp=front;
front=front->next;
printf("\nITEM DELETED\t %d\n",temp->data);
free(temp);
}
}
void display()
{
if (front==NULL)
printf("\nQUEUE EMPTY\n");
else
{
struct node *temp=front;
while(temp->next!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}
3. WAP to create a stack.
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
void push() ;
void pop() ;
void display() ;
int top = -1 ;
int item,i;
int s[MAXSIZE];
void main()
{
int choice;
clrscr();
do
{
printf("\n1. PUSH") ;
printf("\n2. POP") ;
printf("\n3. DISPLAY") ;
printf("\n4. EXIT") ;
printf("\nEnter Your Choice") ;
scanf("%d", &choice) ;
switch (choice)
{
case 1: push() ;
break ;
case 2: pop() ;
break ;
case 3: display();
break ;
case 4: exit (0) ;
break ;
default: printf("\nWrong choice");
}
}
while(choice!=4) ;
getch();
}
void push()
{
int item ;
if (top == MAXSIZE-1)
{
printf ("\nThe Stack is Full") ;
}
else
{
printf ("Enter the element to be inserted") ;
scanf ("%d", &item) ;
top = top + 1;
s[top] = item ;
}
}
void pop()
{
if (top == -1)
{ printf("The stack is Empty") ;
return;
}
else
{
printf("deleted element=%d\n",s[top]) ;
top = top - 1 ;
}
}
void display()
{
int i ;
if (top == -1)
{
printf ("The stack is Empty") ;
}
else
{
for (i = top ; i >= 0 ; i --)
{
printf ("\nStack elements are |%d|-> %d",i, s[i]);
}
}
}
4. WAP to create stack using link list.
#include<stdio.h>
#include<conio.h>
struct Node
{
int data; struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice,value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1)
{
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
5. Menu driven program to create a singly linked list.
//menue driven program for singly link list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
}*first=NULL,*rear=NULL;
struct node *header,* item,*prior,*pointer,*current;
void main()
{
int opt,i,val;
item=(struct node*)malloc(sizeof(struct node));
clrscr();
printf("\tSingly Link List");
printf("\n Press 1 to create new link list,5 nodes(by default).");
l:
printf("\n Press 2 to enter at begning of list.");
printf("\n Press 3 to insert node after any node.");
printf("\n Press 4 to insert node before any node.");
printf("\n Press 5 to insert at the end of list.");
printf("\n Press 6 to display list.");
printf("\n Enter your option:");
scanf("%d",&opt);
switch(opt)
{
case 1:
for(i=0;i<5;i++)
{
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter a value: ");
scanf("%d",&val);
item->num=val;
if(first==NULL)
{
first=item;
header->next=first;
header=first;
rear=first;
}
else
{
rear->next=item;
rear=item;
item->next=NULL;
}
}
goto l;
case 2:
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter value to be inserted at begning: ");
scanf("%d",&val);
item->num=val;
if(rear==NULL)
{
first=item;
rear=item;
}
else
{
item->next=first;
first=item;
}
header=first;
goto l;
case 3:
first=header;
pointer=first;
printf("\n Enter element after which you want to enter value: ");
scanf("%d",&val);
while(pointer!=NULL)
{
if(pointer->num==val)
{
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter value to inserted: ");
scanf("%d",&item->num);
item->next=pointer->next;
pointer->next=item;
}
pointer=pointer->next;
}
goto l;
case 4:
first=header;
pointer=first;
printf("\n Enter element befor which you want to enter value: ");
scanf("%d",&val);
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter value to be inserted: ");
scanf("%d",&item->num);
current=first;
while(current!=NULL)
{
prior=current;
current=current->next;
if(current->num==val)
{
item->next=current;
prior->next=item;
}
}
goto l;
case 5:
printf("\n Enter value to be inserted at the end: ");
item=(struct node*)malloc(sizeof(struct node));
scanf("%d",&item->num);
if(rear==NULL)
{
first=item;
rear=item;
}
else
{
item->next=NULL;
rear->next=item;
rear=item;
}
goto l;
case 6:
pointer=first;
printf("\n Elements in list are: ");
while(pointer!=NULL)
{
printf("%d\t",pointer->num);
pointer=pointer->next;
}
printf("\n To continue press 1");
printf("\n To exit press 2");
printf("\n Enter your choice: ");
scanf("%d",&opt);
if(opt==1)
{ goto l; }
else
{ break; }
default:printf("\n incorrect input");
break;
}
getch();
}
6. Menu driven program to create a circular linked list.
//singly circular link list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int num;
struct node* next;
}*front=NULL,*rear=NULL,*ptr,*pre,*item;
void main()
{
int opt,i=0,n,flg=0,val;
clrscr();
printf("\tCircular Singly Link List");
printf("\nPress 1 to create list(insert 5 elements).");
l:
printf("\nPress 2 to insert at the begining.");
printf("\nPress 3 to insert before any node.");
printf("\nPress 4 to insert after any node.");
printf("\nPress 5 to insert at the end.");
printf("\nPress 6 to delete node.");
printf("\nPress 7 to display.");
printf("\nPress 8 to exit.");
printf("\nEnter your option:");
scanf("%d",&opt);
switch(opt)
{
case 1://create
printf("Enter elements:\n");
for(n=0;n<5;n++)
{
item=(struct node*)malloc(sizeof(struct node));
scanf("%d",&item->num);
if(front==NULL)
{
front=item;
rear=front;
}
else
{
item->next=front;
rear->next=item;
rear=item;
}
i++;
}
goto l;
case 2://begining
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter element: ");
scanf("%d",&item->num);
if(front==NULL)
{
front=item;
rear=front;
i=0;
}
else
{
item->next=front;
rear->next=item;
front=item;
i++;
}
goto l;
case 3://befor
ptr=front;
item=(struct node*)malloc(sizeof(struct node));
printf("\n Enter element befor which u want to insert new element: ");
scanf("%d",&val);
if(ptr->num==val)
{
printf("\nEnter new element: ");
scanf("%d",&item->num);
item->next=front;
rear->next=item;
front=item;
i++;
flg=1;
}
else
{
for(n=0;n<i;n++)
{
pre=ptr;ptr=ptr->next;
if(ptr->num==val)
{
printf("\nEnter new element: ");
scanf("%d",&item->num);
pre->next=item;
item->next=ptr;
i++;
flg=1;
break;
}
}
}
if(flg==0)
printf("\nElement not found!");
goto l;
case 4://after
ptr=front;
flg=0;
item=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element after which u want to enter new node: ");
scanf("%d",&val);
while(ptr->next!=front)
{
if(ptr->num==val)
{
printf("\nEnter mew element: ");
scanf("%d",&item->num);
item->next=ptr->next;
ptr->next=item;
i++;
flg=1;
break;
}
ptr=ptr->next;
}
if((ptr==rear)&&(ptr->num==val))
{
printf("\nEnter new element: ");
scanf("%d",&item->num);
rear->next=item;
item->next=front;
rear=item;
i++;
flg=1;
}
if(flg==0)
printf("\nelement not found");
goto l;
case 5://end
item=(struct node*)malloc(sizeof(struct node));
printf("\nEnter element to be inserted at the end: ");
scanf("%d",&item->num);
if(rear==NULL)
{
front=item;
rear=item;
i=0;
}
else
{
rear->next=item;
item->next=front;
rear=item;
i++;
}
goto l;
case 6://delete
ptr=front;
flg=0;
printf("\nEnter element to delete form the list: ");
scanf("%d",&val);
for(n=0;n<i;n++)
{
pre=ptr;
ptr=ptr->next;
if((pre==front)&&(pre->num==val))
{
front=front->next;
i--;
flg=1;
break;
}
else if((ptr==rear)&&(ptr->num==val))
{
rear=pre;
i--;
flg=1;
break;
}
else
{
if(ptr->num==val)
{
pre->next=ptr->next;
i--;
flg=1;
break;
}
}
}
if(flg==0)
{
printf("\nElement not found!");
}
goto l;
case 7://display
ptr=front;
printf("\nNo. of element in list: %d",i);
printf("\nElement in list are: ");
for(n=0;n<i;n++)
{
printf("%d\t",ptr->num);
ptr=ptr->next;
}
goto l;
case 8: exit(0);
default:printf("\nWrong input!");
break;
}
getch();
}
7. Menu driven program for searching and sorting.
//menu driven program of sorting and searching
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
void linears();
void binarys();
void insertsort();
void selectsort();
void bubblesort();
void main()
{
int c;
clrscr();
do
{
printf("\n1. Linear Search");
printf("\n2. Binary Search");
printf("\n3. Insertion Sort");
printf("\n4. Selection Sort");
printf("\n5. Bubble Sort");
printf("\n6. Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1: linears(); break;
case 2: binarys(); break;
case 3: insertsort(); break;
case 4: selectsort(); break;
case 5: bubblesort(); break;
case 6: exit(0); break;
default: printf("\nWrong choice!");
}
}while(c!=6);
getch();
}
void binarys()
{
int a[10],i,n,m,c=0,l,u,mid,temp,j;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted array is: ");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
printf("\nEnter the no. to be search: ");
scanf("%d",&m);
i=0;
u=n-1;
while((i<=u)&&(m!=a[mid]))
{
mid=(i+u)/2;
if(m==a[mid])
{ c=1; break; }
else if(m<=a[mid])
{ u=mid-1; }
else
{ i=mid+1; }
}
if(c==0)
printf("The no. is not found.");
else
printf("The no. is found %d.",m);
getch();
return;
}
void linears()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter element to serach: ");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
break;
}
if(i<n)
printf("Element found at index %d->%d",i,x);
else
printf("Element not found!");
getch();
return;
}
void bubblesort()
{
int a[5],size,i,j,temp;
clrscr();
printf("\nEnter the size of array: ");
scanf("%d",&size);
printf("\nEnter the element at the array:\n");
for(i=0;i<size;i++)
{ scanf("%d",&a[i]); }
printf("\nActual array is: ");
for(i=0;i<size;i++)
{ printf("%d\t",a[i]); }
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted array is: ");
for(i=0;i<size;i++)
{ printf("%d\t",a[i]); }
getch();
return;
}
void insertsort()
{
int i,j,n,temp,a[30];
clrscr();
printf("Enter the no. of elements: ");
scanf("%d",&n);
printf("\nEnter the element:\n");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nSorted list is as follows:\n");
for(i=0;i<n;i++)
{ printf("%d\t",a[i]); }
getch();
return;
}
void selectsort()
{
int i,j,n,loc,temp,min,a[30];
clrscr();
printf("Enter the no. of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
min=a[0];
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
if(loc!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
printf("\nSorted list is as follows:\n");
for(i=0;i<n;i++)
{ printf("%d\t",a[i]); }
getch();
return;
}
8. Menu drive program for tree traversal.
Note: " Few things are commented in the program remove and add comments according to the need "
//Tree program with all types of traversal.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *insert(struct tree *,int);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
/*struct tree *delet(struct tree *,int);
struct tree *search(struct tree *);*/
int main(void)
{
struct tree *root;
int choice,item,item_no;
root=NULL;
clrscr();
/*rear=NULL;*/
do
{
do
{
printf("\n\t1. Insert in Binary Tree");
//printf("\n\t2. Delete from Binary Tree");
printf("\n\t2. Inorder traversal of Binary Tree");
printf("\n\t3. Postorder traversal of Binary Tree");
printf("\n\t4. Preorder traversal of Binary Tree");
//printf("\n\t6. Search and replace");
printf("\n\t5. Exit");
printf("\n\t Enter choice: ");
scanf("%d",&choice);
if(choice<1 || choice>5)
printf("\n Invalid choice - try again");
}while(choice<1 || choice>5);
switch(choice)
{
case 1:
printf("\nEnter new element: ");
scanf("%d",&item);
root=insert(root,item);
printf("\n root is %d",root->info);
//printf("\n Inorder traversal of binary tree is: ");
//inorder(root);
break;
/*case 2:
printf("\n Enter the element to be deleted: ");
scanf("%d",&item_no);
root=delet(root,item_no);
inorder(root);
break;*/
case 2:
printf("\n Inorder traversal of binary tree is: ");
inorder(root);
break;
case 3:
printf("\n Postorder traversl of binary tree is: ");
postorder(root);
break;
case 4:
printf("\n Preorder traversal of binary tree is: ");
preorder(root);
break;
/*case 6:
printf("\n Search and replace operation in binary tree ");
root=search(root);
break;*/
default:
printf("\nEnd of program");
}/*end of switch*/
}while(choice!=5);
return(0);
}
struct tree * insert(struct tree *root,int x)
{
if(!root)
{
root=(struct tree *)malloc(sizeof(struct tree));
root->info=x;
root->left=NULL;
root->right=NULL;
return(root);
}
if(root->info<x)
root->left=insert(root->left,x);
else
{
if(root->info>x)
root->right=insert(root->right,x);
}
return(root);
}
void inorder(struct tree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d",root->info);
inorder(root->right);
}
return;
}
void postorder(struct tree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d",root->info);
}
return;
}
void preorder(struct tree *root)
{
if(root!=NULL)
{
printf("%d",root->info);
preorder(root->left);
preorder(root->right);
}
return;
}
/*function to delete a node from a binary tree*/
/*struct tree *delet(struct tree *ptr,int x)
{
struct tree *p1,*p2;
if(!ptr)
{
printf("\nNode not found");
return(ptr);
}
else
{
if(ptr->info<x)
{
ptr->right=delet(ptr->right,x);
/*return(ptr);*/
/*}
else if(ptr->info>x)
{
ptr->left=delet(ptr->left,x);
return(ptr);
}
else /*no. 2 else*/
/*{
if(ptr->info==x) /*no. 2 if*/
/*{
if(ptr->left==ptr->right) /*i.e. a leaf node*/
/*{
free(ptr);
return(NULL);
}
else if(ptr->left==NULL) /*a right subtree*/
/*{
p1=ptr->right;
free(ptr);
return(p1);
}
else if(ptr->right==NULL) /*a left subtree*/
/*{
p1=ptr->left;
free(ptr);
return(p1);
}
else
{
p1=ptr->right;
p2=ptr->right;
while(p1->left!=NULL)
{ p1=p1->left; }
p1->left=ptr->left;
free(ptr);
return(p2);
}
}/*end of no. 2 if*/
/* }/*end of no. 2 else*/
/*check which path to search for a given no.*/
/*}
return(ptr);
}
/*function to search and replace an element in the binary tree*/
/*struct tree *search(struct tree *root)
{
int no,i,ino;
struct tree *ptr;
ptr=root;
printf("\nEnter the element to be searched: ");
scanf("%d",&no);
fflush(stdin);
while(ptr)
{
if()
ptr=ptr->right;
else if(no<ptr->info)
ptr=ptr->left;
else
break;
}
if(ptr)
{
printf("\nElement %d which was search is found and is = %d",no,ptr->info);
printf("\nDo you want replace it, press 1 for yes: ");
scanf("%d",&i);
if(i==1)
{
printf("Enter new element: ");
scanf("%d",&ino);
ptr->info=ino;
}
else
printf("\n\tIt's okay.");
}
else
printf("\nElement %d does not exist in the binary tree.",no);
return(root);
}*/
9. Program for doubly linked list.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int num;
struct node* prev;
struct node* next;
};
struct node *first=NULL,*last;
void create();
void start();
void left();
void right();
void end();
void display();
void main()
{
int i,j,n,ch;
clrscr();
do
{
printf("\n\\\\\\\\\\\\\\select A option\\\\\\\\\\\\\\\\");
printf("\n1.create the list:-\n2.insert at start:-\n 3.left of any node:-\n4.right of the any node:\n5.end of the list:-\n6.display:-\n7.exit:-\n");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: start();
break;
case 3: left();
break;
case 4: right();
break;
case 5: end();
break;
case 6: display();
break;
case 7: exit(0);
default: printf("\n wrong input!!!!!!!!!!");
}
}
while(ch!=7);
getch();
}
void create()
{
struct node *item,*ptr,*x;
int n,i;
printf("enter the number of elements into the list:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(first==NULL)
{
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data to be inserted:");
scanf("%d",&item->num);
item->next=NULL;
item->prev=NULL;
first=item;
last=first;
}
else
{
ptr=first;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&x->num);
ptr->next=x;
x->prev=ptr;
x->next=NULL;
last=x;
}
}
}
void start()
{
struct node *item;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data :");
scanf("%d",&item->num);
if(first==NULL)
{
item->next=NULL;
item->prev=NULL;
first=item;
last=first;
}
else
{
item->next=first;
first->prev=item;
first=item;
first->prev=NULL;
}
}
void display()
{
struct node *ptr;
ptr=first;
if(first==NULL)
{
printf("\n list is empty:");
}
else
{
while(ptr!=NULL)
{
printf("\n %d",ptr->num);
ptr=ptr->next;
}
}
}
void end()
{
struct node *ptr,*item;
ptr=first;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&item->num);
item->next=NULL;
ptr->next=item;
item->prev=ptr;
}
void left()
{
struct node* ptr,*prior,*item,*x,*y,*r;
ptr=first;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the number whose left you want to insert the node : ");
scanf("%d",&item->num) ;
if(item->num==ptr->num)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data:");
scanf("%d",&y->num);
y->next=ptr;
ptr->prev=y;
y->prev=NULL;
first=y;
return;
}
while(ptr!=NULL)
{
if(item->num==ptr->num)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&x->num);
r=ptr->prev;
x->next=ptr;
ptr->prev=x;
r->next=x;
x->prev=r;
return;
}
else
{
ptr=ptr->next;
}
}
}
void right()
{
struct node *ptr,*item,*x,*r;
ptr=first;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the number whose right side you want to insert the node:");
scanf("%d",&item->num);
while(ptr!=NULL)
{
if(item->num==ptr->num)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data :");
scanf("%d",&x->num);
r=ptr->next;
r->prev=x;
x->next=r;
x->prev=ptr;
ptr->next=x;
return;
}
else
{
ptr=ptr->next;
}
}
}
9. Program for doubly linked list.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int num;
struct node* prev;
struct node* next;
};
struct node *first=NULL,*last;
void create();
void start();
void left();
void right();
void end();
void display();
void main()
{
int i,j,n,ch;
clrscr();
do
{
printf("\n\\\\\\\\\\\\\\select A option\\\\\\\\\\\\\\\\");
printf("\n1.create the list:-\n2.insert at start:-\n 3.left of any node:-\n4.right of the any node:\n5.end of the list:-\n6.display:-\n7.exit:-\n");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: start();
break;
case 3: left();
break;
case 4: right();
break;
case 5: end();
break;
case 6: display();
break;
case 7: exit(0);
default: printf("\n wrong input!!!!!!!!!!");
}
}
while(ch!=7);
getch();
}
void create()
{
struct node *item,*ptr,*x;
int n,i;
printf("enter the number of elements into the list:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(first==NULL)
{
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data to be inserted:");
scanf("%d",&item->num);
item->next=NULL;
item->prev=NULL;
first=item;
last=first;
}
else
{
ptr=first;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&x->num);
ptr->next=x;
x->prev=ptr;
x->next=NULL;
last=x;
}
}
}
void start()
{
struct node *item;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data :");
scanf("%d",&item->num);
if(first==NULL)
{
item->next=NULL;
item->prev=NULL;
first=item;
last=first;
}
else
{
item->next=first;
first->prev=item;
first=item;
first->prev=NULL;
}
}
void display()
{
struct node *ptr;
ptr=first;
if(first==NULL)
{
printf("\n list is empty:");
}
else
{
while(ptr!=NULL)
{
printf("\n %d",ptr->num);
ptr=ptr->next;
}
}
}
void end()
{
struct node *ptr,*item;
ptr=first;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&item->num);
item->next=NULL;
ptr->next=item;
item->prev=ptr;
}
void left()
{
struct node* ptr,*prior,*item,*x,*y,*r;
ptr=first;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the number whose left you want to insert the node : ");
scanf("%d",&item->num) ;
if(item->num==ptr->num)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data:");
scanf("%d",&y->num);
y->next=ptr;
ptr->prev=y;
y->prev=NULL;
first=y;
return;
}
while(ptr!=NULL)
{
if(item->num==ptr->num)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&x->num);
r=ptr->prev;
x->next=ptr;
ptr->prev=x;
r->next=x;
x->prev=r;
return;
}
else
{
ptr=ptr->next;
}
}
}
void right()
{
struct node *ptr,*item,*x,*r;
ptr=first;
item=(struct node*)malloc(sizeof(struct node));
printf("\n enter the number whose right side you want to insert the node:");
scanf("%d",&item->num);
while(ptr!=NULL)
{
if(item->num==ptr->num)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data :");
scanf("%d",&x->num);
r=ptr->next;
r->prev=x;
x->next=r;
x->prev=ptr;
ptr->next=x;
return;
}
else
{
ptr=ptr->next;
}
}
}
Comments
Post a Comment