Vous êtes sur la page 1sur 42

1 1. Write a program to perform the insert operation on a singly linked list. a) At beginning b) At middle c) At end Program:#include<stdio.h> #include<stdlib.h> #include<alloc.

h> #include<conio.h> typedef struct node { int data; struct node *next; }node; void createlist(); void showlist(); void insertbeg(); void insertend(); void insertafter(); node *start=NULL; int n, pos; void main() { int c; clrscr(); while(1) { printf("\n------Menu-----\n"); printf("1.Create List\n 2. Insert List\n 3.Insert After\n 4.Insert End\n 5.Display\n 6.Exit\n"); printf("Enter your choice\n"); scanf("%d",&c); switch(c) { case 1:printf("Enter Domain\n"); scanf("%d",&n); createlist(); break; case 2: printf("Enter Data to be inserted\n"); scanf("%d",&n); insertbeg(); break;

2 case 3:printf("Enter postion of data\n"); scanf("%d",&pos); insertafter(); break; case 4:printf("Enter Data\n"); scanf("%d",&n); insertend(); break; case 5:printf("Created linked list\n"); showlist(); break; case 6:exit(0); break; default:printf("Wrong entry\n"); } getch(); }} void createlist() { node *temp; if(start==NULL) { start=(node*)malloc(sizeof(node)); start->data=n; start->next=NULL; } else { temp = start; while(temp->next!=NULL) temp=temp->next; temp->next=(node*)malloc(sizeof(node)); temp->next->data=n; temp->next->next=NULL; }} void insertbeg() {node *temp; temp=(node*)malloc(sizeof(node)); temp->data=n; temp->next=start; start=temp; }

3 void insertafter() { node *temp; node *s=start; int i; for(i=1;i<pos;i++) {s=s->next; if(s==NULL) {printf("Insertion not possible\n"); getch(); return; }} temp=(node*)malloc(sizeof(node)); temp->data=n; temp->next=s->next; s->next=temp; } void insertend() { node *temp; temp=start; while(temp->next!=NULL) temp=temp->next; temp->next=(node*)malloc(sizeof(node)); temp->next->data=n; temp->next->next=NULL; } void showlist() { node*p; if(start==NULL) { printf("List Empty\n") ; getch(); return; } p=start; while(p!=NULL) { printf("%d\n",p->data); p=p->next; }}

4 Output:-

Remarks:-

We have used here the self refrencial structure to execute the singly linked list insert operation a) at beginning b) at middle c) at end In this we have defined different functions to perform each operation such as Create list, insert at beg,insert at end ,display and so on.

5 2. Write a program to perform the delete operation on a singly linked list. a) At beginning b) At middle c) At end Program:#include<stdio.h> #include<stdlib.h> #include<alloc.h> #include<conio.h> typedef struct node { int data; struct node *next; }node; void delbeg(); void delend(); void delafter(); node *start=NULL; int n, pos; void main() { int c; while(1) { printf("------Menu-----\n"); printf("1.Delete beginning\n 2.Delete end\n 3.Delete After\n 4.Exit\n"); printf("Enter your choice\n"); scanf("%d",&c); switch(c) { case 1:delbeg(); break; case 2:delend(); break; case 3:printf("Enter postion of data\n"); scanf("%d",&pos); delafter(); break; case 4:exit(0); break; default:printf("Wrong entry\n"); }

6 getch(); }} void delbeg() {node *temp; temp=start; start=start->next; temp->next=NULL; free(temp); } void delafter() { node *temp; node *s=start; int i; for(i=1;i<pos;i++) {s=s->next; if(s==NULL) {printf("node not sufficient\n"); getch(); return; }} temp=s->next; s->next=temp->next; temp->next=NULL; free(temp); } void delend() { node *temp; node *s=start; while(s->next->next!=NULL) s=s->next; temp=s->next; s->next=NULL; free(temp); }

7 Output:-

Remarks:-

We have used here the self refrencial structure to execute the singly linked list delete operation a) at beginning b) at middle c) at end In this we have defined different functions to perform each operation such as delete from beg, delete from end ,delete after any position ,display and so on.

