You are on page 1of 45

EX.

NO: Program for Stack without using functon
#include<stdio.h> #include<conio.h> #include<process.h> void main() { int s[5],i,n,j,top; clrscr(); top=-1; for(;;) { printf("\nenter your choice"); printf("\n1. for insertion"); printf("\n2. for deletion"); printf("\n3. for view top element"); printf("\n4. for view all element\n"); scanf("%d",&n); switch(n) { case 1: { if(top==4) { printf("stack is full"); } else { printf("Enter the value for insertion"); scanf("%d",&i); top=top+1; s[top]=i; } } break; case 2: { if(top==-1) { printf("stack is empty"); } else {

SIVALINGAM.A 10MCA38

i=s[top]; top=top-1; printf("the removed element is %d",i); } } break; case 3: { if(top==-1) { printf("stack is empty"); } else { i=s[top]; printf("top element is %d",i); } } break; case 4: if(top==-1) { printf("stack is empty"); } else { for(j=0;j<=top;j++) { printf(" %d \n",s[j]); } } break; default: exit(0); } } getch(); }

Output:
Enter your choice 1. For insertion 2. For deletion 3. Foe view top element 4. For view all the element 1 Enter the value for insertion 43

Enter your choice 1 Enter the value for insertion 38 Enter your choice 3 Top element is 38 Enter your choice Elements are 43 38 Enter your choice 2 The removed elements is 38 Enter your choice 3 Top element is 43

EX.NO: Program for Stack using function:
#include<stdio.h> #include<conio.h> void push(int *,int *); void pop(int *,int *); void disp(int *,int *); void main() { int top,st[10]; int s; top=-1; clrscr(); for(;;) { printf("\n1.insert\n"); printf("2.delete\n"); printf("3.display\n"); printf("4,exit\n"); printf("\nEnter your choice:\t"); scanf("%d",&s); switch(s) { case 1: push(&top,&st[0]); break; case 2: pop(&top,&st[0]); break; case 3: disp(&top,&st[0]); break; default: exit(0); break; } } } void push(int *top,int *st) { int x=0; if(*top==9) {

SIVALINGAM.A 10MCA38

