Sei sulla pagina 1di 51

Algorithm

Insertion Procedure ADDQ(item,Q,n,rear) //Insert an element in to the Queue represented it as Q(1:n) If rear = n the call QUEUE_FULL rear = rear + 1 Q(rear)<-item End ADDQ Deletion Procedure DELETEQ(item,Q,front,rear) //Delete an item from the Queue If front = rear the call QUEUE_EMPTY front = front + 1 item<-Q(front) End DELETEQ

PROGRAM NO:9 DATE : AIM : Program to implement queue using linear arrays
#include<stdio.h> #include<conio.h> void main() { int q[10],front=0,rear=0,n,item,c,i; clrscr(); printf("enter the size of the queue\n"); scanf("%d",&n); printf("..............QUEUE OPERATIONS.....................\n"); printf("1.INSERT\n2.DELETE\n3.DISPLAY\n"); do { printf("enter your choice\n"); scanf("%d",&c); switch(c) { case 1:if(rear==n) { printf("QUEUE IS FULL\n"); } else { printf("enter the item\n"); scanf("%d",&item); rear=rear+1; q[rear]=item; } break; case 2:if(front==rear)

{ printf("QUEUE IS EMPTY\n"); } else { front=front+1; item=q[front]; printf("the item was sucessfully deleted from your queue\n"); } break; case 3:for(i=front+1;i<=rear;i++) { printf("%d\t",q[i]); } break; default:printf("the choice is invalid"); break; } } while(c!=3); getch(); }

OUTPUT
Enter the size of the queue:5 ........QUEUE OPERATIONS........ 1.ADD 2.DELETE 3.DISPLAY 4.EXIT Enter your choice: 1 Enter the element to be inserted 34 You have successfully inserted the element 34 to the queue Enter your choice: 1 Enter the element to be inserted 65 You have successfully inserted the element 65 to the queue Enter your choice: 2 You have successfully deleted the element 34 from the queue Enter your choice: 3 The elements in the queue are 65 Enter your choice: 4 Invalid choice

Algorithm
Insertion Procedure ADDQ(item,Q,n,front,rear) //Insert item in the circular Queue stored in Q(0:n-1).rear points to the last item and front is one position counter clockwise from the first item in Queue rear<-(rear+1)mod n if(front==rear) the call QUEUE_FULL Q(rear)<-item End ADDQ

Deletion Procedure DELETEQ(item,Q,n,front,rear) //Removes front element of Queue Q(0:n-1) If(front+1)mod n = rear then call QUEUE_EMPTY front<-(front+1)mod n item<-Q(front) End DELETEQ

PROGRAM NO:10 DATE : AIM : Program to implement circular queue using linear arrays
#include<stdio.h> #include<conio.h> void main() { int q[10],front=0,rear=0,item,n,c,i; clrscr(); printf("Enter the size of circular queue\n"); scanf("%d",&n); printf(".........CIRCULAR QUEUE OPERATION.........\n"); printf("1.Insert\n2.Delete\n3.Display\n"); do { printf("\nEnter your choice\n"); scanf("%d",&c); switch(c) { case 1: rear=(rear+1)%n; if(front==rear) { printf("queue is full\n"); } else { printf("Enter the item to be inserted\n"); scanf("%d",&item); q[rear]=item; } break; case 2: front=(front+1)%n; if(front==rear)

{ printf("\nqueue is empty\n"); } else { item=q[front]; printf("\nItem %d is sucessfully deleted\n",item); } break; case 3: printf("\nThe elements are...\n"); for(i=front+1;i<=rear;i++) { printf("%d\t",q[i]); } break; default:printf("\nInvalid choice\n"); break; } } while(c<4); getch(); }

OUTPUT
Enter the size of circular queue 5 .........CIRCULAR QUEUE OPERATION......... 1.Insert 2.Delete 3.Display

Enter your choice 1 Enter the item to be inserted 45

Enter your choice 1 Enter the item to be inserted 78

Enter your choice 1 Enter the item to be inserted 12

Enter your choice 2

Item 45 is sucessfully deleted

Enter your choice 3

The elements are... 78 12

Enter your choice 4

Invalid choice

