ds all prgms

98
POLYNOMIAL ADDITION USING LINKED LIST CODING: #include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct polynode { int coeff,exp; struct polynode*next; }node; node*first=NULL; node*second=NULL; node*result=NULL; void createfirst(int,int); void createsecond(int,int); void displayfirst(node*); void displaysecond(node*); void displayresult(node*); void addpoly(node*,node*); node *getnode(); node *getnode() { node*temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; return temp; } void main() { int r,s,i,j,x,y,ch; clrscr(); printf("\nenter the number of terms in first and second polynomial:"); scanf("%d%d",&r,&s); for(i=0;i<r;i++) { printf("\n1st");

Upload: pavithraram

Post on 21-Dec-2015

216 views

Category:

Documents


1 download

DESCRIPTION

data structures

TRANSCRIPT

Page 1: Ds All Prgms

POLYNOMIAL ADDITION USING LINKED LISTCODING:

#include<stdio.h>#include<conio.h>#include<stdlib.h>typedef struct polynode{int coeff,exp;struct polynode*next;}node;node*first=NULL;node*second=NULL;node*result=NULL;void createfirst(int,int);void createsecond(int,int);void displayfirst(node*);void displaysecond(node*);void displayresult(node*);void addpoly(node*,node*);node *getnode();node *getnode(){node*temp;temp=(node*)malloc(sizeof(node));temp->next=NULL;return temp;}void main(){int r,s,i,j,x,y,ch;clrscr();printf("\nenter the number of terms in first and second polynomial:");scanf("%d%d",&r,&s);for(i=0;i<r;i++){printf("\n1st");printf("\nenter the coeff part:");scanf("%d",&x);printf("\nenter the exponent part:");scanf("%d",&y);createfirst(x,y);}for(i=0;i<s;i++){

Page 2: Ds All Prgms

printf("\n2nd");printf("\nenter the coeff part:");scanf("%d",&x);printf("\nenter the exponent part:");scanf("%d",&y);createsecond(x,y);}fflush(stdin);do{printf("\nPOLYNOMIAL ADDITION");printf("\n1.display the first equation");printf("\n2.display the second equation");printf("\n3.add the equation");printf("\n4.display the result");printf("\n5.exit");printf("\nenter your choice:");scanf("%d",&ch);switch(ch){case 1:displayfirst(first);break;case 2:displaysecond(second);break;case 3:addpoly(first,second);break;case 4:displayresult(result);break;case 5:exit(0);default:printf("\ninvalid choice");}}while(ch!=5);}void createfirst(int x,int y){node*temp,*q;temp=getnode();temp->coeff=x;temp->exp=y;temp->next=NULL;

Page 3: Ds All Prgms

if(first==NULL)first=temp;else{q=first;while(q->next!=NULL)q=q->next;q->next=temp;}}void createsecond(int x,int y){node*temp,*q;temp=getnode();temp->coeff=x;temp->exp=y;temp->next=NULL;if(second==NULL)second=temp;else{q=second;while(q->next!=NULL)q=q->next;q->next=temp;}}void displayfirst(node*first){printf("\nfirst equation is:");while(first!=NULL){printf("%dx^%d+",first->coeff,first->exp);first=first->next;}}void displaysecond(node*second){printf("\nsecond equation is:");while(second!=NULL){printf("%dx^%d+",second->coeff,second->exp);second=second->next;}}void displayresult(node*result)

Page 4: Ds All Prgms

{printf("\nresult is:");while(result!=NULL){printf("%dx^%d+",result->coeff,result->exp);result=result->next;}}void addpoly(node*first,node*second){struct polynode*temp;if(first==NULL&&second==NULL)return;while(first!=NULL&&second!=NULL){if(result==NULL){result=getnode();temp=result;}else{temp->next=getnode();temp=temp->next;}if(first->exp>second->exp){temp->coeff=first->coeff;temp->exp=first->exp;first=first->next;}elseif(first->exp<second->exp){temp->coeff=second->coeff;temp->exp=second->exp;second=second->next;}elseif(first->exp==second->exp){temp->exp=second->exp;temp->coeff=(first->coeff)+(second->exp);second=second->next;first=first->next;}

Page 5: Ds All Prgms

}while(first!=NULL){if(result==NULL){result=getnode();temp=result;}else{temp->next=getnode();temp=temp->next;}temp->coeff=first->coeff;temp->exp=first->exp;first=first->next;}while(second!=NULL){if(result==NULL){result=getnode();temp=result;}else{temp->next=getnode();temp=temp->next;}temp->coeff=second->coeff;temp->exp=second->exp;second=second->next;}}

OUTPUT:

enter the number of terms in first and second polynomial:33

1stenter the coeff part:1

enter the exponent part:2

Page 6: Ds All Prgms

1stenter the coeff part:2

enter the exponent part:1

1stenter the coeff part:3

enter the exponent part:0

2ndenter the coeff part:1

enter the exponent part:2

2ndenter the coeff part:2

enter the exponent part:1

2ndenter the coeff part:3

enter the exponent part:0

POLYNOMIAL ADDITION1.display the first equation2.display the second equation3.add the equation4.display the result5.exitenter your choice:1

first equation is:1x^2+2x^1+3x^0+POLYNOMIAL ADDITION1.display the first equation2.display the second equation3.add the equation4.display the result5.exitenter your choice:2

second equation is:1x^2+2x^1+3x^0+POLYNOMIAL ADDITION1.display the first equation

Page 7: Ds All Prgms

2.display the second equation3.add the equation4.display the result5.exitenter your choice:3

POLYNOMIAL ADDITION1.display the first equation2.display the second equation3.add the equation4.display the result5.exitenter your choice:4

result is:2x^2+4x^1+6x^0+POLYNOMIAL ADDITION1.display the first equation2.display the second equation3.add the equation4.display the result5.exitenter your choice:5

RESULT:

Page 8: Ds All Prgms

SINGLY LINKED LIST

CODING:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<malloc.h>

struct list

{

int data;

struct list *next;

};

typedef structlist node;