int *st) { int x=0.int *st) { if(*top==-1) { printf("stack is under flow\n"). st[*top]=x. } else { printf("\nenter the element for insertion:\t").x).st[*top]). if(*top==-1) { printf("\nstack empty"). } else { printf("\nThe top element is:%d".x). printf("\nThe deleted element is:%d".printf("\nstack is full"). } else { x=st[*top]. } } . printf("\nThe add element is:%d". *top=*top+1. *top=*top-1.&x). } } void pop(int *top. scanf("%d". } } void disp(int *top.

delete 3.Output: 1.delete 3.display 4.exit Enter your choice: 1 The insert element is: 25 The add element is:25 Enter your choice: The top element is:25 Enter your choice: 2 3 The deleted element is:25 Enter your choice 3 The top element is: 85 .exit Enter your choice: 1 The insert element is: 85 The add element is:85 1.insert 2.display 4.insert 2.

} else { f=f+1. } else { r=r+1.) { printf("\nenter your choice").i.h> #include<process. case 2: if(r<=f) { printf("\nthe queue is empty").r.&s).x.h> #include<conio.A 10MCA38 . printf("\npress 3 over flow or not"). scanf("%d". r=-1. f=-1.s. for(.. printf("\npress 2 for deletion"). printf("\npress 5 for exit").h> void main() { int a[10].NO: Program for Queue without function: #include<stdio. printf("\npress 1 for insertion").EX. printf("\npress 4 under flow or not").&x). switch(s) { case 1: if(r==10) { printf("\nthe queue is full"). SIVALINGAM. } break. a[r]=x. scanf("%d". printf("\nEnter the value for insertion"). clrscr().f.

case 3: if(r==10) { printf("the queue is over flow"). } } getch(). } . default: exit(0). } break. } else { printf("the queue is not over flow").x=a[f]. } break. break. } break. case 4: if(f==r) { printf("the queue is under flow"). printf("\nthe removed element is %d ".x). } else { printf("the queue is not under flow").

Output: Enter your choice 1 for insertion 2 for deletion 3 for over flow or not 4 for underflow or not 5 for display 1 Enter the value for insertion45 Enter your choice 1 Enter the value for insertion85 Enter your choice 5 Front element is 45 Rear element is 85 Enter your choice 2 Removed element is 45 Enter your choice 5 Front element is 85 Rear element is 85 .

break.&r). case 4: underflow(&f.f.h> #include<process..NO: Program for Queue using function.h> #include<conio. case 3: overflow(&f. break. void main() { int a[10].&r).s.&s).int *r). printf("\n 1 for insertion ").&f.int *f.&r). scanf("%d".&r). void underflow(int *f. void overflow(int *f.int *r).int *f. clrscr(). void display(int *a.) { printf("\n Enter your choice"). break.int *r). break.&r). for(. switch(s) { case 1: insert(&a[0]. r=f=-1.EX.A 10MCA38 . } SIVALINGAM. printf("\n 2 for deletion"). case 5: display(&a[0]. default: exit(0). void del(int *a.h> void insert(int *a. break. printf("\n 4 for underflow"). break. #include<stdio.int *r). printf("\n 3 for over flow").&f. printf("\n 5 for display\n").r. case 2: del(&a[0].int *r).

int *r) { int x. } else { *r=*r+1.int *f.int *r) { int x. } } void underflow(int *f. if(*r==*f) { printf("The Queue is empty").&x). x=a[*f]. } void insert(int *a. } else { *f=*f+1. printf("Enter the value for insertion").int *r) { if(*r==*f) { printf(" The queue is under flow"). if(*r==10) { printf("The queue is full"). } else { printf(" The queue is not under flow"). scanf("%d". } } void overflow(int *f. } } void del(int *a. printf("Removed element is %d". a[*r]=x.int *r) { if(*r==10) .x).} getch().

} else { printf("\nfront element is %d". } } Output: Enter your choice 1 for insertion 2 for deletion 3 for over flow 4 for underflow 5 for display 1 Enter the value for insertion78 Enter your choice 1 Enter the value for insertion52 Enter your choice 5 front element is 78 Rear element is 52 .a[*f+1]). printf("\nRear element is %d".a[*f]).{ printf("The queue is over flow").int *f.int *r) { if(*f==-1) { printf("\nfront element is %d". } else { printf("The queue is not over flow"). printf("\nRear element is %d".a[*r]). } } void display(int *a.a[*r]).

Enter your choice 2 Removed element is 78 Enter your choice 5 front element is 52 Rear element is 52 .

switch(ch) { case 1: if(((rear+1)%n)!=front) { printf("\nEnter Element: " ).Pop\n3.temp. printf("Enter the size of the Queue:").Push\n2.&ch).n. clrscr(). case 2: if(front==rear) { printf("Queue is empty").&cq[rear]). } . scanf("%d". rear=(rear+1)%n.front=0.&n).h> #include<conio.Exit").ch.rear=0. } else { front=(front+1)%n.h> void main() { int cq[10]. printf("\nYour Element to be Added").EX.A 10MCA38 Program for Circular queue without using function #include<stdio. scanf("%d".cq[front]). printf("The Deleted Element Is : %d". printf("\nEnter Ur Option : ").Display\n4. } break. do { printf("\nCircular Queue Implementation"). printf("\n1. } else { printf("Queue Is Full").i.NO: SIVALINGAM. scanf("%d".

getch(). default: printf("\nPlease Enter Your Choice 1 to 4 "). } . temp=front. while(temp!=rear) { temp=(temp+1)%n. } } break.cq[temp]). printf("\n\t%d". } else { printf("Element In Queue : "). break. } }while(ch!=4). case 4: exit(0). case 3: if(front==rear) { printf("Queue Is Empty").break.

Exit Enter Ur Option : 1 Enter Element: 78 Your Element to be Added Enter Ur Option : 1 Enter Element: 46 Your Element to be Added Enter Ur Option : 1 Enter the element: 85 Your Element to be Added Enter Ur Option : 3 Elements in queue: 78 46 85 Enter Ur Option : 2 The deleted element is: 78 Enter Ur Option : 3 Elements in queue: 45 85 .Pop 3.Display 4.Push 2.Output: Enter the size of the Queue:5 Circular Queue Implementation 1.

clrscr(). break.f).) { Printf(³\n1 for insertion´).h> #include<conio. switch(c) { case 1: r=rear(&a[0].int..A 10MCA38 Program for Circular queue with using function: #include<stdio. } } } int rear(int *a. break.f). f=0. int).EX.NO: SIVALINGAM.f). case 3: disp(&a[0]. int).r.r. break.int r. void disp(int *. case 4: exit(0). for(. if((r+1)==5) { . break.int f) { int x.int.&c). Printf(³\n2 foe deletion´). printf("\nenter your choice\t"). int front(int *. Printf(³\n3 for display´). void main() { int c.h> int rear(int *. r=0. scanf("%d".a[5]. int). case 2: f=front(&a[0].r.f.r.int.

} else { if((f+1)==5) { f=0. } else { printf("insert your element\n"). if(r==f) { printf("queue is empty\n").int f) { int x. scanf("%d". printf("inserted element is:%d\n".x). } } void disp(int *a. } else { r=r+1.int f) { while(f!=r) .r=0. } x=a[f]. } if(r==f) { printf("\nqueue is full\n"). return r.int r. return f.int r. printf("the deleted element is:%d\n".&x). return r. a[r]=x. } else { f=f+1. } } int front(int *a. return f.a[r]).