8 3. Write a program to perform the search operation on a array using:-. a) Linear search b) Binary search Program:#include<stdio.h> #include<conio.h> int linearsearch(int ,int [],int ); int binarysearch(int [],int ,int ); void main() { int x[20],s,n,i,j,p,key,temp; clrscr(); printf("\nEnter the no. of elements"); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&x[i]); printf("\nEnter the element to be searched"); scanf("%d",&key); printf("\nWHICH TYPE OF SEARCH YOU WANT ?\n1-BINARY SEARCH\n2-LINEAR SEARCH\n"); scanf("%d",&s); switch(s) { case 1:printf("\t\tBINARY SEARCH\n"); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } printf("ENTERED NUMBERS IN ASCENDING ORDER\n"); for(i=0;i<n;i++) printf("%d\n",x[i]); p=binarysearch(x,n,key); if(p==-1) printf("\nThe search is unsuccessful"); else printf("\n %d is found at location %d",key,p+1);

9 break; case 2:("\t\tLINEAR search\n"); p=linearsearch(key,x,n); if(p==-1) printf("The search is unsuccessful"); else printf("\n %d is found at location %d",key,p+1); break; default:printf("Wrong Choice entered"); break; } } int binarysearch(int a[],int n,int k) { int lo,hi,mid; lo=0; hi=n-1; while(lo<=hi) { mid=(lo+hi)/2; if(k==a[mid]) return(mid); if(k<a[mid]) hi=mid-1; else lo=mid+1; } return(-1); } int linearsearch(int k,int a[],int n) { int i=0; while(k>=a[i]&&(i<n)) { if(k==a[i]) return(i); i=i+1; } return(-1); }

10 Output:-

Remarks:-

In this program we have used two types of searching techniques to search an element in an array a) Linear search b) Binary search In linear search what we have done is traverse the whole array and each time we match the item to be searched with the element at that position if the item is found it is displayed with the position In binary search we sort the array first and then divide the array in two half . Match the item with the element at the mid position if the item to be found matches with the element at mid position mid is displayed and search is successful otherwise we find whether the element to be search is smaller or larger then element at mid if large then low = mid +1 else high = mid -1.

11 4. Write a program to perform the sorting operation on a array using:-. a) Bubble sort b) Insertion sort c) Selection sort d) Quick sort Program:#include<stdio.h> #include<conio.h> void bubblesort(int [],int); void insertionsort(int [],int); void selectionsort(int [],int); void quicksort(int [],int, int); void main() { int a[100],n,l,i,ch; clrscr(); l=0; printf("Enter value of n:\n"); scanf("%d",&n); printf("\nEneter the elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter choice in which u want to sort"); printf("\n-----------Menu---------"); printf("\n1.Bubble sort"); printf("\n2.Insertion sort"); printf("\n3.Selection sort"); printf("\n4.Quick sort\n\n"); scanf("\n%d",&ch); switch(ch) { case 1: bubblesort(a,n); break; case 2: insertionsort(a,n); break; case 3: selectionsort(a,n); break; case 4: quicksort(a,l,n); break;

12 default:printf("\n Invalid Entry"); break; } printf("\nSorted list is as follow"); for(i=0;i<n;i++) printf("\n%d",a[i]); getch(); } void bubblesort(int a[],int n) { int temp,i,j; for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } void selectionsort(int a[],int n) { int min,loc,temp,i,j; min=a[0]; for(i=0;i<=n;i++) { min=a[i]; loc=i; for(j=i+1;j<=n-1;j++) { if(a[j]<min) { min=a[j]; loc=j; } } if(loc!=i) { temp=a[i]; a[i]=a[loc]; a[loc]=temp; }

13 } } void insertionsort(int a[],int n) { int j,k,temp; for(k=1;k<=n-1;k++) { temp=a[k]; j=k-1; while((temp<a[j])&&(j>=0)) { a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } } void quicksort(int a[],int l,int h) { int temp,key,low,high; low=l; high=h; key=a[(low+high)/2]; do { while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(l<high) quicksort(a,l,high);

14 if(low<h) quicksort(a,low,h); }

Output:-

Remarks:-

In this program we use a menu driven window to choose which sorting technology the user want to sort. a) Bubble sort b) Insertion sort c) Selection sort d) Quick sort Using the above we can sort all the elements in an array in ascending or in descending order.

