/* C program to implement stacks using arrays */ #define MAX 10 void push(); void pop(); void display(); int a[MAX],top=-1; main() { int ch; clrscr(); while(1) { printf("\n 1 push an element \t 2 pop \t 3 display \t 4 exit"); printf("\n\n enter your choice"); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\n enter a valid choice"); break; } } } void push() { int ele; if(top==MAX-1) printf("\n stack is full"); else { top=top+1; printf("\n enter an element to be inserted"); scanf("%d",&ele); a[top]=ele; }

} void pop() { if(top==-1) printf("\n stack is empty"); else { printf("\n removed element is %d",a[top]); top--; } } void display() { int i; if(top==-1) printf("\n stack is empty"); else { for(i=0;i<=top;i++) printf("\t %d",a[i]); } } OUTPUT:

/* C program to implement queues using arrays */ #define MAX 5 void enqueue(); void delete(); void display(); int a[MAX],front=-1,rear=-1; main() { int ch; clrscr(); while(1) { printf("\n 1 enqueue \t 2 dequeue \t 3display \t 4 exit"); printf("\n\n enter your choice"); scanf("%d",&ch); switch(ch) { case 1: enqueue(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\n enter the correct choice"); break; } } } void enqueue() { int ele; if(rear==MAX-1) { printf("\n queue is full"); } else { if(front==-1) front=0; printf("\n enter the element to be inserted");

scanf("%d",&ele); rear++; a[rear]=ele; } } void delete() { if(front==-1||front>rear) { printf("\n queue is empty"); return; } else printf("\n removed element is %d",a[front]); front++; } void display() { int i; if(front==-1) printf("\n queue is empty"); else { for(i=front;i<=rear;i++) { printf("\n %d",a[i]); } } }

OUTPUT

T.A.K.R

ds stacks and queues using arrays (1).pdf

ds stacks and queues using arrays (1).pdf. ds stacks and queues using arrays (1).pdf. Open. Extract. Open with. Sign In. Main menu.

120KB Sizes 2 Downloads 280 Views

Recommend Documents

No documents