a[f]). if(f==5) { f=0. } } } Output: 1 for insertion 2 for deletion 3 for display enter your choice 1 insert your element 78 inserted element is:78 enter your choice 1 insert your element 43 inserted element is:43 enter your choice the element:78 the element:43 3 enter your choice 2 the deleted element is :78 enter your choice the element : 43 3 . } else { printf("the element:%d\n"..{ f=f+1.

printf("1 create list\n"). case 2: insert(). void erase(). struct link *next. printf("4 delete list\n").. void display().h> #include<process.*t. case 4: erase(). scanf("%d". printf("3 delete element\n"). switch(ch) { case 1: create(). clrscr(). SIVALINGAM.NO: program for Link list #include<stdio.A 10MCA38 . void del(). break. void main() { int ch. printf("2 insert list\n").h> typedef struct link { int data. for(. }. break.&ch). break.*head=NULL. void create().h> #include<conio. void insert().*x. case 3: del(). printf("5 display element\n"). struct link *newlink.EX.h> #include<malloc.) { printf("\n\nenter your choice\n").

exit(0). break. default: printf("\ninput only numbers 1-5"). } } } void insert() { int i. if(pos==1) { newlink->next=head. case 5: display().t=t->next) t->next=newlink. if(head==NULL) { head=newlink. newlink->next=NULL.&newlink->data).t=t->next.i=1.i++) . printf("enter the position to be inserted\n"). scanf("%d". scanf("%d". } else { for(t=head.break. printf("\nenter the data for insertion\n").pos.&pos).t!=NULL. break. scanf("%d".&newlink->data). head=newlink.t->next!=NULL. } for(t=head. newlink=malloc(sizeof(struct link)). printf("\nenter the data\n"). } } } void create() { { newlink=malloc(sizeof(struct link)).

i++) { if(pos==1) { printf("\nDeleted element is %d".{ if(i==pos-1) { newlink->next=t->next.&pos).t->data). t->next=newlink. t=t->next. free(x). for(t=head. break. printf("\nenter the position to be deleted\n"). } if(i==pos-1) { x=t->next. printf("\nDeleted elememt is %d".t=t->next.t->next->data). } } } void del() { int pos. head=t. break. } } } void erase() { head=NULL. } else { .i. scanf("%d". } void display() { if(head==NULL) { printf("\nthe list is empty\n"). t->next=t->next->next.t!=NULL.i=1. printf("\nAll elements are deleted from the list").

t!=NULL.for(t=head. } } } Output: enter your choice 1 create list 2 insert list 3 delete element 4 delete list 5 display element 1 enter the data 78 enter your choice 2 enter the data for insertion 45 enter the position to be inserted 2 enter your choice 2 enter the data for insertion 85 enter the position to be inserted 1 enter your choice 5 85 78 45 .t=t->next) { printf("\n\t%d".t->data).

enter your choice 3 Enter the position to de deleted 2 Deleted element is 78 enter your choice 5 85 45 .

head= NULL.h> #include<stdlib.*t. printf("enter your choice:"). struct node *link.h" SIVALINGAM. scanf("%d".&f).*t1. }node1. for(. printf("3..) { printf("Give the value for x (*** x=1 for exit ***)\n"). int x=0.f.*head. printf("Give the place for insertion \n"). . clrscr().) { printf("1. switch(f) { case 1: for(.&x).count.EX.h> #include<conio.NO: Program for Singly circular linked list #include<stdio. if(x==1) { break. } n1=(node1*)malloc(sizeof(struct node)).display\n").c. printf("4.exit\n").h> #include "malloc. n1->data =x. void main() { node1 *n1.deletion\n").insertion\n"). printf("2. . scanf("%d".A 10MCA38 typedef struct node { int data.

t->link = n1. for(. } else { t=head.t->link!=head. if(count==1) { printf("First element insertion\n"). t->link=n1. t=head. t1=t.c++) { t=t->link. } n1->link = t->link . } } break. for(c=1.count).c<count-1. head=n1. head=n1. .. if(head==NULL) { n1->link=n1.&count). } n1->data=x.) { printf("get the position for deletion:"). } } else { printf("%d th node insertion\n".) { t=t->link. case 2: for(. n1->link=t1.scanf("%d".

for(.t->data).t=t->link) { printf(" %d \n". t=head. } } break. break. if(x==0) { break.c<x.t->link!=head. } head=t1. for(c=1.c++) { t1=t.) { t=t->link. break. } else if(x==1) { printf("first node deletion\n"). } . case 3: t=head. for(. } else { printf("%d th node deletion\n". t1=t->link. } t1->link=t->link. t->link=head. case 4: exit(0).t->link!=head. } printf("%d\n".&x).x). t=head.scanf("%d".t->data). t=t->link.

insertion 2.} getch(). } Output: 1.exit enter your choice:1 Give the value for insertion 45 Give the place for insertion 1 First element insertion enter your choice:1 Give the value for insertion 78 Give the place for insertion 1 First element insertion enter your choice:3 78 45 Enter your choice: 2 Enter the position for deletion 2 2 th node deletion Enter your choice:3 78 .deletion 3.display 4.