15 5. Write a program to perform the following:a) Create a heap. b) Sort the element of a heap Program:#include<stdio.h> #include<conio.h> int parent(int); int left(int); int right(int); void heapify(int [],int ); void buildheap(int [],int ); void heapsort(int [],int ); void main() { int x[20],n,i; clrscr(); printf("\nEnter the numbers of numbers to be sorted:\t"); scanf("%d",&n); printf("\nEnter %d elements \n",n); for(i=0;i<n;i++) scanf("%d",&x[i]); heapsort(x,n); printf("The sorted array is \n "); for(i=0;i<n;i++) printf("%4d",x[i]); } int parent(int i) { return(i/2); } int left(int i) { return(2*i+1); } int right(int i) { return(2*i+2); } void heapify(int a[],int i,int n) { int l,r,large,temp; l=left(i);

16 r=right(i); if((l<=n-1)&&(a[l]>a[i])) large=l; else large=i; if((r<=n-1)&&(a[r]>a[large])) large=r; if(large!=i) { temp=a[i]; a[i]=a[large]; a[large]=temp; heapify(a,large,n); } } void buildheap(int a[],int n) { int i; for(i=(n-1)/2;i>=0;i--) heapify(a,i,n); } void heapsort(int a[],int n) { int i,m,temp; buildheap(a,n); m=n; for(i=n-1;i>=1;i--) { temp=a[0]; a[0]=a[i]; a[i]=temp; m=m-1; heapify(a,0,m); } }

17 Output:-

Remarks:-

18 6. Write a program to perform the Tower of Hanoi problem using recursion. Program:#include<stdio.h> #include<conio.h> void tower_of_hanoi(int n, char LEFT, char RIGHT, char CENTER) { if(n>0) { tower_of_hanoi(n-1, LEFT, CENTER, RIGHT); printf("\n\tMove disk %d from %c to %c", n, LEFT, RIGHT); tower_of_hanoi(n-1, CENTER, RIGHT, LEFT); } } void main() { int n; printf("\nEnter no. of disks: "); scanf("%d",&n); clrscr(); printf("\nSOLUTION \n\t (L=Left,R=Right,C=Ceter)\n"); tower_of_hanoi(n,'L','R','C'); } Output:-

19

Remarks:-

In this program we have use the tower of Hanoi problem .Tower of Hanoi problem is used for disk transfer from one space to another . Here we have use recursive function in order to perform the operation .
~~~~~~~~*~~~~~~~~*~~~~~~~~~~~

7. Write a program to perform the following operation on a circular queue using array. a) Insert ( ) b) Delete ( ) c) Display ( ) Program:# include<stdio.h> # include<stdlib.h> # define MAX 5 void insert(); void del(); int display(); int cqueue_arr[MAX]; int front = -1; int rear = -1; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 :insert();break;

20 case 2 :del();break; case 3:display();break; case 4:exit(0); default:printf("Wrong choice\n"); }}} void insert() { int added_item; if((front == 0 && rear == MAX-1) || (front == rear+1)) { printf("Queue Overflow \n"); return; } if (front == -1) /*If queue is empty */ { front = 0; rear = 0; } else if(rear == MAX-1)/*rear is at last position of queue */ rear = 0; else rear = rear+1; printf("Input the element for insertion in queue : "); scanf("%d", &added_item); cqueue_arr[rear] = added_item ; }/*End of insert()*/ void del() { if (front == -1) { printf("Queue Underflow\n"); return ; } printf("Element deleted from queue is : %d\n",cqueue_arr[front]); if(front == rear) /* queue has only one element */ {front = -1; rear=-1; } else if(front == MAX-1) front = 0; else front = front+1; }/*End of del() */

21 int display() {int front_pos = front,rear_pos = rear; if(front == -1) {printf("Queue is empty\n"); return 0; } printf("Queue elements :\n"); if( front_pos <= rear_pos ) while(front_pos <= rear_pos) {printf("%d ",cqueue_arr[front_pos]); front_pos++; } else {while(front_pos <= MAX-1) {printf("%d ",cqueue_arr[front_pos]); front_pos++; } front_pos = 0; while(front_pos <= rear_pos) {printf("%d ",cqueue_arr[front_pos]); front_pos++; }}printf("\n"); } Output:-

