Sample Data Structure In C
////////////////////STACK////////////////////
#include <stdio.h> #include<ctype.h> #include <cstdlib> # define MAXSIZE 200 int STACK[MAXSIZE]; int top,CHOISE=1,i; void push(int); int pop(); void display(); void main() { int num; while(CHOISE==1){ printf("\n MAIN MENU\n\tPRESS 1 to Add element to stack\n\tPRESS 2 to Delete element from the stack\n\tPRESS 3 to Display STACK\n\tPRESS 9 to EXIT from the STACK"); scanf("%d",&CHOISE); switch(CHOISE){ case 1: printf("Enter Number To Insert In To Stack\n"); scanf("%d",&num); push(num); printf("NOW TOP VALUE ON STACK IS:::%d\n",STACK[top]); break; case 2: i=pop(); printf("Value returned from pop function is %d",i); break; case 3: display();break; case 9: exit(0); break; default: printf("YOU ENTER WRONG CHOISE\n"); } printf("Do you want to do more operations on Stack ( 1 for yes, any other key to exit)"); scanf("%d" , &CHOISE); } } void push(int y) { if(top>MAXSIZE){ printf("STACK IS FULL\n"); return; }else{ top++; STACK[top]=y; } } int pop() { int a; if(top<0){ printf("STACK EMPETY\n"); return 0; }else{ a=STACK[top]; top--; } return(a); } void display() { if (top<0){ printf("STACK is empty\n"); } else { printf("STACK IS::::-->\n"); for(i=1;i<=top;i++) printf("%d ",STACK[i]); printf("\n"); } } ////////////////////QUEUE///////////////////// #include <stdio.h> #include <cstdlib> #include <ctype.h> # define MAXSIZE 200 int QUEUE[MAXSIZE]; int front=0,rear=0,CHOISE=1,i; void add(int); void display(); int del(); void main() { int num; while(CHOISE==1){ printf("\n MAIN MENU\n\tPRESS 1 to Add element to QUEUE\n\tPRESS 2 to Delete element from the QUEUE\n\tPRESS 3 to Display QUEUE\n\tPRESS 9 to EXIT from the QUEUE "); scanf("%d",&CHOISE); switch(CHOISE){ case 1: printf("Enter Number To Insert In To QUEUE\n"); scanf("%d",&num); add(num); break; case 2: i=del(); printf("Value returned from pop function is %d",i); break; case 3: display();break; case 9: exit(0); break; default: printf("YOU ENTER WRONG CHOISE\n"); } printf("Do you want to do more operations on Stack ( 1 for yes, any other key to exit)"); scanf("%d" , &CHOISE); } } void add(int y) { if(rear>MAXSIZE){ printf("QUEUE IS FULL\n"); return; }else{ QUEUE[rear]=y; rear++; } } int del() { int a; if(front==rear){ printf("QUEUE IS EMPETY\n"); return 0; }else{ a=QUEUE[front]; front++; } return(a); } void display() { if (front == rear) printf("Queue is empty\n"); else { printf("Queue is :\n"); for(i=front;i<=rear-1;i++) printf("%d ",QUEUE[i]); printf("\n"); } } //////////////////DEQUEUE///////////////////// ->This Program will work for simple dequeue and for Input/Output restricted Dequeue #include <stdio.h> #include <conio.h> #include <cstdlib> #include <string.h> # define MAX 100 int deque_arr[MAX]; int front=0,rear=0,choice,option,i=1; int element; //Element of int type taking by user void input_que(); void output_que(); void non_que(); void delete_right(); void delete_left(); void insert_right(int); void insert_left(int); void show_forward( void ); // DISPLAY OF DEQUE : TOP TO BOTTOM void show_backward( void ); // DISPLAY OF DEQUE : BOTTOM TO TOP int length_deque( void ); // PRINTS LENGTH OF DEQUE void display_queue(); void main() { printf("Press 1.Input restricted dequeue\n"); printf("Press 2.Output restricted dequeue\n"); printf("Press 3.Non restricted dequeue\n"); printf("Enter your choice : \n"); scanf("%d",&choice); switch(choice) { case 1: input_que();break; case 2: output_que();break; case 3: non_que();break; default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of main()*/ void input_que() { while(1) { printf("Press 1.Insert at right\n"); printf("Press 2.Delete from left\n"); printf("Press 3.Delete from right\n"); printf("Press 4 to Show DEQUE From Front\n"); printf("Press 5 to Show DEQUE From End\n"); printf("Press 0.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter Element \n"); scanf("%d",&element); insert_right(element);break; case 2: delete_left();break; case 3: delete_right();break; case 4: show_forward();break; case 5: show_backward();break; case 0: exit(0);break; default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of input_que() */ void output_que() { while(1) { printf("Press 1.Insert at right\n"); printf("Press 2.Insert at left\n"); printf("Press 3.Delete from left\n"); printf("Press 4 to Show DEQUE From Front\n"); printf("Press 5 to Show DEQUE From End\n"); printf("Press 0.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter Element \n"); scanf("%d",&element); printf("RIGHT INSERTED VALUE IS %d\n",element); insert_right(element); break; case 2: printf("Enter Element \n"); scanf("%d",&element); printf("LEFT INSERTED VALUE IS %d\n",element); insert_left(element); break; case 3: delete_left(); break; case 4: show_forward(); break; case 5: show_backward(); break; case 0: exit(0);break; default: printf("Wrong choice\n");break; }/*End of switch*/ }/*End of while*/ }/*End of output_que() */ void non_que() { while(1) { printf("\n\t==================================\n"); printf("\t| DEQUE OPERATIONS |"); printf("\n\t==================================\n"); printf("\n\n\tPush Element From Top ........ [1]"); printf("\n\n\tPush Element From End ........ [2]"); printf("\n\n\tPop Element From Top ......... [3]"); printf("\n\n\tPop Element From End ......... [4]"); printf("\n\n\tShow DEQUE From Front ........ [5]"); printf("\n\n\tShow DEQUE From End .......... [6]"); printf("\n\n\tGo Back To DEQUE TYPE ........ [7]"); printf("\n\n\tExit Program ................. [0]"); printf("\n\n\tEnter Your Option : "); scanf("%d", &option); switch(option) { case 1: printf("Enter Element \n"); scanf("%d",&element); insert_left(element); break; case 2: printf("Enter Element \n"); scanf("%d",&element); insert_right(element); break; case 3: delete_left(); break; case 4: delete_right(); break; case 5: show_forward(); // DISPLAY OF DEQUE : TOP TO BOTTOM break; case 6: show_backward(); break; case 7: main(); break; case 0: exit(0); break; default: printf("YOU ENTERED WRONG CHOISE\n"); break; } } } void insert_left(int x) { if(front > MAX){ printf("\n\tDEQUEUE IS OVER FLOW\n"); return; }else{ front=front-1; deque_arr[front]=x; printf("Element Inserted From Top Of DeQueue Is::-->%d\n",x); } } void insert_right(int x) { if(rear > MAX){ printf("\n\tDEQUEUE IS OVER FLOW\n"); return; }else{ deque_arr[rear]=x; rear++; printf("Element Inserted From End Of Queue Is::-->%d\n",x); } } void delete_left() { int a; if(front == rear){ printf("\n\tDEQUE IS EMPTY\n"); return; }else{ a=deque_arr[front]; front++; printf("Element Deleed From Top Of DeQueue Is::-->%d\n",a); } } void delete_right() { int a; if(rear == front){ printf("\n\tDEQUE IS EMPTY\n"); return; }else{ a=deque_arr[rear]; rear--; printf("Element Deleed From End Of DeQueue Is::-->%d\n",a); } } void show_forward() { int i; if(front == rear){ printf("\n\tEMPTY DEQUE!\n"); return; } printf("FRONT IS::%d&&&&REAR IS:::%d\n",front,rear); printf("\n\n\tThe DEQUE IS:- "); for(i=front;i<=rear-1;i++) printf("%d ",deque_arr[i]); printf("\n"); } void show_backward() { int i; if(front == rear){ printf("\n\tEMPTY DEQUE!\n"); return; } printf("\n\n\tThe DEQUE IS:- "); for(i=rear-1;i>=front;i--) printf("%d ",deque_arr[i]); printf("\n"); } |
Comments
Post a Comment