node*head=NULL;

void display(node*head)

{

node*temp=head;

while(temp!=NULL)

{

printf("\n %d\n",temp->data);

temp=temp->next;

}

}

void addbeg(int no)

{

node*temp;

temp=(node*)malloc(size of (node));

temp->data=no;

Page 9: Ds All Prgms

if(head==NULL)

temp->next=NULL;

else

{

temp->next=head;

head=temp;

}

}

void addend(int no)

{

node*temp*r;

temp=(node*)malloc(size of (node));

temp->data=no;

temp->next=NULL;if(head==NULL)

head=temp;

else

{

r=head;

while(r->next!=NULL)

r=r->next;

r->next=temp;

}

}

void addafter(int no)

{

Page 10: Ds All Prgms

node*temp*r;

int i,loc;

temp=head;

printf("\n Enter position to be added:");

scanf("%d",&loc);

for(i=0;i<loc;i++)

{

temp=temp->next;

if(temp==NULL)

{

printf("empty");

}

}

r=(node*)malloc(size of(node));

r->data=no;

r->next-=temp->next;

temp->next=r;

}

void delbeg()

{

node*temp;

temp=(node*)malloc(size of (node));

temp=head;

if(head==NULL)

printf("empty list: deletion cannot be done");

else

Page 11: Ds All Prgms

head=temp->next;

}

void delend()

{

node*r;

if(head==NULL)

printf("empty list");

else

{

r=head;

while(r->next!=NULL)

r=r->next;

printf("\n The deleted element is %d",r->next=data);

r->next=NULL;

}

}

void delafter(node*head)

{

node*temp,*r=head,*q=NULL;

int i,loc;

printf(“enter the position”);

scanf(“%d”,&loc);

for(i=0;i<=loc;i++)

{

q=r;

r=r->next;

Page 12: Ds All Prgms

if(r=NULL)

{

printf(“empty”);

}

else

q->next=r->next;

}

}

void create(int no)

{

node*temp,*q;

temp=(node*)malloc(sizeof(node));

temp->data no;

temp->next=NULL;

if(head==NULL)

head =temp;

else

{

q=head;

while(q->next!=NULL)

q=q->next;

q->next=temp;

}

}

Page 13: Ds All Prgms

void main()

{

int choice,no.element;

clrscr();

do

{printf(“singly linked list implementation of list\n”);

printf(“n0.create\n1.insert at front\n2.insert at end\n3.insert at a specified\n4.delete at beginning\n5.deletion at end\n6.deletion at position\n7.display list\n8.exit”);

printf(“\nenter the choice”);

scanf(“%d”,&choice);

switch (choice)

{

case 0:

printf(“enter the element\n”);

scanf(“%d”,&no);

create(no);

break;

case 1:

printf(“enter the element\n”);

ccanf(“%d”,&no);

addbeg(no);

break;

case 2:

printf(“enter the element\n”);

Page 14: Ds All Prgms

scanf(“%d”,&no);

addend(no);

break;

case 3:

printf(“enter the element\n”);

scanf(“%d”,&no);

addafter(no);

break;

case 4:

delbeg(no);

break;

case 5:

delend(no);

break;

case 6:

delafter(no);

break;

case 7:

printf(“\n display”);

display(head);

break;

case 8:

exit(0);

}

}

Page 15: Ds All Prgms

while(1);

getch();

}

OUTPUT:

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

6.delete at position

7.display the list

8.exit

Enter your choice 0

Enter the element 10

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

6.delete at position

Page 16: Ds All Prgms

7.display the list

8.exit

Enter your choice 1

Enter the element 5

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

6.delete at position

7.display the list

8.exit

Enter your choice 2

Enter the element 15

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

Page 17: Ds All Prgms

6.delete at position

7.display the list

8.exit

Enter your choice 3

Enter the element 12

Enter the position to be added 1

singly linked list

0. create

1. insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

6.delete at position

7.display the list

8.exit

Enter your choice 4

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

Page 18: Ds All Prgms

6.delete at position

7.display the list

8.exit

Enter your choice 5

the deleted element is 56

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

6.delete at position

7.display the list

8.exit

Enter your choice 6

Enter the position 0

singly linked list

0.create

1.insert at front

2.insert at end

3.insert at specified

4.delete at beginning

5.deletion at end

Page 19: Ds All Prgms

6.delete at position

7.display the list

Enter your choice7

display

10

INFIX TO POSTFIX CONVERSION

Page 20: Ds All Prgms

CODING:

#include<stdio.h>

#include<conio.h>

#include<string.h>

int top=-1;

char stack[100];

int len;

void inpost(char[]);

int priority(char);

void push(char);

char pop();

void main()

{

char infix[50];

clrscr();

printf("enter the expression");

scanf("%s",infix);

len=strlen(infix);

inpost(infix);

getch();

}

void inpost(char exp[])

{

int i;

Page 21: Ds All Prgms

for(i=0;i<len;i++)

{

if((exp[i]>='a')&&(exp[i]<='z'))

printf("%c",exp[i]);

else if(exp[i]=='(')

push(exp[i]);

else if(exp[i]==')')

{

while (stack[top]!='(')

{

printf("%c",pop());

}

top=top-1;

}

else if(priority(exp[i])>priority(stack[top]))

push(exp[i]);

else if(priority(exp[i])<=priority(stack[top]))

{

while(priority(exp[i])<=priority(stack[top])&&(top!=1))

{

printf("%c",pop());

}

push(exp[i]);

}

Page 22: Ds All Prgms

else

{

printf("\n invalid input");

break;

}

}

while((top!=-1)&&(stack[top]!='('))

printf("%c",pop());

}

int priority(char ele)

{

switch(ele)

{

case'^':return(3);

case'*':return(2);

case'/':return(2);

case'+':return(1);

default:return(0);

}

}

void push(char in)

{

if(top==(len-1))

printf("\n stack is full");

Page 23: Ds All Prgms

else

{

stack[++top]=in;

}

}

char pop()