22

Remarks:-

In this program we have used a structure containing array and two variable called rear and front in order to execute the different operation performed on queue such as insert , delete , display etc. A queue is one is which we can enter elements only from rear of a array and delete element from front of an array. It is also called a FIFO (First in first out). ``````````````*````````````*``````````````

8. Write a program to perform the following operation on a circular queue using linked list. a) Insert ( ) b) Delete ( ) c) Display ( ) Program:#include<stdio.h> #include<stdlib.h> #include<alloc.h> typedef struct node { int data; struct node *next; }node; node *last=NULL; void create(int); void display(); void insathead(int); void insattail(int); void insatpos(int,int); void delathead(); void delattail(); void delatpos(int);

23

void main() { int n,c,loc; char ch; do { printf("MENU\n1. Press 1 to create circular linked list\n2. Press 2 to display the list\n3. Press 3 to isert an element at head\n4. Press 4 to insert an element at tail\n5. Press 5 to insert an element at any position you want\n6. Press 6 to delete an element from head\n7. press 7 to delete an element from tail\n8. Press 8 to delete an element from any position\n10. Press 0 to Exit"); printf("\nEnter your choice"); scanf("%d",&c); switch(c) { case 1: printf("Enter the elements u want to insert: "); scanf("%d",&n); create(n); break; case 2: printf("The elements of the linked list are: "); display(); break; case 3: printf("Enter the elements u want to insert at head: "); scanf("%d",&n); insathead(n); break; case 4: printf("Enter the elements u want to insert at tail: "); scanf("%d",&n); insattail(n); break; case 5: printf("Enter the elements and position u want to insert: "); scanf("%d%d",&n,&loc); insatpos(n,loc); break; case 6: delathead(); break; case 7: delattail(); break; case 8: printf("Enter the position from wherte u want to delete the element:"); scanf("%d",&loc); delatpos(loc); break; case 0: exit(1); default: printf("Wrong choice"); } printf("\nDo you want to conyinue(y/n)"); fflush(stdin);

24 scanf("%c",&ch); }while(ch=='Y'||ch=='y'); } void create(int x) { node *t,*temp; temp=(node *)malloc(sizeof(node)); temp->data=x; if(last==NULL) { last=temp; last->next=temp; return; } else { for(t=last->next;t!=last;t=t->next); temp->next=last->next; last->next=temp; last=temp; } } void insathead(int x) { node *temp; temp=(node *)malloc(sizeof(node)); temp->data=x; if(last==NULL) { last=temp; last->next=temp; return; } else { temp->next=last->next; last->next=temp; } } void insattail(int x) { node *temp; temp=(node *)malloc(sizeof(node)); temp->data=x; if(last==NULL) { last=temp; last->next=temp; return; }

25 else { temp->next=last->next; last->next=temp; last=temp; } } void display() { node *t; for(t=last->next;t!=last;t=t->next) printf("%d\t",t->data); printf("%d",t->data); } void insatpos(int x,int loc) { node *temp,*t; int cp; temp=(node *)malloc(sizeof(node)); temp->data=x; if(loc==1) { if(last==NULL) { last=temp; last->next=temp; return; } temp->next=last->next; last->next=temp; return; } for(cp=1,t=last->next;cp<loc-1&&t!=last;t=t->next); { /*if(cp<loc-1) { printf("Cant insert at this position"); return; } */ if(t==last) { temp->next=last->next; last->next=temp; last=temp; return; }

26 else { } } } void delathead() { int x; if(last==NULL) { printf("No item in the list"); return; } x=last->next->data; last->next=last->next->next; } void delattail() { node *t; int x; if(last==NULL) { printf("No item in the list"); return; } for(t=last->next;t->next!=last;t=t->next); x=last->data; t->next=last->next; last=t; } void delatpos(int pos) { node *t; int cp,x; if(pos==1) { if(last==NULL) { printf("No item in the list"); return; } x=last->next->data; last->next=last->next->next; } for(cp=1,t=last->next;cp<pos-1&&t!=last;t=t->next); if(cp<pos-1)

temp->next=t->next; t->next=temp; return;

27 { printf("Out of range"); return;

} if(t->next==last) { x=t->next->data; t->next=last->next; last=t; return; } else { x=t->next->data; t->next=t->next->next; return; } }

