#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 10 //LINKED LIST struct node{ int data; struct node *next; }; struct node *head=NULL; int insertbegin(int value){ struct node *newnode; newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=value; if(head==NULL){ newnode->next=NULL; head=newnode; } else{ newnode->next=head; head=newnode; } printf("\nInsertion successful\n"); } int insertend(int value){ struct node *newnode; newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=value; newnode->next=NULL; if(head==NULL){ head=newnode; } else{ struct node *temp=head; while(temp->next!=NULL){ temp=temp->next; } temp->next=newnode; } printf("\nInsertion successful\n"); } int insertspecific(int value){ int pos; struct node *newnode; newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=value; if(head==NULL){ newnode->next=NULL; head=newnode; } else{ printf("enter the position : "); scanf("%d",&pos); struct node *temp=head,*temp2; int i=1; for(i=1;i<pos;i++){ temp2=temp; temp=temp->next; } temp2->next=newnode; newnode->next=temp; } printf("\nInsertion successful\n"); } int deletebegin(){ struct node *temp=head; if(head==NULL){ printf("\n list empty(underflow)\n"); } else{ head=temp->next; free(temp); printf("deletion successful\n"); } } int deleteend(){ if(head==NULL){ printf("\nlist empty(underflow)\n"); } else{ struct node *temp=head,*temp2; while(temp->next!=NULL){ temp2=temp; temp=temp->next; } free(temp); temp2->next=NULL; printf("\nDeletion successful\n"); } } int deletespecific(){ int pos; printf("enter the position :: "); scanf("%d",&pos); struct node *temp=head,*temp2; if(pos==1){ head=temp->next; free(temp); } else{ int i; for(i=1;i<=pos-1;i++){ temp2=temp; temp=temp->next; } temp2->next=temp->next; free(temp); printf("\ndeletion successful\n"); } } int display(){ struct node *temp=head; if(head==NULL){ printf("\nlist empty(underflow)\n"); } else{ printf("\nlist : "); while(temp!=NULL){ printf("%d ",temp->data); temp=temp->next; } } printf("\n------------------------------------------------------------\n"); } //STACK int stack[MAX],top=-1; int push(){ if(top==MAX-1){ printf("Overflow\n"); } else{ int value; printf("\nenter the value :: "); scanf("%d",&value); top++; stack[top]=value; printf("\nValue pushed successfully\n"); } } int pop(){ if(top==-1){ printf("Underflow\n"); } else{ printf("value poped from stack :: | %d |",stack[top]); top--; } } int displaystack(){ int i; if(top==-1){ printf("\nstack empty(underflow)\n"); } else{ printf("\nstack = "); for(i=top;i>=0;i--){ printf("\n| %d |",stack[i]); } } } //QUEUE int queue[MAX],front=-1,rear=-1; int enqueue(){ if(rear==MAX-1){ printf("overflow\n"); } else{ int value; printf("\nenter the value : "); scanf("%d",&value); if(front==-1 || rear==-1){ front=0; rear=0; queue[rear]=value; } else{ rear++; queue[rear]=value; } printf("enqueued successful\n"); } } int dequeue(){ if(rear==-1 ||rear < front){ printf("\nqueue underflow"); } else{ if(front==rear){ printf("\ndequeued = %d ",queue[front]); front=rear=-1; } else{ printf("\ndequeued = %d ",queue[front]); front++; } printf("\ndequeued successfull\n"); } } int displayqueue(){ int i; if(front==-1){ printf("\nunderflow\n"); } else{ printf("\n queue = | "); for(i=front;i<=rear;i++){ printf("%d ",queue[i]); } printf("|"); } } //main function int main(){ dss: printf("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); int ds; printf("\nselect DATA STRUCTURE\n"); printf("======================================\n"); printf("(1)linked list \n(2)stack \n(3)queue \n(0)Exit\n"); printf("\nEnter DS : "); scanf("%d",&ds); printf("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); //DS switch(ds){ //LINKED LIST case 1: printf("\n\t\t------------------------------\n"); printf("\t\t| LINKED LIST |"); printf("\n\t\t------------------------------\n"); int value,i,op; shikha: printf("\n\n------------------------------------------------------------\n"); printf("\nINSERT = 1.begin 2.end 3.specific \nDELETE = 5.begin 6.end 7.specific \n\n4.display 0.exit 99.change data structure\n\nenter operation :: "); scanf("%d",&op); printf("\n\n------------------------------------------------------------\n"); switch(op){ case 1: printf("enter the value : "); scanf("%d",&value); insertbegin(value); break; case 2: printf("enter the value : "); scanf("%d",&value); insertend(value); break; case 3: printf("enter the value : "); scanf("%d",&value); insertspecific(value); break; case 5: deletebegin(); break; case 6: deleteend(); break; case 7: deletespecific(); break; case 4: display(); getch(); break; case 0: exit(0); case 99: goto dss; default: printf("Invalid operation"); } goto shikha; break; //STACK case 2: printf("\n\t\t-----------------------\n"); printf("\t\t| STACK |"); printf("\n\t\t-----------------------\n"); int opp; tanmay: printf("\n\n------------------------------------------------------------\n"); printf("\n1.PUSH 2.POP \n\n3.displaystack 0.exit 99.change data structure\n\nenter operation :: "); scanf("%d",&opp); printf("\n\n------------------------------------------------------------\n"); switch(opp){ case 1: push(); displaystack(); break; case 2: pop(); displaystack(); break; case 3: displaystack(); getch(); break; case 99: goto dss; case 0: exit(0); default: printf("Invalid operation"); } goto tanmay; break; //queue case 3: printf("\n\t\t-----------------------\n"); printf("\t\t| QUEUE |"); printf("\n\t\t-----------------------\n"); int opr; shiktan: printf("\n\n------------------------------------------------------------\n"); printf("\n1.Enqueue 2.Dequeue \n\n3.displayqueue 0.exit 99.change data structure\n\nenter operation :: "); scanf("%d",&opr); printf("\n\n------------------------------------------------------------\n"); switch(opr){ case 1: enqueue(); displayqueue(); break; case 2: dequeue(); displayqueue(); break; case 3: displayqueue(); getch(); break; case 99: goto dss; case 0: exit(0); default: printf("Invalid operation"); } goto shiktan; break; //exit case 0: exit(0); default: printf("Invalid operation"); } getch(); goto dss; }