{

char stacktop;

if(top==-1)

printf("\n stack is empty");

else

{

stacktop=stack[top];

top--;

}

return(stacktop);

}

OUTPUT:

Enter the expression

((a*b)=(c*d))

ab*cd*+

ARRAY IMPLEMENTATION OF QUEUE

Page 24: Ds All Prgms

CODING:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

# define max 20

int s[max],front=-1,rear=-1;

void insert(int k)

{

if(rear==(max-1))

{

printf("\n queue is full");

}

else

{

rear+=1;

s[rear]=k;

if(front==-1)

front+=1;

}

}

void delet()

{

int del;

if(front==-1)

printf("\n queue is empty");

Page 25: Ds All Prgms

else

{

del=front;

printf("%d",del);

s[front]=0;

if(front==rear)

front=rear=-1;

else

front+=1;

}

}

void display()

{

int i;

if(front==-1)

printf("\n queue is empty");

else

{

for(i=front;i<=rear;i++)

printf("\n%d",s[i]);

}

}

void main()

{

int n,p;

Page 26: Ds All Prgms

clrscr();

while(1)

{

printf("\nARRAY IMPLEMENTATION OF QUEUE\n");

printf("\n1.to insert an element in the queue\n 2.to delete an element in the queue\n 3.display the elments in the queue\n 4.exit");

printf("enter your choice");

scanf("%d",&n);

switch(n)

{

case 1:

printf("\n enter the elements to be inserted in the queue");

scanf("%d",&p);

insert(p);

break;

case 2:

delet();

break;

case 3:

printf("\n the contents in the queue are");

display();

break;

case 4:

exit(0);

}

Page 27: Ds All Prgms

}

getch();

}

OUTPUT:

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exit

enter your choice1

enter the elements to be inserted in the queue10

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exit

enter your choice1

enter the elements to be inserted in the queue20

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

Page 28: Ds All Prgms

4.exit

enter your choice1

enter the elements to be inserted in the queue30

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exit

enter your choice2

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exit

enter your choice3

the contents in the queue are

20

30

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

Page 29: Ds All Prgms

3.display the elments in the queue

4.exitenter your choice2

1

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exitenter your choice2

2

ARRAY IMPLEMENTATION OF QUEUE

1.to insert an element in the queue

2.to delete an element in the queue

3.display the elments in the queue

4.exitenter your choice3

the contents in the queue are

queue is empty

STACK IMPLEMENTATION USING LINKED LIST

Page 30: Ds All Prgms

CODING:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<malloc.h>

struct stack

{

int data;

struct stack *next;

};

typedef struct stack node;

node *top=NULL;

void display(node *top)

{

node *temp=top;

while(temp!=NULL)

{

printf("\n%d\n",temp->data);

temp=temp->next;

}

}

void push(int no)

{

node *temp;

temp=(node*)malloc(sizeof(node));

temp->data=no;

Page 31: Ds All Prgms

temp->next=top;

top=temp;

}

void pop()

{

node *temp;

int no;

if(top==NULL)

printf("\n stack is empty");

temp=top;

no=temp->data;

printf("%d",no);

top=top->next;

free(temp);

}

void main()

{

int ch,no;

clrscr();

do

{

printf("\nLINKED LIST IMPLEMENTATION OF STACK");

printf("\n 1.push in to the stack\n 2.pop from the stack \n 3.dispaly stack\n 4.exit");

printf("\n enter your choice");

scanf("%d",&ch);

switch(ch)

Page 32: Ds All Prgms

{

case 1:

printf("enter the element to be pushed into the stack");

scanf("%d",&no);

push(no);

break;

case 2:

printf("the poped element is:");

pop();

break;

case 3: printf("elements in the stack are:");

display(top);

break;

case 4:

exit(0);

}

}

while(1);

getch();

}

OUTPUT:

LINKED LIST IMPLEMENTATION OF STACK

1.push in to the stack

2.pop from the stack

3.dispaly stack

Page 33: Ds All Prgms

4.exit

enter your choice1

enter the element to be pushed into the stack10

LINKED LIST IMPLEMENTATION OF STACK

1.push in to the stack

2.pop from the stack

3.dispaly stack

4.exit

enter your choice1

enter the element to be pushed into the stack20

LINKED LIST IMPLEMENTATION OF STACK

1.push in to the stack

2.pop from the stack

3.dispaly stack

4.exit

enter your choice1

enter the element to be pushed into the stack30

LINKED LIST IMPLEMENTATION OF STACK

1.push in to the stack

2.pop from the stack

3.dispaly stack

4.exit

enter your choice3

Page 34: Ds All Prgms

elements in the stack are:

30

20

10

LINKED LIST IMPLEMENTATION OF STACK

1.push in to the stack

2.pop from the stack

3.dispaly stack

4.exit

enter your choice2

the poped element is:30

DOUBLY LINKEDLIST

Page 35: Ds All Prgms

CODING:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<ctype.h>

#define null 0

struct info

{

int data;

struct info*next;

struct info*prev;

};

struct info*head,*temp,*disp;

void create();

void display();

void addatbeg();

void addafter();

void addatend();

void delatbeg();

void delinbet();

void search();

void create()

{

struct info *q;

q=(struct info *)malloc(sizeof(struct info));

Page 36: Ds All Prgms

printf("enter the data");

scanf("%d",q->data);

if(head==NULL)

{

head=q;

q->next=NULL;

q->prev=NULL;

temp=q;

}

else

{

temp->next=q;

q->prev=temp;

q->next=NULL;

temp=q;

}

}

void display()

{

struct info *disp;

if(head==NULL)

{

printf("list is empty\n");

return;

}

Page 37: Ds All Prgms

else

printf("the elements are\n");

printf("from forward direction");

for(disp=head;disp!=NULL;disp=disp->next)

{

printf("%d->\t",disp->data);

}

printf("\n from backward direction");

for(disp=temp;disp!=NULL;disp=disp->prev)

{

printf("%d->\t",disp->data);

}

}

void addatbeg()