Output:-

28 Remarks:-

In this program we have used a self refrencial structure i.e. using a linked list and two variable called rear and front in order to execute the different operation performed on queue such as insert , delete , display etc. A queue is one is which we can enter elements only from rear of a array and delete element from front of an array. It is also called a FIFO (First in first out). ``````````````*````````````*``````````````

9. Write a program to perform the following operation on a stack using array. a) Push ( ) b) Pop ( ) c) Display ( ) Program:#include<stdlib.h> #include<stdio.h> #include<conio.h> struct stack { int top; int item[15]; }; void main() { int i,c; struct stack s; clrscr(); s.top=-1; while(1) { printf("\t\tSTACK OPERATION\n"); printf("1- For PUSH\n2- For POP\n3- For DISPLAY\n4- For EXIT"); printf("Enter Your Choice: "); scanf("%d",&c); switch(c){

29 case 1:if(s.top==14) printf("Overflow"); else { printf("Enter the no. u want to enter"); scanf("%d",&s.item[++s.top]); } break; case 2:if(s.top==-1) printf("Stack empty"); else printf("the deleted item is %d",s.item[s.top--]); break; case 3:printf("stack elements are"); for(i=s.top;i>=0;i--) printf("%d\n",s.item[i]); break; case 4:exit(1); default:printf("Wrong choice entered"); break; } getch(); } } Output:-

30 Remarks:-

In this program we have used an array and a variable called top in order to execute the different operation performed on stack such as insert , delete , display etc. A stack is one is which we can enter elements only from top of a array and delete element from top of the array only. It is also called a LIFO (Last in first out). `````````````*```````````*````````````

10. Write a program to perform the following operation on a stack using linked list. a) Push ( ) b) Pop ( ) c) Display ( ) Program:#include<stdio.h> #include<stdlib.h> struct stack { int data; struct stack *next; }; typedef struct stack node; node *top=NULL; void push(int); void pop(); void display(); void main() { int n,c; char ch; do { printf("\n*********MENU*************");

31 printf("\n1. Push"); printf("\n2. Pop"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&c); switch(c) { case 1: printf("\nEnter the element: "); scanf("%d",&n); push(n); break; case 2: pop(); break; case 3: display(); break; case 0: exit(0); default: printf("Wrong Choice"); } printf("\nDo u want to continue?(y/n)"); fflush(stdin); scanf("%c",&ch); } while(ch=='Y'||ch=='y'); } void push(int x) { node *temp; temp=(node *)malloc(sizeof(node)); if(temp==NULL) { printf("\nOverflow"); return; } temp->data=x; if(top==NULL) { top=temp; top->next=NULL; return; } else { temp->next=top; top=temp; return; }} void pop() { node *t; int x;

32 if(top==NULL) { printf("\nStack is Empty"); return; } x=top->data; t=top; top=top->next; free(t); return; } void display() { node *t; for(t=top;t!=NULL;t=t->next) { printf("\t%d",t->data); }}

Output:-

33 Remarks:-

In this program we have used a link list and a variable called top in order to execute the different operation performed on stack such as insert , delete , display etc. A stack is one is which we can enter elements only from top of a array and delete element from top of the array only. It is also called a LIFO (Last in first out).
````````````````*`````````````````*```````````````

11. Write a program to convert infix to postfix and evaluate using stack. Program:#include<stdio.h> #include<conio.h> #include<string.h> #define MAX 20 char stack[MAX]; int top=-1; char pop(); void push(char item); int prcd(char symbol) { switch(symbol) { case '+': case '-':return 2; break; case '*': case '/':return 4; break; case '^': case '$':return 6; break; case '(': case ')': case '#':return 1;