PROGRAM NO:11 DATE : AIM : Write a program to implement singly linked list and perform the following operations a) Add a node at the beginning of the list b) Add a node at the end of the list c) Add a node at a particular location. d) Add a n item into a sorted list e) Delete a node from the beginning of the list f) Delete a node with a given data item g) Delete the last node h) Traverse and display the elements present in the list

Algorithm

Concatenate(first,second,&third) //Parameter first and second are passed by value and third is passed by reference (pointer to pointer) to allow it to change during concatenation of two list 1.Repeat through step-6 while(first!=NULL) 2.Create a new node temp 3.Set the data field of temp node as temp->data=first->data 4.Update the value of first as first = first->link 5.if(*third == NULL) then (*third) = temp else current->link = temp 6.current = temp 7.Repeat through step-12 While(second!=NULL) 8.Create a new node temp 9.Set the data field of temp node as temp->data = second->data 10.Update the value of second as second = second->link 11.if(*third == NULL) then (*third) = temp else current->link = temp 12.current = temp 13.Finally assign NULL to the link field of current node 14.Exit

PROGRAM NO:12 DATE : AIM : Program to concatenate two linked list.


#include<stdio.h> #include<conio.h> struct node { int data; struct node *link; }; typedef struct node node; node *getnode(); void main() { node *first,*second,*third; int n,item; clrscr(); printf("\nEnter the number of nodes to create in the firstlist\n"); scanf("%d",&n); first=second=third=NULL; listcreate(&first,n); printf("\nEnter the number of nodes to create in the secondlist\n"); scanf("%d",&n); listcreate(&second,n); concatenate(first,second,&third); printf("\nThe contents of firstlist\n"); display(first); printf("\nThe contents of secondlist\n"); display(second); printf("\nLinkedlist after concatenation...\n"); display(third); getch(); }

listcreate(node **f,int n) { node *temp,*current; int i,item; printf("\n"); for(i=1;i<=n;i++) { printf("\nEnter the data field of node %d",i); scanf("%d",&item); temp=getnode(); temp->data=item; temp->link=NULL; if(*f==NULL) { *f=temp; } else current->link=temp; current=temp; } return; } display(node *first) { printf("\n"); while(first!=NULL) { printf("%d->",first->data); first=first->link; } printf("NULL\n"); return; } concatenate(node *first,node *second,node **third)

{ node *temp,*current; while(first!=NULL) { temp=getnode(); temp->data=first->data; first=first->link; if(*third==NULL) (*third)=temp; else current->link=temp; current=temp; } while(second!=NULL) { temp=getnode(); temp->data=second->data; second=second->link; if(*third==NULL) (*third)=temp; else current->link=temp; current=temp; } current->link=NULL; return; } node *getnode() { node *p; p=(node *)malloc(sizeof(node)); return(p); }

OUTPUT
Enter the number of nodes to create in the firstlist 4

Enter the data field of node 1 11

Enter the data field of node 2 22

Enter the data field of node 3 33

Enter the data field of node 4 44

Enter the number of nodes to create in the secondlist 2

Enter the data field of node 1 55

Enter the data field of node 2 66

The contents of firstlist

11->22->33->44->NULL

The contents of secondlist

55->66->NULL

Linkedlist after concatenation...

11->22->33->44->55->66->NULL

PROGRAM NO:13 DATE : AIM : Perform the following operations on a singly linked list a) Count the total number of nodes b) Reverse the list c) Remove the duplicates from the list d) Split the list into 2 sub lists e) Print the data in the nth node.

#include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }*head=NULL; struct node *temp,*p,*q; void main() { void insertion(int); void deletion(int); void display(); int ch,i,j,k,n; clrscr(); do { printf("\n1.INSERTION AT BEGINING");