{

struct info *q;

q=(struct info *)malloc(sizeof(struct info));

printf("enter the inserted");

scanf("%d",q->data);

if(head!=NULL)

{

q->next=head;

head->prev=q;

q->prev=NULL;

head=q;

Page 38: Ds All Prgms

}

}

void addafter()

{

int i,pos;

struct info *s,*temp1;

s=(struct info *)malloc(sizeof(struct info));

printf("enter the data to be insreted");

scanf("%d",&s->data);

printf("enter the position to be inserted");

scanf("%d",&pos);

temp1=head;

for(i=0;i<pos;i++)

{

temp1=temp1->next;

}

s->prev=temp1;

s->next=temp1->next;

temp1->next->prev=s;

temp1->next=s;

}

void addatend()

{

struct info *q,*temp;

q=(struct info *)malloc(sizeof(struct info));

Page 39: Ds All Prgms

printf("enter the element to be inserted");

scanf("%d",q->data);

if(head==NULL)

{

printf("linked list is empty\n");

printf("insertion is not possible");

return;

}

temp=head;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=q;

q->prev=temp;

q->next=NULL;

return;

}

void delatbeg()

{

struct info *p;

p=head;

if(head==NULL)

{

printf("empty list:deletion cannot be done");

Page 40: Ds All Prgms

return;

}

head=head->next;

head->prev=NULL;

free(p);

}

void delinbet()

{

struct info*temp,*temp1;

int pos,c=1;

if(head->next==NULL)

{

printf("no items in the list");

return;

}

else

{

temp=head;

printf("enter the position to be deleted");

scanf("%d",&pos);

while(c<pos)

{

temp1=temp;

temp=temp->next;

c++;

Page 41: Ds All Prgms

}

{

temp1->next=temp->next;

temp->next->prev=temp1;

free(temp);

}

}

}

void delatend()

{

struct info *a;

a=temp;

temp->prev->next=NULL;

temp=temp->prev;

free(a);

}

void search()

{

int tdata,flag=0;

if(head==NULL)

{

printf("\n no.of items in the list");

return;

}

printf("enter the element to be searched\n");

Page 42: Ds All Prgms

scanf("%d",&tdata);

for(disp=head;(disp!=null)&&(flag==0);disp=disp->next)

{

if(disp->data==tdata)

flag=1;

if(flag==0)

printf("elements not found in the list\n");

else

printf("elements found in the list\n");

}

}

void main()

{

int ch,m;

clrscr();

while(1)

{

printf("\ndoubly linked list\n");

printf("\n 1.create\n 2.display \n 3.addatbeg\n 4.addafter\n 5.addend\n 6.delatbeg\n 7.delinbet\n 8.delatend\n 9.serach\n 10.exit");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

create();

Page 43: Ds All Prgms

break;

case 2:

display();

break;

case 3:

addatbeg();

break;

case 4:

addafter();

break;

case 5:

addatend();

break;

case 6:

delatbeg();

break;

case 7:

delinbet();

break;

case 8:

delatend();

break;

case 9:

search();

break;

Page 44: Ds All Prgms

case 10:

exit(0);

}

}

}

OUTPUT:

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice1

enter the data10

DOUBLY LINKED LIST

1.create

Page 45: Ds All Prgms

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice2

the elements are

from forward direction10->

from backward direction10->

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice3

enter the inserted20

Page 46: Ds All Prgms

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice 4

enter the data to be insreted30

enter the position to be inserted2

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

Page 47: Ds All Prgms

10.exitenter your choice5

enter the element to be inserted40

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice6

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

Page 48: Ds All Prgms

8.delatend

9.serach

10.exitenter your choice7

enter the position to be deleted2

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

8.delatend

9.serach

10.exitenter your choice8

DOUBLY LINKED LIST

1.create

2.display

3.addatbeg

4.addafter

5.addend

6.delatbeg

7.delinbet

Page 49: Ds All Prgms

8.delatend

9.serach

10.exitenter your choice9

enter the element to be searched

30

elements not found in the list

elements not found in the list

elements not found in the list

DOUBLE ENDED QUEUE

Page 50: Ds All Prgms

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define size 5

struct queue

{

int queue [size];

int front,rear;

}q;

qfull()

{

if(q.rear==size-1)

return 1;

else

return 0;

}

int qempty()

{

if((q.front>q.rear)||(q.front==-1&&q.rear==-1))

return 1;

else

return 0;

}

int insert_rear(int item)

{

if(q.front==-1&&q.rear==-1)

q.front ++;

q.queue[++q.rear]=item;

return q.rear;

}

int delete_front()

{

int item;

if(q.front==-1)

q.front++;

item=q.queue[q.front];

q.queue[q.front]=-1;

q.front++;

return item;

}

Page 51: Ds All Prgms

int insert_front(int item)

{

int i,j;

if(q.front==-1)

q.front++;

i=q.front-1;

while(i>=0)

{

q.queue[i+1]=q.queue[i];

i--;

}

j=q.rear;

while(j>=q.front)

{

q.queue[j+1]=q.queue[j];

j--;

}

q.rear++;

q.queue[q.front]=item;

return q.front;

}

int delete_rear()

{

int item;

item=q.queue[q.rear];

q.queue[q.rear]=-1;

q.rear--;

return item;

}

void display()

{

int i;

for(i=q.front;i<=q.rear;i++)

printf("%d",q.queue[i]);

}

void main()

{

int choice,item,i;

char ans='y';

q.front=-1;

q.rear=-1;

Page 52: Ds All Prgms

for(i=0;i<size;i++)

q.queue[i]=-1;

printf("\n program for double ended queue");

clrscr();

do

{

printf("\n 1. insert by rear \n 2.del by frnt \n 3. insert betw frnt \n 4. del by rear \n 5. display \n 6.quit");

printf("\n enter ur choice ");

scanf("%d",&choice);

switch(choice)

{

case 1:if(qfull())

printf("\n full");

else

{

printf("\n entr the elements to be inserted ");

scanf("%d",&item);

q.rear=insert_rear(item);

}

break;

case 2:if(qempty())

printf("\n empty");

else

{

item=delete_front();

printf("\n the item deleted is %d",item);

}

break;

case 3:

if(qfull())

printf("\n full");

{

printf("\n entr the elements to be inserted ");

scanf("%d",&item);

q.front=insert_front(item);

}

break;

case 4:

if(qempty())

printf("\n empty");

else

Page 53: Ds All Prgms

{

item=delete_rear();

printf("\n the item deleted is %d",item);

}

break;

case 5:

display();

break;

case 6:

exit(0);

}

}

while(ans=='y'||ans=='y');

getch();

}