34 break; } } int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^': case '$': case '(': case ')':return 1; break; default:return 0; } } void convertip(char infix[],char postfix[]) { int i,symbol,j=0; stack[++top]='#'; for(i=0;i<strlen(infix);i++) { symbol=infix[i]; if(isoperator(symbol)==0) { postfix[j]=symbol; j++; } else{ if(symbol=='(') push(symbol); else if(symbol==')') { while(stack[top]!='(') { postfix[j]=pop(); j++; } pop();//pop out (. } else{ if(prcd(symbol)>prcd(stack[top]))

35 push(symbol); else{ while(prcd(symbol)<=prcd(stack[top])) { postfix[j]=pop(); j++; } push(symbol); }}}} while(stack[top]!='#') { postfix[j]=pop(); j++; } postfix[j]='\0';//null terminate string. } void main() { char infix[20],postfix[20]; clrscr(); printf("Enter the valid infix string:\n"); gets(infix); convertip(infix,postfix); printf("The corresponding postfix string is:\n"); puts(postfix); getch(); } void push(char item) { top++; stack[top]=item; } char pop() { char a; a=stack[top]; top--; return a; }

36

Output:-

Remarks:-

In this program any expression that is in its normal form ,called the infix form because in this expression; operators are performed on two surrounded operands. But in postfix expression operator is expressed after the two operands on which it is to be performed .
**

37

12. Write a program to perform the following operation on a queue using array. a) Insert ( ) b) Delete ( ) c) Display ( ) Program:#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXSIZE 50 int queue[MAXSIZE]; int front,rear; void insert(int); void del(); void display(); void main() { int n,c; char ch; front=0; rear=-1; clrscr(); do { printf("\n\n********MENU********"); printf("\n1. INSERT"); printf("\n2. DELETE"); printf("\n3. DISPLAY"); printf("\n4. EXIT"); printf("\nEnter your choice: "); scanf("%d",&c); switch(c) { case 1: printf("\nEnter the no u want to insert: "); scanf("%d",&n); insert(n); break; case 2: del(); break; case 3: display(); break; case 0: exit(0);

38 default: printf("\nWrong Choice"); } printf("\nDo you want to continue?(y/n)"); //fflush(stdin); scanf("%c",&ch); ch=getche(); } while(ch=='Y'||ch=='y'); } void insert(int item) { if(rear==MAXSIZE-1) { printf("\nQueue is FULL"); return; } else queue[++rear]=item; } void del() { int item; if(front>rear) { printf("\nQueue is EMPTY"); getch(); return; } else item=queue[front++]; } void display() { int i; printf("\nThe elements of the Queue are: "); for(i=front;i<=rear;i++) printf("%d ",queue[i]); }

39

Output:-

Remarks:-

A queue is a data structure in which operations are done on both of the ends of thiis data structure. In a queue generally push operation is done on the rear edge And the pop operation is done on the front edge as it is behavioural in real world.

40

12. Write a program to perform the following operation on a queue using linked list. a) Insert ( ) b) Delete ( ) c) Display ( ) Program:#include<stdio.h> #include<stdlib.h> typedef struct queue { int data; queue *next; }node; node *rear=NULL; void insert(int); void del(); void display(); void main() { int n,c; char ch; do { printf("\n*********MENU*************"); printf("\n1. Insert"); printf("\n2. Delete"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&c); switch(c) { case 1: printf("\nEnter the element: "); scanf("%d",&n); insert(n); break; case 2: del(); break; case 3: display(); break; case 0: exit(0);

41 default: printf("Wrong Choice");

} printf("\nDo u want to continue?(y/n)"); fflush(stdin); scanf("%c",&ch); } while(ch=='Y'||ch=='y'); } void insert(int x) { node *temp,*t; temp=(node *)malloc(sizeof(node)); if(temp==NULL) { printf("Overflow"); return; } temp->data=x; temp->next=NULL; if(rear==NULL) { rear=temp; return; } else { for(t=rear;t->next!=NULL;t=t->next); t->next=temp; return; } } void del() { node *t; int x; x=rear->data; t=rear; rear=rear->next; free(t); return; } void display() { node *t; for(t=rear;t!=NULL;t=t->next) { printf("\t%d",t->data);

42 } } Output:-

Remarks:-

In this program the previous one data structure is operated but in another way. Here the structure is implemented using the limked list . In using a link list in any data structure unwanted memory space is utilized fully.

Vous aimerez peut-être aussi