Upcoming SlideShare
×

# Datastructures asignment

1,059 views

Published on

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total views
1,059
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
86
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Datastructures asignment

1. 1. /* IMPLEMENTATION OF THE STACK USING ARRAYS *//* STACK_A.C */# include<stdio.h># include<string.h># include<ctype.h>#include<conio.h># define size 3int top = -1;int flag = 0;int stack[size];void push(int *, int);int pop(int *);void display(int *);/* Definition of the push function */void push(int s[], int d){ if(top ==(size-1)) flag = 0; else { flag = 1; ++top; s[top] = d; }}/* Definition of the pop function */int pop(int s[]){ int popped_element; if(top == -1) { popped_element = 0; flag = 0; } else { flag = 1; popped_element = s[top]; --top; } return (popped_element);}/* Definition of the display function */void display(int s[]){ int i; if(top == -1) printf("n Stack is empty"); else { for(i = top; i >= 0; --i) printf("n %d", s[i] ); }}
2. 2. /* Function main */void main(){ int data; char choice; int q = 0; int top = -1; clrscr(); do { printf(" n enter i to insert"); printf(" n enter p to delete"); printf(" n enter q to quit"); printf("n enter your choice : "); do { choice = getchar(); choice =tolower(choice); }while(strchr("ipq",choice)==NULL); switch(choice) { case i : printf("n Input the element to push:"); scanf("%d", &data); push(stack, data); if(flag) { printf("n After inserting "); display(stack); if(top == (size-1)) printf("n Stack is full"); } else printf("n Stack overflow after pushing"); break; case p : data = pop(stack); if(flag) { printf("n Data is popped: %d", data); printf("n Rest data in stack is as follows:n"); display(stack); } else printf("n Stack underflow" ); break; case q: q = 1; } } while(!q);}
3. 3. /* THIS PROGRAM DEMONSTRATES THE STACK OPERATIONS USING OOPS-CLASS STACK*//* cstack.cpp */#include<iostream.h>#include<conio.h>#include<stdio.h> class stack { int stack[30]; int top,n; public: stack() { top=-1; //indicates stack is empty n=5; } void push(int); int pop(); void display(); }; void stack::push(int x) { if(top<n) { top++; stack[top]=x; } else cout<<"n the stack is full"; } int stack::pop() { int v=-99; if(top>=0) { v=stack[top]; top--; } return v; } void stack::display() { int i; if(top>=0) { for(i=top;i>=0;i--) cout<<stack[i]<<" "; } else cout<<"n stack is empty"; } void main() { stack s; char ch; int x; clrscr(); do { cout<<"n enter i to insert an element"; cout<<"n enter d to view the contents"; cout<<"n enter r to remove an element";
4. 4. cout<<"n enter q to quit"; cout<<"n enter your choice : "; cin>>ch; switch(ch) { case i: cout<<"n enter the value to insert"; cin>>x; s.push(x); break; case d: cout<<"n"; s.display(); break; case r: x=s.pop(); if(x==-99) cout<<"n stack is empty"; else cout<<" n the element removed is "<<x; break; } }while(ch!=q); }OUTPUT:enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : rstack is emptyenter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : ienter the value to insert : 1enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : ienter the value to insert : 2enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : ienter the value to insert : -1
5. 5. enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : d-1 2 1 enter the value to insert : 2enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : ienter the value to insert : -1enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : d-1 2 1 enter i to insert an element enter d to view the contents enter r to remove an element enter q to quit enter your choice : rthe element removed is -1enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : rthe element removed is 2enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : rthe element removed is 1enter i to insert an elemententer d to view the contentsenter r to remove an elemententer q to quitenter your choice : rstack is empty
6. 6. /*THE FOLLOWING PROGRAM CONVERTS ANY infix EXPRESSION TO POSTFIXpostfix.c */#include<iostream.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>int precedence(char op){ switch(op) { case -:return 1; case +:return 1; case *:return 2; case /:return 2; case \$:return 3; case (:return 0; }return -1;} void push(char a[],int *top,char ch) { a[*top]=ch; (*top)++; } char pop(char a[],int *top) { *top=*top-1; return a[*top]; } void main() { int top=0,i=0,j=0,k,l,length=0; char stack[50],infix[50],postfix[50]; char ch; clrscr(); cout<<"Enter the infix expression : n"; scanf("%s",infix); length=strlen(infix); for(i=0;i<length;i++) { if(infix[i]==() push(stack,&top,infix[i]); if(isalpha(infix[i])) { postfix[j]=infix[i]; j++; }
7. 7. if(infix[i]==)) { while(stack[top-1]!=() { postfix[j]=pop(stack,&top); j++; } pop(stack,&top); } if(infix[i]==+||infix[i]==-||infix[i]==/||infix[i]==*||infix[i]==\$) { if(precedence(stack[top-1])<precedence(infix[i])) push(stack,&top,infix[i]); else { while(precedence(stack[top-1])>=precedence(infix[i])) { postfix[j]=pop(stack,&top); j++; } push(stack,&top,infix[i]); } }} while(top!=0) { postfix[j]=pop(stack,&top); j++; } cout<<"nThe postfix expression is :n"; for(k=0;k<j;k++) cout<<postfix[k];getch();}OUTPUT:Enter the infix expression :(((X*Y-A*B)-D/F)+A)*BThe postfix expression is :XY*AB*-DF/-A+B*do you want to continue (y/n) : yEnter the infix expression :A*B-P/(Q*(A-C+P*D)/B)The postfix expression is :AB*PQAC-PD*+*B//-do you want to continue (y/n) : n
8. 8. /* INSERTION AND DELETION IN A QUEUE ARRAY IMPLEMENTATION *//* queue_a.c */# include<stdio.h># include<string.h># include<ctype.h># include<stdlib.h># define size 5int ch;int q[size];int rear = 0;int front = 0;void Insert_queue();void Delete_queue();void Display_queue();/* Function to create queue */void Insert_queue(){ printf("n Input the element :"); scanf("%d", &ch); if(rear < size) { rear ++; q[rear] = ch ; if(front == 0) front = 1; } else printf("n Queue is overflow");}/* Function to perform delete operation */void Delete_queue(){ if (front == 0) { printf("n Underflow"); return ; } else { ch = q[front]; printf("n Element deleted : %d", ch); } if(front == rear) { front = 0; rear = 0; } else front = front + 1;}
9. 9. /* Output function */void Display_queue() //char q[]){ int i; if (front == 0) return; for(i = front ; i <= rear; i++) printf(" %d ", q[i]);}/* Function main */void main(){ int k = 0; char choice; clrscr(); do { printf("nInsert->i Delete->d Quit->q:"); printf("nInput the choice : "); do { choice = getchar(); choice = tolower(choice); }while(strchr("idq",choice)==NULL); switch(choice) { case i : Insert_queue(); printf("n Queue after inserting "); Display_queue(); break; case d : Delete_queue(); printf("n Queue after deleting:n"); Display_queue(); break; case q: k = 1; } } while(!k);}
10. 10. /* THIS PROGRAM DEMONSTRATES OPERATIONS ON A CIRCULAR QUEUEUSING OOPS-CLASS QUEUE –cqueue.cpp*/#include<iostream.h>#include<conio.h>#include<stdio.h> class queue { int q[30]; //queue declaration int f,r ; int n; public: queue() //constructor { f=r=0; //indicates queue is empty n=5; } void insert(int); int delet(); void display(); }; void queue::insert(int x) { if((r+1)%n==f) cout<<"n the queue is full"; else { r=(r+1)%n; q[r]=x; } } int queue::delet() { int v=-99; if(f!=r) { f=(f+1)%n; v=q[f]; } return v; } void queue::display() { int i; if(f!=r) { i=f; while(i!=r) { i=(i+1)%n; cout<<q[i]<<" "; } } else cout<<"n queue is empty"; } void main() { queue s; char ch;int x; clrscr(); do
11. 11. { cout<<"n enter i to insert an elemnt"; cout<<"n enter d to view the contents"; cout<<"n enter r to remove an elemnt"; cout<<"n enter q to quit"; cout<<"n enter your choice : "; cin>>ch; switch(ch) { case i: cout<<"n enter the value to insert"; cin>>x; s.insert(x); break; case d: cout<<"n"; s.display(); break; case r: x=s.delet(); if(x==-99) cout<<"n queue is empty"; else cout<<" n the element removed is "<<x; break; } }while(ch!=q); }OUTPUT :assume size of queue is 3 integersInsert->i Delete->d Quit->q:Input the choice : iInput the element :1Queue after inserting 1Insert->i Delete->d Quit->q:Input the choice : iInput the element :2Queue after inserting 1 2Insert->i Delete->d Quit->q:Input the choice : dElement deleted : 1Queue after deleting: 2Insert->i Delete->d Quit->q:Input the choice : dElement deleted : 2 Queue after deleting:queue is emptyInsert->i Delete->d Quit->q:Input the choice : q