OUTPUT:

1. insert by rear

2.del by frnt

3. insert betw frnt

4. del by rear

5. display

6.quit

enter ur choice 1

entr the elements to be inserted 20

1. insert by rear

2.del by frnt

3. insert betw frnt

4. del by rear

5. display

6.quit

enter ur choice 4

the item deleted is 20

1. insert by rear

2.del by frnt

3. insert betw frnt

4. del by rear

5. display

6.quit

enter ur choice 3

entr the elements to be inserted 52

1. insert by rear

Page 54: Ds All Prgms

2.del by frnt

3. insert betw frnt

4. del by rear

5. display

6.quit

enter ur choice 2

the item deleted is 52

1. insert by rear

2.del by frnt

3. insert betw frnt

4. del by rear

5. display

6.quit

enter ur choice 6

Page 55: Ds All Prgms

PRIM’S ALGORITHM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int edge[10][2];

void main()

{

int prims(int cost[10][10],int n);

int i,j,k,n,cost[10][10];

int totcost=0;

clrscr();

printf("\n enter the no of vertices : ");

scanf("%d",&n);

printf("\n n=%d",n);

printf("\nenter the cost of the matrix \n ");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(i==j)

{

cost[i][j]=0;

}

else

{

printf("\n cost from edge %d to %d \t ",i,j);

scanf("%d",&cost[i][j]);

}

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

printf("%d",cost[i][j]);

printf("\n");

}

totcost=prims(cost,n);

printf("\n total cost of minimum sapnning tree is %d",totcost);

getch();

}

int prims(int cost[10][10],int n)

{

int closest[10],lowcost[10],min,i,j,k,totcost=0;

for(i=2;i<=n;++i)

Page 56: Ds All Prgms

{

lowcost[i]=cost[1][i];

closest[i]=1;

}

for(i=2;i<=n;++i)

{

min=1000;

k=2;

for(j=2;j<=n;++j)

if(lowcost[j]<min&&lowcost[j]!=0)

{

min=lowcost[j];

k=j;

}

if(min==1000)

{

printf("\n the graph is not connected ");

exit(0);

}

edge[i-1][1]=closest[k];

edge[i-1][2]=k;

printf("\n %d -> %d", edge[i-1][1],edge[i-1][2]);

totcost+=cost[edge[i-1][1]][edge[i-1][2]];

lowcost[k]=0;

for(j=2;j<=n;+++j)

{

if(cost[k][j]<lowcost[j])

if(lowcost[j]>0)

{

lowcost[j]=cost[k][j];

closest[j]=k;

}

}

}

return(totcost);

}

OUTPUT:

enter the no of vertices : 3

n=3

enter the cost of the matrix

cost from edge 1 to 2 1

Page 57: Ds All Prgms

cost from edge 1 to 3 2

cost from edge 2 to 1 3

cost from edge 2 to 3 4

cost from edge 3 to 1 5

cost from edge 3 to 2 6

012

304

560

1 -> 2

1 -> 3

total cost of minimum sapnning tree is 3

PRIORITY QUEUE USING HEAPS

Page 58: Ds All Prgms

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#include<process.h>

struct heapstruct

{int capacity;

int size;

int *a;

};typedef struct heapstruct *pq;

pq initialize(int max,int min)

{pq h;

if(max<min)

printf("\n priority queue is small");

h=(struct heapstruct*)malloc(sizeof(struct heapstruct));

if(h==NULL)

printf("\n out of space");

h->capacity=max;

h->size=0;

h->a[0]=min;

return h;

}void insert(int x,pq h)

{int i;

if(h->size==h->capacity)

{printf("\n priority queue is full");

}else

for(i=++h->size;h->a[i/2]>x;i/=2)

{h->a[i]=h->a[i/2];

}h->a[i]=x;

}int delmin(pq h)

{int i,mindata,last,a,child;

if(h->size==0)

{printf("\n priority queue is empty");

return (h->a[0]);

}mindata=h->a[i];

last=h->a[h->size--];

for(i=1;i*2<=h->size;i=child)

{child=i*2;

if(child!=h->size && h->a[child+1]<h->a[child])

child++;

if(last>h->a[child])

Page 59: Ds All Prgms

{h->a[i]=h->a[child];

}else break;

}h->a[i]=last;

return mindata;

}void display(pq h)

{int i;

for(i=1;i<=h->size;i++)

{printf("\n the data is %d",h->a[i]);

}}void main()

{pq h;

int x,y,z,u,v;

char ch;

clrscr();

printf("enter the max no of element in the priority queue");

scanf("%d",&x);

printf("enter the min no of element in the priority queue");

scanf("%d",&y);

h=initialize(x,y);

menu:

printf("\npriority queue");

printf("\n 1.insert\n 2.delete\n 3.dispaly\n 4.exit\n");

printf("enter your choice");

scanf("%d",&u);

switch(u)

{case 1:

printf("enter the data\t");

scanf("%d",&z);

insert(z,h);

break;

case 2:

printf("\nthe deleted element is %d\n",z);

break;

case 3: display(h);

break;

case 4: exit(0); }goto menu; }

OUTPUT:

enter the max no of element in the priority queue3

enter the min no of element in the priority queue2

priority queue

1.insert

Page 60: Ds All Prgms

2.delete

3.dispaly

4.exit

enter your choice1

enter the data 10

priority queue

1.insert

2.delete

3.dispaly

4.exit

Enter your choice1

enter the data 20

1.insert

2.delete

3.dispaly

4.exit

enter your choice1

enter the data 30