void main() { node1 *n1. scanf("%d". scanf("%d".count.h> #include<stdlib. if(head==NULL) { n1->plink= head.&x).) { printf("\n1. n1->data =x.display\n"). struct node *nlink. printf("enter your choice:").&count). switch(f) { case 1: { printf("Give the value for insertion\n").&f). for(.h> #include<malloc.insertion\n"). scanf("%d". int x=0..NO: Program for Doubly linked list #include<stdio. printf("Give the place for insertion \n").h> #include<conio.*head. struct node *plink.c. n1=(node1*)malloc(sizeof(struct node)).EX. SIVALINGAM.h> typedef struct node { int data.deletion\n"). head=n1. printf("3. clrscr(). }node1. head= NULL.*t1.*t. printf("2.A 10MCA38 . if(count==1) { printf("First element insertion\n").f. n1->nlink= head.

} t->nlink=n1. } n1->plink = t. } } else { printf("%d th node insertion\n". for(c=1. } else if(x==1) { printf("first node deletion\n"). n1->nlink=head. t=head. scanf("%d".count).&x). if(t->nlink!=NULL) { t->nlink->plink = n1.c<count-1. } } break. head=t->nlink. t=head. head->plink=n1. n1->nlink =t->nlink. head=n1. if(x==0) { break.c++) { t=t->nlink. case 2: printf("get the position for deletion:").} else { n1->plink=NULL. head->plink=NULL. .

) { printf("%u\t\t".t->plink).c<x. } t->plink->nlink=t->nlink. printf("%u\n\n".x). if(t->nlink!=NULL) { t->nlink->plink=t->plink. default: exit(0). } break. printf(" %d=%u \t\t".printf("deleted element is %d".t->data).t).c++) { t=t->nlink.t->data. for(.t!=NULL. t=head. } else { printf("%d th node deletion". printf("prevlink\data=address\tnextnlink\n"). break.t->nlink). t=t->nlink. } } break. for(c=1. } } } . case 3: t=head.