printf("\n2.INSERTION AT END"); printf("\n3.INSERTION AT SPECIFIC POSITION"); printf("\n4.INSERTION AT THE SORTED LIST"); printf("\n5.DELETE THE FIRST NODE"); printf("\n6.DETETE A NODE DEPENDING UPON THE DATA"); printf("\n7.DELETE LAST NODE"); printf("\n8.DISPLAY"); printf("\n9.EXIT"); printf("\n Enter your choice"); scanf("%d",&ch); switch(ch) { case 1: k=1; insertion(k); break; case 2: k=2; insertion(k); break; case 3: k=3; insertion(k); break; case 4: k=4; insertion(k); break; case 5: k=5; deletion(k); break; case 6: k=6; deletion(k);

break; case 7: k=7; deletion(k); break; case 8: display(); break; } }while(ch!=9); getch(); } void insertion(int k) { int n,m,i; temp=(struct node*)malloc(sizeof(struct node)); if(k==1) { printf("\nEnter the number to be inserted"); scanf("%d",&n); temp->data=n; if(head==NULL) { head=temp; temp->next=NULL; head=temp; } else { temp->next=head; head=temp; } printf("\n %d Inserted sucessfully",temp->data);

} if(k==2) { printf("\nEnter the number to be inserted"); scanf("%d",&n); temp->data=n; p=head; while(p->next!=NULL) { p=p->next; } p->next=temp; temp->next=NULL; printf("\n %d is sucessfully inserted at the end",temp->data); } if(k==3) { printf("\nEnter the data to be inserted"); scanf("%d",&n); temp->data=n; printf("\nEnter the location where you want to insert the node"); scanf("%d",&m); p=head; if(m==1) { temp->next=head; head=temp; } else { i=2; while(i!=m)

{ p=p->next; i++; } temp->next=p->next; p->next=temp; } printf("\n %d is sucessfully located at %dth position",temp->data,m); } if(k==4) { temp->data=NULL; p=head; while(p->next!=NULL) { q=head; while(q->next!=NULL) { if((q->data)>(q->next)->data) { temp->data=(q->next)->data; (q->next)->data=q->data; q->data=temp->data; } q=q->next; } p=p->next; } printf("\nEnter the data to be inserted"); scanf("%d",&n); temp->data=n; p=head; while(p->data<temp->data)

{ q=p; p=p->next; } if(p==head->next) { temp->next=head->next; head->next=temp; } else { temp->next=p; q->next=temp; } } } void deletion(int k) { temp=(struct node*)malloc(sizeof(struct node)); if(k==5) { p=head; head=head->next; free(p); } if(k==6) { int n; printf("\n Enter the data to be deleted"); scanf("%d",&n); p=head; while(p!=NULL)

{ if(p->data==n) { if(p==head) { head=p->next; free(p); } else { q->next=p->next; free(p); } } else { q=p; p=p->next; } } } if(k==7) { p=head; while(p->next->next!=NULL) { p=p->next; } p->next=NULL; printf("\n last node is sucessfully deleted"); } } void display()

{ printf("\nThe elements inserted are..."); p=head; while(p->next!=NULL) { printf("%3d", p->data); p=p->next; } printf("%3d",p->data); }

output

output 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 1 Enter the number to be inserted 5 5 Inserted sucessfully 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION

4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 2 Enter the number to be inserted 12 12 is sucessfully inserted at the end 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 3 Enter the data to be inserted 7 Enter the location where you want to insert the node 3 7 is sucessfully located at 3th position 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY

9. EXIT Enter your choice 1 Enter the number to be inserted 6 6 Inserted sucessfully 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 4 Enter the data to be inserted 8 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 8 The elements inserted are... 5 6 7 8 12 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION

4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice5 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice6 Enter the data to be deleted7 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice7 last node is sucessfully deleted 1. INSERTION AT BEGINING

2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice8 The elements inserted are... 6 8 1. INSERTION AT BEGINING 2. INSERTION AT END 3. INSERTION AT SPECIFIC POSITION 4. INSERTION AT THE SORTED LIST 5. DELETE THE FIRST NODE 6. DETETE A NODE DEPENDING UPON THE DATA 7. DELETE LAST NODE 8. DISPLAY 9. EXIT Enter your choice 9

Algorithm

PROGRAM NO:14 DATE : AIM: Program to Implement a stack using linked list
#include<stdio.h> #include<conio.h> struct stacknode { int data; struct stacknode *link; }; typedef struct stacknode stacknode; stacknode *getnode(); void main() { int ch,item,flag; stacknode **top; initializestack(&top); clrscr(); do { printf("\n.Linked list implementation of stack .\n"); printf("\n1.PUSH\n2.POP\n3.TOP OF STACK\n4.DISPLAY\n"); printf("\nEnter your choice\n"); scanf("%d",&ch);