priority queue

1.insert

2.delete

3.dispaly

4.exit

enter your choice2

the deleted element is 30

priority queue

1.insert

2.delete

3.dispaly

4.exit

enter your choice3

the data is 10

the data is 20

Page 61: Ds All Prgms

IMPLEMENTATION OF HASHING USING LINEAR PROBING

P ROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 10void display(int a[]);void main(){

int a[MAX],num,key,i;char ans;int create(int);void linear_prob(int [],int,int),display(int []);clrscr();printf("\n collision handling by linear probing");for(i=0;i<MAX;i++)

a[i]= -1;do{printf("\n enter the number");

scanf("%d",&num);key=create(num); linear_prob(a,key,num); printf("\n Do u wish to continue?(y/n)");ans=getche();

}while(ans=='y');display(a); getch();

}int create(int num){int key;

key=num%10;return key;

}void linear_prob(int a[MAX],int key,int num){

int flag,i,count=0;//void display(int a[]);flag=0;if(a[key]==-1)

a[key]=num; else{

i=0;while(i<MAX){

if(a[i]!= -1)count++;

i++;}if(count==MAX) {

Page 62: Ds All Prgms

printf("\nhash table is full");display(a);getch();exit(1);

}for(i=key+1;i<MAX;i++)

if(a[i]== -1) {

a[i]=num; flag=1;break;

}for(i=0;i<key&&flag==0;i++)

if(a[i]== -1){

a[i]=num;flag=1;break;

}}

}void display(int a[MAX]){

int i;printf("\n the hash table is...\n");for(i=0;i<MAX;i++)

printf("\n %d %d",i,a[i]);}

Output:Collision handling by linear probingEnter the number 0

Do u wish to continue?(y/n)yenter the number 2

Do u wish to continue?(y/n)yEnter the number 1

Do u wish to continue?(y/n)yEnter the number 8

Do u wish to continue?(y/n)yEnter the number 3

Do u wish to continue?(y/n)yEnter the number 4

Do u wish to continue?(y/n)yEnter the number 5

Page 63: Ds All Prgms

Do u wish to continue?(y/n)yEnter the number 9

Do u wish to continue?(y/n)yEnter the number 6

Do u wish to continue?(y/n)yEnter the number 7

Do u wish to continue?(y/n)yEnter the number 11

Hash table is fullThe hash table is...

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

INSERTION IN AVL TREE

Page 64: Ds All Prgms

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

typedef struct node st;

struct node

{

char name[20];

int lr,index;

st *left,*right,*par;

}*head,*temp;

void add();

int check(st*);

void indexing();

void rotation (int, int);

void display();

st *stk[30];

int top=-1;

empty();

void push(st*);

st* pop() ;

void main(void)

{

int i,j,n;

char ch,name[20];

clrscr();

head=(st*)malloc(sizeof(st));

head->left=NULL;

head->right=NULL;

head->par=NULL;

head->index=0;

printf("\n\tenter the number of nodes to be inserted: ");

scanf("%d",&n);

printf("\n\n\tEnter the nodes :: \n\n\t\t");

scanf("%s",head->name);

for(i=1 ;i<n;i++)

{

temp=(st*)malloc(sizeof(st));

temp->left=NULL;

temp->right=NULL;

temp->par=NULL;

temp->index=0;

printf("\n\t\t");

scanf("%s",temp->name);

add();

indexing();

Page 65: Ds All Prgms

}

display();

getch();

}

void add()

{

int f1;

st *tp,*tp1;

tp=head;

while(tp)

{

tp1=tp;

f1=check(tp);

if(f1==1)

tp=tp->right;

else

tp=tp->left;

}

if(f1==1)

{

tp1->right=temp;

temp->par=tp1;

temp->lr=-1;

}

else

{

tp1->left=temp;

temp->par=tp1;

temp->lr=1;

}

}

check(st *tp)

{

int flag,i;

for(i=0;tp->name[i]!='\0';i++)

{

if(tp->name[i]<temp->name[i])

{

flag=1;

break;

}

if(tp->name[i]>temp->name[i])

{

flag=0;

break;

}

Page 66: Ds All Prgms

}

if(tp->name[i]=='\0')

if(temp->name[i]!='\0')

flag=1;

return flag;

}

void indexing()

{

int path1=0,path2;

while(temp->par)

{

temp->par->index+=temp->lr;

path2=path1;

if(temp->lr==1)

path1=1;

if(temp->lr==-1)

path1=0;

temp=temp->par;

if(temp->index<-1||temp->index>1)

{

rotation(path1,path2);

break;

}

if(temp->index==0)

break;

}}

void rotation(int path1,int path2)

{

void LL();

void RR();

if(path1==1)

if(path2==1)

LL();

else

RR();

}

void RR()

{

st *hd,*m,*mr;

int f;

hd=temp;

m=hd->right;

mr=m->left;

hd->right=mr;

mr->par=hd;

mr->lr=-1;

Page 67: Ds All Prgms

m->left=hd;

if(hd->par)

{

f=hd->lr;

hd=hd->par;

if(f==1)

hd->left=m;

else

hd->right=m;

m->lr=f;

m->par=hd;

}

else

{

head=m;

m->par=NULL;

}

hd=m->left;

hd->par=m;

hd->lr=1;

m->index=0;

hd->index=0;

}

void LL()

{

st *hd,*m,*mr;

int f;

hd=temp;

m=hd->left;

mr=m->right;

hd->left=mr;

mr->par=hd;

mr->lr=1;

m->right=hd;

if(hd->par)

{

f=hd->lr;

hd=hd->par;

if(f==1)

hd->left=m;

else

hd->right=m;

m->lr=f;

m->par=hd;

}

else

Page 68: Ds All Prgms

{

head=m;

m->par=NULL;

}

hd=m->right;

hd->par=m;

hd->lr=-1;

m->index=0;

hd->index=0;

}

void display()

{

void inorder();

printf("\n\n\t\t AVL tree is => ");

printf("\n\n\tInorder display :: \n\t");

inorder();

}

void inorder()

