4. DOUBLY LINKED LIST PROGRAM #include<stdio.h> #include<conio.
h> void insert(); void delete(); void display(); void find(); struct list { int info; struct list *next; struct list *prev; }*node,*ptr,*temp,*head=NULL; void main() { int choice; clrscr(); do { printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\nEnter your option"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: find(); break; case 5: exit(1); }
}while(choice!=5); getch(); } void insert() { int pos,i; temp=(struct list*)malloc(sizeof(struct list)); printf("\nEnter the data to be inserted"); scanf("%d",&temp->info); if(head==NULL) { head=temp; head->next=NULL; head->prev=NULL; } else { printf("\n Enter the position to be inserted"); scanf("%d",&pos); if(pos==1) { temp->next=head; temp->prev=NULL; head->prev=temp; head=temp; } else { ptr=head; for(i=1;i<pos-1&&ptr->next!=NULL;i++) { ptr=ptr->next; } temp->next=ptr->next; ptr->next=temp; temp->prev=ptr; ptr->next->prev=temp; } } } void delete() { int pos,i; if(head==NULL) printf("\nthe list is empty"); else
printf("\nEnter the position of data to be deleted"); scanf("%d",&pos); temp=head; if(pos==1) { printf("\nThe deleted element is %d",temp->info); head=temp->next; head->prev=NULL; } else { ptr=head; for(i=1;i<pos-1;i++) ptr=ptr->next; node=ptr->next; printf("The deleted element is %d",node->info); ptr->next=node->next; } } void display() { if(head==NULL) printf("\nNo elements in the list"); else { printf("\nThe elements in the stack are\n"); for(ptr=head;ptr!=NULL;ptr=ptr->next) printf("%d\t",ptr->info); } } void find() { int a,flag=0,count=0; if(head==NULL) printf("\nThe list is empty"); else { printf("\nEnter the element to be searched"); scanf("%d",&a); for(ptr=head;ptr!=NULL;ptr=ptr->next) { count++; if(ptr->info==a) { flag=1; printf("\nThe element is found");
printf("\nThe position is %d",count); break; } } if(flag==0) printf("The element is not found"); } } OUTPUT [Link] [Link] [Link] [Link] [Link] Enter your option1 Enter the data to be inserted5 [Link] [Link] [Link] [Link] [Link] Enter your option1 Enter the data to be inserted6 Enter the position to be inserted1 [Link] [Link] [Link] [Link] [Link] Enter your option3 The elements in the stack are 6 5 [Link] [Link] [Link] [Link] [Link] Enter your option2
Enter the position of data to be deleted1 The deleted element is 6 [Link] [Link] [Link] [Link] [Link] Enter your option2 Enter the position of data to be deleted1 The deleted element is 5 [Link] [Link] [Link] [Link] [Link] Enter your option4 The list is empty [Link] [Link] [Link] [Link] [Link] Enter your option1 Enter the data to be inserted5 [Link] [Link] [Link] [Link] [Link] Enter your option1 Enter the data to be inserted2 Enter the position to be inserted1 [Link] [Link] [Link] [Link]
[Link] Enter your option4 Enter the element to be searched2 The element is found The position is 1
5. ARRAY IMPLEMENTATION OF STACK ADT PROGRAM #include<stdio.h> #include<conio.h> #define maxsize 10 int stack[20],top=-1; void push(); void pop(); void display(); void main() { int choice; clrscr(); do { printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\nEnter your option "); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display();
break; case 4: exit(1); } }while(choice!=4); getch(); } void push() { int data; if(top==maxsize) printf("\nStack Overflow"); else { top++; printf("\nEnter the data to be inserted "); scanf("%d",&data); stack[top]=data; } } void pop() { if(top==-1) printf("\nStack Underflow"); else { printf("\nThe deleted element in the stack is %d",stack[top]); top--; } } void display() { int i; if(top==-1) printf("\nNo elements in the stack"); else { printf("\nThe elements in the stack are \n"); for(i=0;i<=top;i++) printf("%d\t",stack[i]); } } OUTPUT
[Link] [Link] [Link] [Link] Enter your option 1 Enter the data to be inserted 100 [Link] [Link] [Link] [Link] Enter your option 1 Enter the data to be inserted 50 [Link] [Link] [Link] [Link] Enter your option 3 The elements in the stack are 100 50 [Link] [Link] [Link] [Link] Enter your option 2 The deleted element in the stack is 50 [Link] [Link] [Link] [Link] Enter your option 3 The elements in the stack are 100 [Link] [Link] [Link] [Link] Enter your option 4 6. LINKED LIST IMPLEMENTATION OF STACK ADT
PROGRAM #include<stdio.h> #include<conio.h> void push(); void pop(); void display(); struct stack { int info; struct stack *next; }*node,*ptr,*temp,*top=NULL; void main() { int choice; clrscr(); do { printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); } }while(choice!=4); getch(); } void push() { int data; temp=(struct stack*)malloc(sizeof(struct stack));
printf("\nEnter the data to be inserted"); scanf("%d",&data); if(top==NULL) { top=temp; temp->info=data; temp->next=NULL; } else { temp->info=data; temp->next=top; top=temp; } } void pop() { if(top==NULL) printf("\nStack is empty"); else { printf("\nThe deleted element is %d",top->info); top=top->next; } } void display() { int i; if(top==NULL) printf("\nThere are no elements in the stack"); else { printf("\nThe elements in the stack are\n"); for(ptr=top;ptr!=NULL;ptr=ptr->next) printf("%d\t",ptr->info); } } OUTPUT [Link] [Link] [Link] [Link] Enter your choice 1
Enter the data to be inserted 20 [Link] [Link] [Link] [Link] Enter your choice 1 Enter the data to be inserted 40 [Link] [Link] [Link] [Link] Enter your choice 3 The elements in the stack are 40 20 [Link] [Link] [Link] [Link] Enter your choice 2 The deleted element is 40 [Link] [Link] [Link] [Link] Enter your choice 4 7. IMPLEMENTATION OF BALANCED PARENTHESIS USING STACK ADT Main program #include<stdio.h> #include<conio.h> #include"Z:STACK.C" void main() { char exp[50]; int i=0,n; clrscr(); printf("Enter the expression"); scanf("%s",exp); n=strlen(exp); for(i=0;i<=n;i++)
{ if(exp[i]=='(') push(exp[i]); else if(exp[i]==')') { if(top==NULL) { printf("\nUnbalanced Expression"); getch(); exit(1); } else pop(); } } if(top==NULL) printf("\nBalanced Parenthesis"); else printf("\nUnbalanced Parenthesis"); getch(); } Sub Program #include<stdio.h> #include<conio.h> struct stack { char element; struct stack *next; }*node,*temp,*top=NULL; void push(char x) { temp=(struct stack*)malloc(sizeof(struct stack)); temp->element=x; if(top==NULL) { top=temp; temp->next=NULL; } else { temp->next=top; top=temp;
} } void pop() { if(top==NULL) printf("Stack Underflow"); else { temp=top; top=temp->next; free(temp); } } OUTPUT Enter the expression (A+B) Balanced Parenthesis
8. QUEUE IMPLEMENTATION USING ARRAY PROGRAM #include<stdio.h> #include<conio.h> #define maxsize 10 int queue[20],front=-1,rear=-1; void insert(); void delete(); void display(); void main() { int choice; clrscr(); do { printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\[Link]"); printf("\nEnter your option"); scanf("%d",&choice); switch(choice) {
case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(1); } }while(choice!=4); getch(); } void insert() { int data; printf("\nEnter the element to be inserted"); scanf("%d",&data); if(rear==maxsize) printf("\nQueue is full"); else if(front==-1) { front++; rear++; queue[front]=data; } else { rear++; queue[rear]=data; } } void delete() { if(front==-1) printf("\nQueue is empty"); else if(front==rear) { printf("\nThe deleted element is %d",queue[front]); front=-1; rear=-1; } else {
printf("\nThe deleted element is %d",queue[front]); front++; } } void display() { int i; if(front==-1) printf("\nNo elements in the queue"); else { printf("\nThe elements in the queue are\n"); for(i=front;i<=rear;i++) printf("%d\t",queue[i]); } } OUTPUT [Link] [Link] [Link] [Link] Enter your option 1 Enter the element to be inserted 50 [Link] [Link] [Link] [Link] Enter your option 1 Enter the element to be inserted 20 [Link] [Link] [Link] [Link] Enter your option 2 The deleted element is 50 [Link] [Link] [Link] [Link]
Enter your option 3 The elements in the queue are 20 [Link] [Link] [Link] [Link] Enter your option 4 9. POLYNOMIAL ADDITION USING LINKED LIST PROGRAM #include<stdio.h> #include<conio.h> #include<malloc.h> struct link { int coeff; int pow; struct link *next; }; struct link *poly1=NULL,*poly2=NULL,*poly=NULL; void create(struct link *node) { char ch; do { printf("\n Enter Coeff\n "); scanf("%d",&node->coeff); printf("\nEnter power \n"); scanf("%d",&node->pow); node->next=(struct link*)malloc(sizeof(struct link)); node=node->next; node->next=NULL; printf("\nContinue(y/n): "); ch=getch(); } while(ch=='y'||ch=='y'); } void show(struct link *node) { while(node->next!=NULL) { printf("%dx^%d",node->coeff,node->pow);
node=node->next; if(node->next!=NULL) printf("+"); } } void polyadd(struct link *poly1,struct link *poly2,struct link *poly) { while(poly1->next&&poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link*)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } while(poly1->next||poly2->next) { if(poly1->next) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; }
poly->next=(struct link*)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } } void main() { char ch; clrscr(); do { poly1=(struct link*)malloc(sizeof(struct link)); poly2=(struct link*)malloc(sizeof(struct link)); poly=(struct link*)malloc(sizeof(struct link)); printf("\nEnter 1st number: \n"); create(poly1); printf("\n\nEnter 2nd number:\n "); create(poly2); printf("\n\n1st number\n"); show(poly1); printf("\n\n2nd number\n "); show(poly2); polyadd(poly1,poly2,poly); printf("\n\nAdded polynomial\n"); show(poly); printf("\n\nAdd two more numbers "); ch=getch(); } while(ch=='y'||ch=='y'); } OUTPUT Enter 1st number: Enter Coeff 3 Enter power 2 Continue(y/n): Enter 2nd number:
Enter Coeff 4 Enter power 2 Continue(y/n): 1st number 3x^2 2nd number 4x^2 Added polynomial 7x^2
Add two more numbers