switch(ch) { case 1:printf("\nEnter the item to be pushed"); scanf("%d",&item); pushnode(&top,item); break; case 2:popnode(&top,&item,&flag); if(flag==1) { printf("\nStack empty"); continue; } else printf("\nPopped item=%d\n",item); break; case 3:stacktopnode(top,&item,&flag); if(flag==1) { printf("\nStack empty"); } else { printf("\nTop element of stack=%d\n",item); } break; case 4:display(top); break; default:printf("\nInvalid choice\n"); } }while(ch!=5); getch(); } initializestack(stacknode **top) {

(*top)=NULL; return 0; } pushnode(stacknode **top,int item) { stacknode *temp; temp=getnode(); temp->data=item; temp->link=(*top); (*top)=temp; return 0; } popnode(stacknode **top,int *item,int *flag) { stacknode *temp; if(Isstackempty(*top)) { *flag=1; return; } *flag=0; temp=(*top); *item=temp->data; (*top)=(*top)->link; freenode(temp); return; } stacktopnode(stacknode *top,int *item,int *flag) { if(Isstackempty(top)) { *flag=1; return; }

*flag=0; (*item)=top->data; return; } display(stacknode *top) { int i; if(Isstackempty(top)) { printf("\nStack is empty"); return; } printf("\nContents of stack are as...\n"); while(top!=NULL) { printf("\n%d",top->data); top=top->link; } printf("\n"); return; } Isstackfull(stacknode *t) { if(t==NULL) return 1; else return 0; } Isstackempty(stacknode *t) { if(t==NULL) return 1; else return 0;

} stacknode *getnode() { stacknode *t; t=(stacknode *)malloc(sizeof(stacknode)); return t; } freenode(stacknode *t) { free(t); return; }

OUTPUT
.Linked list implementation of stack.

1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY Enter your choice 1 Enter the item to be pushed 45 .Linked list implementation of stack. 1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY

Enter your choice 1 Enter the item to be pushed 67

.Linked list implementation of stack. 1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY Enter your choice 2 Popped item=67

.Linked list implementation of stack. 1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY Enter your choice 3 Top element of stack=45 .Linked list implementation of stack.

1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY

Enter your choice 4 Contents of stack are as...

45

.Linked list implementation of stack.

1.PUSH 2.POP 3.TOP OF STACK 4.DISPLAY

Enter your choice 5 Invalid choice

Algorithm
Insertion Procedure ADDQ(i,y) // Add y to the i th QUEUE call GETNODE(x) DATA(x)<-y LINK(x)<-0 if f(i)=0 then f(i)<-r(i)<-x //queue is empty else LINK(r(i))<-x r(i)<-x End ADDQ //queue was not empty

Deletion Procedure DELETEQ(i,y) // if f(i)=0 then call QUEUE_EMPTY else x<-f(i) f(i)<-LINK(x) y<-DATA(x) call RET(x) End DELETEQ

PROGRAM NO:15 DATE : AIM: Program to Implement a queue using linked list
#include<stdio.h> #include<conio.h> struct node { int info; struct node *next; } *front, *rear; void insert(int elt); int delete(); void display(); void main() { int ch, elt; clrscr(); rear = NULL; front = NULL; do { printf("\n........ Menu .........\n"); printf("\n1.Insert\n2.Delete\n3.Display\n"); printf("Enter your choice :: ");

scanf("%d", &ch); switch (ch) { case 1:printf("\nEnter The Element Value\n"); scanf("%d", &elt); insert(elt); break; case 2:elt = delete(); printf("\nThe deleted element = %d\n", elt); break; case 3:display(); break; default:printf("Invalid Choice"); } }while(ch<4); getch(); } void insert(int elt) { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p->info = elt; p->next = NULL; if (rear == NULL || front == NULL) front = p; else rear->next = p; rear = p; } int delete() { struct node *p; int elt; if (front == NULL || rear == NULL)

{ printf("\nUnder Flow"); getch(); exit(0); } else { p = front; elt = p->info; front = front->next; free(p); } return (elt); } void display() { struct node *t; t = front; while (front == NULL || rear == NULL) { printf("\nQueue is empty"); getch(); exit(0); } while (t != NULL) { printf("%d\t", t->info); t = t->next; } }