{

temp=head;

while(temp||empty())

{

while(temp)

{

push(temp);

temp=temp->left;}

temp=pop();

printf("%s",temp->name);

temp=temp->right;

}

}

}

void push(st *node)

{

if(top==30)

printf("\n\tStack is full ");

else

{

top++;

stk[top]=node;

}

}

st* pop()

{

st *node;

if(!empty())

Page 69: Ds All Prgms

node=NULL;

else

{

node=stk[top];

top--;

}

return(node);

}

empty()

{

if(top==-1)

return(0);

else

return(1);

}

OUTPUT: Enter the number of nodes to be inserted:

Enter the nodes:5 3 9 6

AVL tree is =>

Inorder display:

3 5 6 9

BINARY SEARCH TREE

Page 70: Ds All Prgms

CODING:#include<stdio.h>#include<conio.h>#include<malloc.h>#include<stdlib.h>struct treenode;typedef struct treenode *position;typedef struct treenode *searchtree;typedef struct treenode ptrnode;typedef int elementtype;searchtree makeempty(searchtree t);position find(elementtype x,searchtree t);position findmin(searchtree t);position findmax(searchtree t);searchtree insert(elementtype x,searchtree t);searchtree delet(elementtype x,searchtree t);elementtype retrive(position p);struct treenode{elementtype element;searchtree left;searchtree right;};searchtree makeempty(searchtree t){if(t!=NULL){makeempty(t->left);makeempty(t->right);free(t);}return NULL;}searchtree insert(elementtype x,searchtree t){if(t==NULL){t=(ptrnode *)malloc(sizeof(struct treenode));if(t==NULL)printf("\n Out of space");else{t->element=x;

Page 71: Ds All Prgms

t->left=NULL;t->right=NULL;}}else if(x<t->element){t->left=insert(x,t->left);}else if(x>t->element){t->right=insert(x,t->right);}return t;}searchtree delet(elementtype x,searchtree t){position tmp;if(t==NULL){}else if(x<t->element){t->left=delet(x,t->left);}else if(x>t->element){t->right=delet(x,t->right);}else{if(t->right && t->left){tmp=findmin(t->right);t->element=tmp->element;t->right=delet(tmp->element,t->right);}else{tmp=t;if(t->left==NULL)t=t->right;if(t->right==NULL)t=t->left;free(tmp);

Page 72: Ds All Prgms

}}return t;}position find(elementtype x, searchtree t){if(t==NULL)return NULL;else if(x < t->element){return find(x,t->left);}else if(x > t->element){return find(x,t->right);}elsereturn t;}position findmin(searchtree t){if(t==NULL)return NULL;else if(t->left==NULL)return t;elsereturn findmin(t->left);}position findmax(searchtree t){if(t==NULL)return NULL;else if(t->right==NULL)return t;elsereturn findmax(t->right);}void inorder(searchtree t){if(t!=NULL){inorder(t->left);printf("\n%d",t->element);inorder(t->right);

Page 73: Ds All Prgms

}}void main(){int opt,item;searchtree t;position p;t=NULL;clrscr();do{printf("\n MENU \n\n1.Insert\n2.Delete\n3.search\n4.Display(inorder traversal)\n5.Findmin \n6.Findmax\n7.Exit\n");printf("\n Enter choice:");scanf("%d",&opt);switch(opt){case 1:printf("\n Enter the item to be inserted:");scanf("%d",&item);t=insert(item,t);printf("\n Item is inserted");break;case 2:printf("\n Enter the item to be deleted:");scanf("%d",&item);if(find(item,t)){t=delet(item,t);printf("\n Itemk is deleted");}elseprintf("\n Item is not found");break;case 3:printf("\n Enter the item to be found:");scanf("%d",&item);p=find(item,t);if(p)printf("\n Item is found");elseprintf("\n Item is not found");break;case 4:

Page 74: Ds All Prgms

if(t){printf("Binary searchtree is\n\n");inorder(t);}elseprintf("\nBinsary searchtree empty");break;case 5:p=findmin(t);printf("\n The min element in the tree %d",p->element);break;case 6:p=findmax(t);printf("\n The max element in the tree %d",p->element);break;case 7:exit(1);}}while(opt<=7);}

OUTPUT:MENU1.Insert2.Delete3.Search4.Display(inorder traversal)5.Findmin6.Findmax7.ExitEnter choice:1Enter the item to be inserted:10Item is inserted

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.Exit

Page 75: Ds All Prgms

Enter choice:1 Enter the item to be inserted:20Item is inserted

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.Exit

Enter choice:1 Enter the item to be inserted:607.ExitEnter choice:4Binary searchtree is102060

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.ExitEnter choice:3Enter the item to be found:20Item is found

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.Exit

Enter choice:3

Page 76: Ds All Prgms

Enter the item to be found:90 Item is not found

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.Exit

Enter choice:5The min element in the tree 10MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.Exit

Enter choice:6The max element in the tree 60

MENU1.Insert2.Delete3.search4.Display(inorder traversal)5.Findmin6.Findmax7.ExitEnter choice:2Enter the item to be deleted:20Item is deleted

Page 77: Ds All Prgms

TREE TRAVERSAL IN AN EXPRESSION TREE

CODING:

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#include<ctype.h>

#define size 20

typedef struct node

{

char data;

struct node *left;

struct node *right;

}btree;

btree *stack[size];

int top;

void main()

{

btree *root;

char exp[80];

btree *create(char exp[]);

void inorder(btree *root);

void postorder(btree *root);

void preorder(btree *root);

clrscr();

printf("\n Enter the postfix expression\n");

scanf("%s",exp);

top=-1;

root=create(exp);

printf("\n The tree is created\n");

Page 78: Ds All Prgms

printf("\n The inorder tree traversal is\n");

inorder(root);

printf("\n The preorder tree traversal is\n");

preorder(root);

printf("\n The postorder traversal is\n");

postorder(root);

getch();

}

btree *create(char exp[])