deletion 3.display enter your choice:1 Give the value for insertion 48 Give the place for insertion 1 First element insertion enter your choice:1 Give the value for insertion 85 Give the place for insertion 2 2 th node insertion enter your choice:1 give the value for insertion 96 Give the place for insertion 2 2 th node insertion Enter your choice: 3 Preulink data&address 0 48&2144 2144 96&2164 2164 85&2154 Enter your choice: 2 Give the position for deletion:1 First node deletion Deleted element is 48 Enter your choice: 3 Preulink data&address 0 96&2164 2164 85&2154 nextlink 2164 2154 0 nextlink 2154 0 .insertion 2.Output : 1.

}node1.t=t->nlink) { } n1->plink=t->nlink. void firstins() { int x. } else { n1->nlink=head.&x).*t.&x). } } void nthins() { int x. t->nlink=n1. for(t=head.A 10MCA38 .i. head=n1.pos.*nlink. n1=(node1*)malloc(sizeof(struct node)). n1->nlink=n1. SIVALINGAM. printf("Enter a Value:\n"). printf("Enter The Position:\n").&pos). n1->data=x.*head. head->plink=n1.h> #include<conio. n1=(node1*)malloc(sizeof(struct node)).EX. printf("Enter a Value:\n").h> typedef struct node { int data. struct node *plink. scanf("%d". node1 *n1. head=n1. if(head==NULL) { n1->plink=n1.NO: Program for Circular doubly linked list #include<stdio. scanf("%d". scanf("%d".t->nlink!=head.

scanf("%d". } else { t=head. } } void lastins() { int x. (t->plink)->nlink=n1. for(t=head. n1->plink=t. for(i=1.t=t->nlink) { } t->nlink=n1. n1=(node1*)malloc(sizeof(struct node)). } void firstdel() { for(t=head. n1->data=x.n1->data=x.t->nlink!=head. .i++) { t=t->nlink. printf("Enter a Value:\n"). t->plink=n1. } n1->plink=t->plink. for(t=head.&x).t=t->nlink) { } n1->plink=t.i<pos.t=t->nlink) { } t->nlink=head->nlink. head=head->nlink. t->nlink=n1. n1->nlink=head.t->nlink!=head.t->nlink!=head. head->plink=n1. n1->nlink=t. if(pos==1) { n1->nlink=head. head=n1.

Nth Node Deletion:"). printf("\t\n4..head->plink=t. printf("\t\n3. for(. printf("\t\nEnter Ur Choice:"). scanf("%d". } (t->plink)->nlink=t->nlink. } void disp() { for(t=head.i<pos. for(i=1.t=t->nlink) { printf("%p\t%d=%p\t%p\n".Insertion:").&pos).1st Node Deletion:").t. (t->nlink)->plink=t->plink.t->plink.t. head->plink=t->plink.t->data.t->data. .i.t=t->nlink) { } (t->plink)->nlink=head. head=NULL.) { printf("\t\n1. clrscr().create :").i++) { t=t->nlink. } void lastdel() { for(t=head. printf("Enter The Position For Deletion:\n").t->nlink). } void main() { int ch.t->nlink!=head. t=head.t->plink.t->nlink!=head. printf("\t\n2. } printf("%p\t%d=%p\t%p\n".t->nlink). } void nthdel() { int pos.

disp(). case 3: firstdel(). disp(). disp(). break.Insertion: 3. break.1st Node Deletion: 4. switch(ch) { case 1: firstins(). case 4: nthdel(). default: exit(0). case 2: nthins(). break. break.create : 2.Nth Node Deletion: Enter Ur Choice:1 Enter a Value: 48 0814 48=0814 0814 Enter Ur Choice:2 Enter a Value: 49 Enter The Position: 2 081E 48=0814 081E 0814 49=081E 0814 Enter Ur Choice:2 Enter a Value: 96 . disp().&ch). } } } Output : 1.scanf("%d".

Enter The Position: 1 081E 96=0828 0814 0828 48=0814 081E 0814 49=081E 0828 Enter Ur Choice: 4 Enter the position for delete: 3 0814 96=0828 0814 0828 48=0814 0828 Enter Ur Choice: 3 0814 48=0814 0814 .

i. scanf("%d". } a[x+1]=t.NO: Program for Insertion sort #include<stdio.EX.i++) { scanf("%d". for(i=0.x. while(x>=0&&t<a[x]) { a[x+1]=a[x].A 10MCA38 .&a[i]). for(i=0. } for(i=1.h> #include<conio.h> void main() { int a[20]. x=x-1. clrscr().&n). printf(³\nenter the value one by one\n´). x=i-1. printf("enter the limit"). } SIVALINGAM.i<n.a[i]). } getch(). } printf("\n ascending order").t.i++) { t=a[i].i<n.i++) { printf("\n%d".i<n.n.

Output: enter the limit 5 enter the value 65 23 85 12 75 ascending order 12 23 65 75 85 .

a[temp]=t. } getch(). for(i=0. printf("enter the limit\n").h> void main() { int a[10]. } for(i=0.n. for(j=i+1.i++) { printf("\n%d".t.j.&a[i]).a[i]).h> #include<conio.&n). clrscr().i++) { temp=i.i<n. printf("enter the value one by one\n"). a[i]=a[temp]. for(i=0. } SIVALINGAM.j++) { if(a[temp]>a[j]) { temp=j. } } t=a[i].EX. scanf("%d".i.A 10MCA38 .NO: Program for Selection sort: #include<stdio.i++) { scanf("%d". } printf("\n ascending order").temp.j<n.i<n.i<n.

Output: enter the limit 5 enter the value one by one 10 96 26 100 03 ascending order 3 10 26 96 100 .

j<n.j. for(i=0. } for(i=0. printf("enter the limit\n"). } } } printf("\nAscending order\n").j++) { if(a[i]>a[j]) { t=a[i]. a[i]=a[j]. scanf("%d". } getch().n. a[j]=t.&n).i++) { for(j=i+1.t.EX.h> #include<conio.i. printf("enter the value one by one\n").i<n. } SIVALINGAM.h> void main() { int a[10]. for(i=0.A 10MCA38 .NO: Program for Bubble sort: #include<stdio.i<n.a[i]).i++) { scanf("%d".i++) { printf("%d\n". clrscr().&a[i]).i<n.

Output: enter the limit 8 enter the value one by one 45 63 21 96 87 02 10 25 Ascending order 2 10 21 25 45 63 87 96 .

i.q. getch(). int upp) { int j.&a[i]).A 10MCA38 .NO: Program for Quick sort: #include<stdio.upp.low. if(upp>low) { j=split(a.&n). quick(a.upp).i++) printf("%d\n". for(i=0.0.h> #include<conio.h> void quick(int[]. while(a[q]>i) SIVALINGAM.p.j+1. int split(int[]. printf("enter the limit:").int .low).low. } } int split(int a[].a[10]. low=n-1. void main() { int j.i<n. scanf("%d".a[i]).n. int upp) { int i.EX. int low. } void quick(int a[].t.int . q=upp.j-1).int).int low.int low. p=low+1.i++) scanf("%d". while(q>=p) { while(a[p]<i) p++. printf("\nafter sorting:"). printf("enter elements:"). for(i=0.upp).int). i=a[low]. quick(a.i<n. clrscr(). quick(a.

a[q]=t. a[q]=t.q--. a[low]=a[q]. return q. a[p]=a[q]. if(q>p) { t=a[p]. } Output: enter the limit:5 enter elements: 16 25 48 03 96 after sorting: 3 16 25 48 96 . } } t=a[low].