OUTPUT
........ Menu .........

1.Insert 2.Delete 3.Display Enter your choice :: 1

Enter The Element Value 45

........ Menu .........

1.Insert 2.Delete 3.Display Enter your choice :: 1

Enter The Element Value 56

........ Menu .........

1.Insert 2.Delete 3.Display Enter your choice :: 2

The deleted element = 45

........ Menu .........

1.Insert 2.Delete 3.Display Enter your choice :: 3

The elements in the queue are 56 ........ Menu .........

1.Insert 2.Delete 3.Display Enter your choice :: 4 Invalid Choice

Algorithm

PROGRAM NO:16 DATE : AIM : Implement a doubly linked list. Perform a) Add a node at the beginning b) Delete a node after a particular node c) Delete a particular node.

#include<stdio.h> #include<conio.h> struct node { int data; struct node *rlink,*llink; }; struct node *start=NULL,*p,*q,*temp,*c; void create() { int item;

temp=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data:\n"); scanf("%d",&item); temp->data=item; } void insertbeg() { if(start==NULL) { start=temp; temp->rlink=NULL; temp->llink=NULL; } else { temp->rlink=start; temp->rlink->llink=temp; temp->llink=NULL; start=temp; } } void deleteafter() {

int item,k=0; c=start; printf("\nEnter the node after which data is to be deleted:\n"); scanf("%d",&item); while(c!=NULL) { if(c->data==item) { k=1; c=c->rlink; if(c->rlink==NULL) { c->llink->rlink=NULL; c->llink=NULL; } else { c->llink->rlink=c->rlink; c->rlink->llink=c->llink; } free(c); break; }

c=c->rlink; } if(k==0) printf("\nThere is no such node in the list\n"); } void deletenode() { int item,f=0; printf("\nEnter the data to be deleted:"); scanf("%d",&item); c=start; while(c!=NULL) { if(c->data==item) { f=1; if(c->llink==NULL) { start=c->rlink; c->llink=NULL; c->rlink=NULL; free(c); }

else if(c->rlink==NULL) { c->llink->rlink=NULL; c->llink=NULL; free(c); } else { c->llink->rlink=c->rlink; c->rlink->llink=c->llink; c->rlink=NULL; c->llink=NULL; free(c); } } c=c->rlink; } if(f==0) printf("\nThere is no such a node in the list\n"); else printf("\nThe node is successfully deleted\n"); } void display()

{ c=start; if(start==NULL) printf("\nThe list is empty\n"); else { printf("\nThe elements in the list are \n"); while(c!=NULL) { printf("%d\t",c->data); c=c->rlink; } } } void main() { int ch; clrscr(); printf("\n......Implementation of Doubly linked list......"); while(ch<=4) { printf("\n1:Add a node at the begining\n2:Delete a node after a particular node\n3:Delete a particular node\n4:Exit\n"); printf("\nEnter the choice:\n");

scanf("%d",&ch); switch(ch) { case 1:create(); insertbeg(); display(); break; case 2:deleteafter(); display(); break; case 3:deletenode(); display(); break; case 4:printf("\nEnter right choice"); break; } } }

PROGRAM NO: DATE : AIM: Write a program to perform Bubble sort


#include<stdio.h> #include<conio.h> void bubblesort(int a[],int n); void main() { int a[10]; int i,n; clrscr(); printf("\nEnter the size of the array"); scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); }

bubblesort(a,n); printf("\nThe sorted array is \n\n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } getch(); } void bubblesort(int a[],int n) { int i,j,t; for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } }

PROGRAM NO : DATE : AIM: Write a program to perform Insertion sort


#include<stdio.h> #include<conio.h> void insertionsort(int a[],int n); void main() { int a[10]; int i,n; clrscr(); printf("\nEnter the size of the array"); scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); }

insertionsort(a,n); printf("\nThe sorted array is"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } getch(); } void insertionsort(int a[],int n) { int i,j,t,item; for(j=1;j<n;j++) { item=a[j]; for(i=j-1;i>=0&&item<a[i];i--) { a[i+1]=a[i]; } a[i+1]=item; } }

Potrebbero piacerti anche