{

btree *temp;

int pos;

char ch;

void push(btree*);

btree *pop();

pos=0;

ch=exp[pos];

while(ch!='\0')

{

temp=(btree *)malloc(sizeof(btree));

temp->left=temp->right=NULL;

temp->data=ch;

if(isalpha(ch))

push(temp);

else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')

{

temp->right=pop();

temp->left=pop();

push(temp);

Page 79: Ds All Prgms

}

else

printf("\n Invalid character");

pos++;

ch=exp[pos];

}

temp=pop();

return (temp);

}

void push(btree *node)

{

if(top +1 >= size)

printf("\n error ,Stack is full\n");

top++;

stack[top]=node;

}

btree *pop()

{

btree *node;

if(top==-1)

printf("Stack is empty\n");

node=stack[top];

top--;

return (node);

}

void inorder(btree *root)

{

btree *temp;

temp=root;

Page 80: Ds All Prgms

if(temp!=NULL)

{

inorder(temp->left);

printf("%c",temp->data);

inorder(temp->right);

}

}

void postorder(btree *root)

{

btree *temp;

temp=root;

if(temp!=NULL)

{

postorder(temp->left);

postorder(temp->right);

printf("%c",temp->data);

}

}

void preorder(btree *root)

{

btree *temp;

temp=root;

if(temp!=NULL)

{

printf("%c",temp->data);

preorder(temp->left);

preorder(temp->right);

}

}

Page 81: Ds All Prgms

OUTPUT:

Enter the postfix expression

a b + c *

The tree is created..

The inorder tree traversal is…

a + b * c

The preorder traversal is…

* + a b c

The post order tree traversal is…

a b + c *

Page 82: Ds All Prgms

CIRCULAR QUEUE

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#include<stdlib.h>

int front=-1,rear=-1;

void enqueue(int item,int size,int cq[])

{if(front==(rear+1)%size)

printf("\nCircular Queue is full");

else

{if(front==-1&&rear==-1)

{front=(front+1)%size;

rear=(rear+1)%size;

cq[rear]=item;

}else

{rear=(rear+1)%size;

cq[rear]=item;

}}}int dequeue(int size,int cq[])

{int x;

if(front==-1&&rear==-1)

printf("\nCircular Queue is Empty");

else

x=cq[front];

if(front==rear)

front=rear=-1;

else

front=(front+1)%size;

return x;

}void display(int size,int cq[])

{if(front==-1&&rear==-1)

printf("\nQueue is Empty");

else

{int i=front;

printf("\nThe Circular Queue is");

while(i!=rear)

{printf("%d->",cq[i]);

i=(i+1)%size;

Page 83: Ds All Prgms

}printf("%d",cq[i]);

}}void main()

{int cq[20];

int ch,qsize,items;

printf("\nEnter the size of Circular Queue:");

scanf("%d",&qsize);

printf("\n\t\tMENU");

printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");

do

{printf("\nEnter Your Choice:");

scanf("%d",&ch);

switch(ch)

{case 1:printf("\nEnter Item to be Inserted:");

scanf("%d",&items);

enqueue(items,qsize,cq);

printf("\nItem is Inserted at rear");

break;

case 2:printf("\nThe Deleted Item is %d",dequeue(qsize,cq));

break;

case 3:printf("\nThe Circular Queue is");

display(qsize,cq);

break;

case 4:exit(1);

break;

}}while(1);

}

OUTPUT:

Enter the size of Circular Queue:3

MENU

1.Insert

2.Delete

3.Display

4.Exit

Enter Your Choice:1

Enter Item to be Inserted:10

Item is Inserted at rear

Enter Your Choice:1

Enter Item to be Inserted:20

Page 84: Ds All Prgms

Item is Inserted at rear

Enter Your Choice:3

The Circular Queue is

The Circular Queue is10->20

Enter Your Choice:2

The Deleted Item is 10

Enter Your Choice:3

The Circular Queue is

The Circular Queue is20

Enter Your Choice:4

Page 85: Ds All Prgms

ARRAY IMPLEMENTATION OF STACK

PROGRAM:

#include<stdio.h>

#include<conio.h>

void push();

void pop();

void display();

int stack[25],top=-1;

void main()

{int choice;

clrscr();

do

{printf("STACK OPERATIONS\n");

printf("\n1. Push Operation\n2. Display Stack\n3. Pop Operation\n4. Exit");

printf("\nEnter the choice: ");

scanf("%d",&choice);

switch(choice)

{case 1:

push();

break;

case 2:

display();

break;

case 3:

pop();

break;

case 4:

break;

}}while(choice<=3);

}void push()

{if(top==24)

printf("Stack Overflow");

else

{int c;

lable:

top++;

printf("Enter data :");

Page 86: Ds All Prgms

scanf("%d",&stack[top]);

printf("Do u want to continue press 1 else press 0: ");

scanf("%d",&c);

if(c==1&&top<24)

goto lable;

}}void display()

{int t=top;

if(top==-1)

printf("Stack is Empty");

else

{for(t=top;t>-1;t--)

printf("\n%d",stack[t]);

}}void pop()

{int x;

if(top==-1)

printf("\nStack is Empty");

else

{x=stack[top];

free(x);

top--;

}}

OUTPUT:

STACK OPERATIONS

1. Push Operation

2. Display Stack

3. Pop Operation

4. Exit

Enter the choice: 1

Enter data :20

Do u want to continue press 1 else press 0: 1

Enter data :30

Do u want to continue press 1 else press 0: 1

Enter data :40

Do u want to continue press 1 else press 0: 0

STACK OPERATIONS

1. Push Operation

2. Display Stack

3. Pop Operation

Page 87: Ds All Prgms

4. Exit

Enter the choice: 2

40

30

20

STACK OPERATIONS

1. Push Operation

2. Display Stack

3. Pop Operation

4. Exit

Enter the choice: 3

STACK OPERATIONS

1. Push Operation

2. Display Stack

3. Pop Operation

4. Exit

Enter the choice: 2

30

20

STACK OPERATIONS

1. Push Operation

2. Display Stack

3. Pop Operation

4. Exit

Enter the choice: 4