|
| 1 | +/* |
| 2 | +Copyright (C) Deepali Srivastava - All Rights Reserved |
| 3 | +This code is part of DSA course available on CourseGalaxy.com |
| 4 | +*/ |
| 5 | + |
| 6 | +#include<stdio.h> |
| 7 | +#include<stdlib.h> |
| 8 | + |
| 9 | +struct node |
| 10 | +{ |
| 11 | + int info; |
| 12 | + struct node *link; |
| 13 | +}; |
| 14 | + |
| 15 | +void createList(struct node *head); |
| 16 | +void displayList(struct node *head); |
| 17 | +void insertAtEnd(struct node *head,int data); |
| 18 | +void insertBefore(struct node *head,int data,int x); |
| 19 | +void insertAtPosition(struct node *head,int data,int k); |
| 20 | +void deleteNode(struct node *head,int data); |
| 21 | +void reverse(struct node *head); |
| 22 | + |
| 23 | +main() |
| 24 | +{ |
| 25 | + int choice,data,x,k; |
| 26 | + struct node *head; |
| 27 | + |
| 28 | + head=(struct node *)malloc(sizeof(struct node)); |
| 29 | + head->info=0; |
| 30 | + head->link=NULL; |
| 31 | + |
| 32 | + createList(head); |
| 33 | + |
| 34 | + while(1) |
| 35 | + { |
| 36 | + printf("1.Display list\n"); |
| 37 | + printf("2.Insert a node at the end of the list\n"); |
| 38 | + printf("3.Insert a new node before a node\n"); |
| 39 | + printf("4.Insert at a given position\n"); |
| 40 | + printf("5.Delete a node\n"); |
| 41 | + printf("6.Reverse the list\n"); |
| 42 | + printf("7.Quit\n\n"); |
| 43 | + printf("Enter your choice : "); |
| 44 | + scanf("%d",&choice); |
| 45 | + |
| 46 | + if(choice==7) |
| 47 | + break; |
| 48 | + |
| 49 | + switch(choice) |
| 50 | + { |
| 51 | + case 1: |
| 52 | + displayList(head); |
| 53 | + break; |
| 54 | + case 2: |
| 55 | + printf("Enter the element to be inserted : "); |
| 56 | + scanf("%d",&data); |
| 57 | + insertAtEnd(head,data); |
| 58 | + break; |
| 59 | + case 3: |
| 60 | + printf("Enter the element to be inserted : "); |
| 61 | + scanf("%d",&data); |
| 62 | + printf("Enter the element before which to insert : "); |
| 63 | + scanf("%d",&x); |
| 64 | + insertBefore(head,data,x); |
| 65 | + break; |
| 66 | + case 4: |
| 67 | + printf("Enter the element to be inserted : "); |
| 68 | + scanf("%d",&data); |
| 69 | + printf("Enter the position at which to insert : "); |
| 70 | + scanf("%d",&k); |
| 71 | + insertAtPosition(head,data,k); |
| 72 | + break; |
| 73 | + case 5: |
| 74 | + printf("Enter the element to be deleted : "); |
| 75 | + scanf("%d",&data); |
| 76 | + deleteNode(head,data); |
| 77 | + break; |
| 78 | + case 6: |
| 79 | + reverse(head); |
| 80 | + break; |
| 81 | + default: |
| 82 | + printf("Wrong choice\n\n"); |
| 83 | + } |
| 84 | + }/*End of while */ |
| 85 | +}/*End of main()*/ |
| 86 | + |
| 87 | +void displayList(struct node *head) |
| 88 | +{ |
| 89 | + struct node *p; |
| 90 | + if(head->link==NULL) |
| 91 | + { |
| 92 | + printf("List is empty\n"); |
| 93 | + return; |
| 94 | + } |
| 95 | + p=head->link; |
| 96 | + printf("List is :\n"); |
| 97 | + while(p!=NULL) |
| 98 | + { |
| 99 | + printf("%d ",p->info); |
| 100 | + p=p->link; |
| 101 | + } |
| 102 | + printf("\n"); |
| 103 | +}/*End of displayList()*/ |
| 104 | + |
| 105 | +void insertAtEnd(struct node *head,int data) |
| 106 | +{ |
| 107 | + struct node *p,*temp; |
| 108 | + temp= (struct node *)malloc(sizeof(struct node)); |
| 109 | + temp->info=data; |
| 110 | + |
| 111 | + p=head; |
| 112 | + while(p->link!=NULL) |
| 113 | + p=p->link; |
| 114 | + |
| 115 | + p->link=temp; |
| 116 | + temp->link=NULL; |
| 117 | +}/*End of insertAtEnd()*/ |
| 118 | + |
| 119 | +void createList(struct node *head) |
| 120 | +{ |
| 121 | + int i,n,data; |
| 122 | + |
| 123 | + printf("Enter the number of nodes : "); |
| 124 | + scanf("%d",&n); |
| 125 | + |
| 126 | + for(i=1; i<=n; i++) |
| 127 | + { |
| 128 | + printf("Enter the element to be inserted : "); |
| 129 | + scanf("%d",&data); |
| 130 | + insertAtEnd(head,data); |
| 131 | + } |
| 132 | +}/*End of createList()*/ |
| 133 | + |
| 134 | +void insertBefore(struct node *head,int data,int x) |
| 135 | +{ |
| 136 | + struct node *temp,*p; |
| 137 | + |
| 138 | + /*Find pointer to predecessor of node containing x*/ |
| 139 | + p=head; |
| 140 | + while(p->link!=NULL) |
| 141 | + { |
| 142 | + if(p->link->info==x) |
| 143 | + break; |
| 144 | + p=p->link; |
| 145 | + } |
| 146 | + |
| 147 | + if(p->link==NULL) |
| 148 | + printf("%d not present in the list\n",x); |
| 149 | + else |
| 150 | + { |
| 151 | + temp=(struct node *)malloc(sizeof(struct node)); |
| 152 | + temp->info=data; |
| 153 | + temp->link=p->link; |
| 154 | + p->link=temp; |
| 155 | + } |
| 156 | +}/*End of insertBefore()*/ |
| 157 | + |
| 158 | +void insertAtPosition(struct node *head,int data,int k) |
| 159 | +{ |
| 160 | + struct node *temp,*p; |
| 161 | + int i; |
| 162 | + |
| 163 | + p=head; |
| 164 | + for(i=1; i<=k-1 && p!=NULL; i++) |
| 165 | + p=p->link; |
| 166 | + |
| 167 | + if(p==NULL) |
| 168 | + printf("You can insert only upto %dth position\n\n",i-1); |
| 169 | + else |
| 170 | + { |
| 171 | + temp=(struct node *)malloc(sizeof(struct node)); |
| 172 | + temp->info=data; |
| 173 | + temp->link=p->link; |
| 174 | + p->link=temp; |
| 175 | + } |
| 176 | +}/*End of insertAtPosition()*/ |
| 177 | + |
| 178 | +void deleteNode(struct node *head, int data) |
| 179 | +{ |
| 180 | + struct node *temp,*p; |
| 181 | + |
| 182 | + p=head; |
| 183 | + while(p->link!=NULL) |
| 184 | + { |
| 185 | + if(p->link->info==data) |
| 186 | + break; |
| 187 | + p=p->link; |
| 188 | + } |
| 189 | + |
| 190 | + if(p->link==NULL) |
| 191 | + printf("Element %d not found\n",data); |
| 192 | + else |
| 193 | + { |
| 194 | + temp=p->link; |
| 195 | + p->link=temp->link; |
| 196 | + free(temp); |
| 197 | + } |
| 198 | +}/*End of deleteNode()*/ |
| 199 | + |
| 200 | +void reverse(struct node *head) |
| 201 | +{ |
| 202 | + struct node *prev, *ptr, *next; |
| 203 | + prev=NULL; |
| 204 | + ptr=head->link; |
| 205 | + while(ptr!=NULL) |
| 206 | + { |
| 207 | + next=ptr->link; |
| 208 | + ptr->link=prev; |
| 209 | + prev=ptr; |
| 210 | + ptr=next; |
| 211 | + } |
| 212 | + head->link=prev; |
| 213 | +}/*End of reverse()*/ |
| 214 | + |
0 commit comments