Sie sind auf Seite 1von 8

Solutions #include<stdio.

h> #define max 5 int top=-1; int f=0; void main() { void push(int *,int); int pop(int *); void Traverse(int *); int isEmptyStack(int *); int isFull(int*); int item,ch,stk[max],f; clrscr(); printf("\n\n\n************************************\n"); printf("\n\n\t STACK OPERATION \n "); printf("\n\n\n************************************\n"); printf("\n\n\n\n\t\t1.push(insertion)\n\n\t\t2.pop(deletion)\n\n\t\t3.Traverse(visiting)\ n\n"); do { printf("\n\t\tenter your choice :"); scanf("%d",&ch); switch(ch) { case 1: printf("\n\n\tenter the element u want to input:"); scanf("%d",&item); push(stk,item); break; case 2: item=pop(stk); if(f) printf("item is poped : %d",item); else printf("\nstack is under flow"); break; case 3: Traverse(stk); break; } }while(ch<4); getch();

} void push(int s[],int item) { if(isFull(s)==0) printf("stack is overflow"); else { top=top+1; s[top]=item; } } int pop(int s[]) { int poped_item; if(isEmptyStack(s)==0) { poped_item=0; f=0; } else { f=1; poped_item=s[top]; top=top-1; } return poped_item; } void Traverse(int s[]) { int i; printf("\n\nthe elements of stack...."); if(top==-1) { printf("stack is empty"); } else { for(i=top;i>=0;i--) printf(" %d",s[i]); } } int isEmptyStack(int s[]) { If(top==-1)

return 0; else return 1; } int isFull(int s[]) { if(top == MAX 1) return 0; else return 1; } Program to check whether a string is palindrome or not using stack. #include<stdio.h> #include<conio.h> int top=-1; char stk[10]; void main() { void push(char); char pop(); int i; char str[30],ch; clrscr(); printf("enter a string"); gets(str); for(i=0;str[i]!='\0';i++) { push(str[i]); } for(i=0;i<strlen(str);i++) { if((ch=pop())!=str[i]) break; } if(i<strlen(str)) printf("not palindrome"); else printf("palindrome"); } void push(char ch) { top=top+1; stk[top]=ch; } char pop()

{ return stk[top--]; } QUEUE #include<stdio.h> struct que { int info; struct que *next; }; struct que *front,*rear; void insert(int); void disp(); void delete(); void main() { int ch,item; front=rear=NULL; while(1) { printf("OPERATION OF QUEUE USING LINKED LIST\n"); printf("1.insertion\n2.travrse\n3.delete\n4.exit\n"); printf("enter ur choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter the item"); scanf("%d",&item); insert(item); break; case 2: delete(); break; case 3: disp(); break; case 4: exit(); } } }

} void insert(int item) { struct que *ptr; ptr=(struct que *)malloc(sizeof(struct que )); if(ptr==NULL) printf("memory full"); else { ptr->info=item; ptr->next=NULL; if(front==NULL && rear==NULL) { front=rear=ptr; } else { rear->next=ptr; rear=ptr; }} } void delete(void) { int item; struct que *ptr; if(front==NULL) printf("memory empty"); else { item=front->info;; printf("%d item is deleted",item); ptr=front; if(front==rear) { front=rear=NULL; } else { front=front->next; free(ptr);} } } void disp() {

struct que *ptr; if(front==NULL) printf("queue is empty"); else { for(ptr=front;ptr!=NULL;ptr=ptr->next) printf("%d",ptr->info); } Disadvantages of linear queue The array size must be pre defined. In such case there are chances of poor utilization of memory. There are chances of lack of storage. In a linear queue when we delete an element, then only rear pointer gets incremented. CIRCULAR QUEUE implementation #include<stdio.h> #include<conio.h> #define MAX 5 void traverse(int [],int ,int); void insert(int *,int *,int *,int ); int delete(int *,int *,int *); int cnt=0; void main() { int CQ[MAX],rear,front,item,ch,i; clrscr(); front=rear=-1; while(1) { printf("\n\n\n*****************************\n"); printf("\n\n\n\tQUEUE OPERATION\n"); printf("\n\n\n******************************\n"); printf("\n\n\t\t1.INSERT\n"); printf("\n\n\t\t2.DELETE\n"); printf("\n\n\t\t3.TRAVERSE\n"); printf("\n\n\t\t4.EXIT\n"); printf("\n\n\n\t\tenter ur choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("enter the item"); scanf("%d",&item); insert(CQ,&front,&rear,item);

break; case 2: item=delete(CQ,&front,&rear); printf("%d deleted from queue",item); break; case 3: traverse(CQ,front,rear); break; default: exit(0); } } } void insert (int *CQ,int *front,int *rear,int item) { if((*rear==MAX-1 && *front==0)|| (*front==*rear+1)) { printf("overflow"); return ; } if(*front==-1) *front=*rear=0; else *rear=*rear+1; CQ[*rear]=item; cnt++; } int delete(int *CQ,int *front,int *rear) { int item; if(*front==-1) { printf("underflow"); return NULL; } item=CQ[*front]; if(*front==*rear) *front=*rear=-1; else *front=*front+1; cnt--; return item;

} void traverse(int CQ[],int front,int rear) { int i; if(front<=rear) { for(i=front;i<=rear;i++) printf("%d",CQ[i]); } else { for(i=front;i<=MAX-1;i++) printf("%d ",CQ[i]); for(i=0;i<=rear;i++) printf("%d ",CQ[i]); } }

Das könnte Ihnen auch gefallen