| 1 | #include<stdio.h> | ||
| 2 | #include<stdlib.h> | ||
| 3 | struct node | ||
| 4 | { | ||
| 5 | int data; | ||
| 6 | struct node *next; | ||
| 7 | struct node *prev; | ||
| 8 | }; | ||
| 9 | struct node *header=NULL; | ||
| 10 | struct node *ptr=NULL,*temp=NULL; | ||
| 11 | void create(); | ||
| 12 | void insert_at_front(); | ||
| 13 | void insert_at_end(); | ||
| 14 | void insert_at_middle(); | ||
| 15 | void delete_at_front(); | ||
| 16 | void delete_at_end(); | ||
| 17 | void delete_at_middle(); | ||
| 18 | void count(); | ||
| 19 | void display(); | ||
| 20 | void search(); | ||
| 21 | int main() | ||
| 22 | { | ||
| 23 | int ch; | ||
| 24 | header=(struct node*)malloc(sizeof(struct node)); | ||
| 25 | header->next=NULL; | ||
| 26 | header->prev=NULL; | ||
| 27 | ptr=header; | ||
| 28 | //clrscr(); | ||
| 29 | printf("\n\n1.create \n2- insert at front\n3-insert at middle\n4-insert at end\n5-display\n6.delete_at_front\n7.delete_at_middle\n8.delete_at_end\\n9-exit"); | ||
| 30 | while(1) | ||
| 31 | { | ||
| 32 | printf("\nenter ur choice : "); | ||
| 33 | scanf("%d",&ch); | ||
| 34 | switch(ch) | ||
| 35 | { | ||
| 36 | case 1: create(); | ||
| 37 | break; | ||
| 38 | case 2: insert_at_front(); | ||
| 39 | break; | ||
| 40 | case 3: insert_at_middle(); | ||
| 41 | break; | ||
| 42 | case 4: insert_at_end(); | ||
| 43 | break; | ||
| 44 | case 5: display(); | ||
| 45 | break; | ||
| 46 | case 6: delete_at_front(); | ||
| 47 | break; | ||
| 48 | case 7: delete_at_middle(); | ||
| 49 | break; | ||
| 50 | case 8: delete_at_end(); | ||
| 51 | break; | ||
| 52 | case 9: exit(0); | ||
| 53 | default: printf("wrong choice"); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | void create() | ||
| 58 | { | ||
| 59 | char ch; | ||
| 60 | while(1) | ||
| 61 | { | ||
| 62 | insert_at_end(); | ||
| 63 | printf("if u want to continue press 'y' or 'n' :"); | ||
| 64 | fflush(stdin); | ||
| 65 | scanf("%c",&ch); | ||
| 66 | if(ch=='y'||ch=='Y') | ||
| 67 | continue; | ||
| 68 | else | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | void insert_at_front() | ||
| 73 | { | ||
| 74 | int ele; | ||
| 75 | struct node *new1; | ||
| 76 | new1=(struct node*)malloc(sizeof(struct node)); | ||
| 77 | if(new1==NULL) | ||
| 78 | { | ||
| 79 | printf("out of space"); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | printf("enter ele:"); | ||
| 83 | scanf("%d",&ele); | ||
| 84 | new1->next=header->next; | ||
| 85 | header->next->prev=new1; | ||
| 86 | header->next=new1; | ||
| 87 | new1->prev=header; | ||
| 88 | new1->data=ele; | ||
| 89 | printf("inserted successfully"); | ||
| 90 | } | ||
| 91 | int ele; | ||
| 92 | struct node *new1; | ||
| 93 | new1=(struct node*)malloc(sizeof(struct node)); | ||
| 94 | if(new1==NULL) | ||
| 95 | { | ||
| 96 | printf("out of space"); | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | printf("enter ele:"); | ||
| 100 | scanf("%d",&ele); | ||
| 101 | new1->next=header->next; | ||
| 102 | header->next->prev=new1; | ||
| 103 | header->next=new1; | ||
| 104 | new1->prev=header; | ||
| 105 | new1->data=ele; | ||
| 106 | printf("inserted successfully"); | ||
| 107 | } | ||
| 108 | void insert_at_middle() | ||
| 109 | { | ||
| 110 | struct node *new1; | ||
| 111 | int ele; | ||
| 112 | int pos,count=0; | ||
| 113 | new1=(struct node*)malloc(sizeof(struct node)); | ||
| 114 | ptr=header; | ||
| 115 | if(new1==NULL) | ||
| 116 | { | ||
| 117 | printf("out of space"); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | printf("enter Inserted ele:"); | ||
| 121 | scanf("%d",&ele); | ||
| 122 | printf("enter where it to_be inserted at what position_number:"); | ||
| 123 | scanf("%d",&pos); | ||
| 124 | /* Calculate the no of elements in the list */ | ||
| 125 | while(ptr->next!=NULL) | ||
| 126 | { | ||
| 127 | count++; | ||
| 128 | ptr=ptr->next; | ||
| 129 | } | ||
| 130 | /*Compare the entered positions number is valid or not using count the no.of elements in the list*/ | ||
| 131 | if(count<pos) | ||
| 132 | { | ||
| 133 | printf("pos is out of range"); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | ptr=header; | ||
| 137 | count=0; | ||
| 138 | /*Move the ptr upto particular entered position number*/ | ||
| 139 | while(count<pos-1) | ||
| 140 | { | ||
| 141 | ptr=ptr->next; | ||
| 142 | count++; | ||
| 143 | } | ||
| 144 | new1->next=ptr->next; | ||
| 145 | new1->prev=ptr; | ||
| 146 | ptr->next->prev=new1; | ||
| 147 | ptr->next=new1; | ||
| 148 | new1->data=ele; | ||
| 149 | printf("inserted successfully"); | ||
| 150 | } | ||
| 151 | void display() | ||
| 152 | { | ||
| 153 | ptr=header; | ||
| 154 | /* Check the List is Empty or not*/ | ||
| 155 | if(ptr->next==NULL) | ||
| 156 | { | ||
| 157 | printf("list is empty"); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | while(ptr->next!=NULL) | ||
| 161 | { | ||
| 162 | printf("%u,%d,%u -> ",ptr->next->prev,ptr->next->data,ptr->next->next); | ||
| 163 | ptr=ptr->next; | ||
| 164 | } | ||
| 165 | printf("NULL"); | ||
| 166 | } | ||
| 167 | void delete_at_front() | ||
| 168 | { | ||
| 169 | ptr=header; | ||
| 170 | /* Check the List is Empty or not*/ | ||
| 171 | if(ptr->next==NULL) | ||
| 172 | { | ||
| 173 | printf("list is empty deletion is not possible"); | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | temp=ptr->next; | ||
| 177 | ptr->next=ptr->next->next; | ||
| 178 | ptr->next->prev=ptr; | ||
| 179 | free(temp); | ||
| 180 | printf("deleted succssfully"); | ||
| 181 | } | ||
| 182 | void delete_at_end() | ||
| 183 | { | ||
| 184 | ptr=header; | ||
| 185 | /* Check the List is Empty or not*/ | ||
| 186 | if(ptr->next==NULL) | ||
| 187 | { | ||
| 188 | printf("list is empty deletion is not possible"); | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | /*Move the ptr upto last but one node*/ | ||
| 192 | while(ptr->next->next!=NULL) | ||
| 193 | { | ||
| 194 | ptr=ptr->next; | ||
| 195 | } | ||
| 196 | temp=ptr->next; | ||
| 197 | ptr->next=NULL; | ||
| 198 | free(temp); /* Clear the Memory*/ | ||
| 199 | printf("deleted succssfully"); | ||
| 200 | } | ||
| 201 | void delete_at_middle() | ||
| 202 | { | ||
| 203 | int pos,count; | ||
| 204 | ptr=header; | ||
| 205 | /* Check the List is Empty or not*/ | ||
| 206 | if(ptr->next==NULL) | ||
| 207 | { | ||
| 208 | printf("list is empty deletion is not possible"); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | count=0; | ||
| 212 | /* Calculate the no.of elements in the list */ | ||
| 213 | while(ptr->next!=NULL) | ||
| 214 | { | ||
| 215 | ptr=ptr->next; | ||
| 216 | count++; | ||
| 217 | } | ||
| 218 | printf("enter deleted position number:"); | ||
| 219 | scanf("%d",&pos); | ||
| 220 | /*Compare the entered positions number is valid or not using count the no.of elements in the list*/ | ||
| 221 | if(count<pos) | ||
| 222 | { | ||
| 223 | printf("position is_out of_range"); | ||
| 224 | return; | ||
| 225 | } | ||
| 226 | ptr=header; | ||
| 227 | count=0; | ||
| 228 | /*Move the ptr upto particular entered position number*/ | ||
| 229 | while(ptr->next->next!=NULL&&count<pos-1) | ||
| 230 | { | ||
| 231 | ptr=ptr->next; | ||
| 232 | count++; | ||
| 233 | } | ||
| 234 | temp=ptr->next; | ||
| 235 | ptr->next=ptr->next->next; | ||
| 236 | ptr->next->prev=ptr; | ||
| 237 | free(temp); | ||
| 238 | printf("deleted succssfully"); | ||
| 239 | } | ||
| /*OUTPUT: | |||
| 1.create | |||
| 2- insert at front | |||
| 3-insert at middle | |||
| 4-insert at end | |||
| 5-display | |||
| 6.delete_at_front | |||
| 7.delete_at_middle | |||
| 8.delete_at_end | |||
| 9-exit | |||
| enter ur choice : 1 | |||
| enter Inserted ele:10 | |||
| inserted_successfully if u want_to_continue press 'y' or 'n' :n | |||
| enter ur choice : 5 | |||
| 28938256,10,0 -> NULL | |||
| enter ur choice : 2 | |||
| enter ele:20 | |||
| inserted successfully | |||
| enter ur choice : 5 | |||
| 28938256,20,28938288 -> 28938320,10,0 -> NULL | |||
| enter ur choice : 3 | |||
| enter Inserted ele:25 | |||
| enter where it to_be inserted at what position_number:2 | |||
| inserted successfully | |||
| enter ur choice : 5 | |||
| 28938256,20,28938352 -> 28938320,25,28938288 -> 28938352,10,0 -> NULL | |||
| enter ur choice : 3 | |||
| enter Inserted ele:35 | |||
| enter where it to be inserted at what position number:3 | |||
| inserted successfully | |||
| enter ur choice : 5 | |||
| 28938256,20,28938352 -> 28938320,25,28938384 -> 28938352,35,28938288 -> 28938384,10,0 -> NULL | |||
| enter ur choice : 6 | |||
| deleted succssfully | |||
| enter ur choice : 5 | |||
| 28938256,25,28938384 ->
28938352,35,28938288 -> 28938384,10,0 -> NULL |
|||
| enter ur choice : 7 | |||
| enter deleted position number:2 | |||
| deleted succssfully | |||
| enter ur choice : 5 | |||
| 28938256, 25, 28938288 -> 28938352,10,0 -> NULL | |||
| enter ur choice : 8 | |||
| Deleted Succssfully | |||
| enter ur choice : 5 | |||
| 28938256,25,0 -> NULL | |||
| enter ur choice : 9 | |||
| */ | |||
C Program for Doubly Linked List Using Functions
Share to other apps