Professional Documents
Culture Documents
SOHAM BARMAN
SCHOOL REGN. NO.: HS-1465
CLASS: XII-A
ROLL NO.: 21
H.S. REGN NO: 4221137938
Date: __/__/____
_____________________
Teacher’s Signature
Department of Computer Science
Ramakrishna Mission Vidyalaya, Narendrapur
CODE
1: #include <stdio.h>
2: #include <conio.h>
3: #include <stdlib.h>
4: #include <malloc.h>
5: struct node
6: {
7: int data;
8: struct node *next;
9: };
10: struct node *head = NULL;
11:
12: void createlist() //Create List
13: {
14: struct node *ptr, *newnode;
15: int num;
16: printf("\nEnter -1 to end...");
17: printf("\nEnter the data: ");
18: scanf("%d",&num);
19: while (num!=-1)
20: {
21: newnode=(struct node *)malloc(sizeof(struct node));
22: newnode->data=num;
23: if (head==NULL)
24: {
25: newnode->next=NULL;
26: head=newnode;
27: }
28: else
29: {
30: ptr=head;
31: while (ptr->next!=NULL)
32: {
33: ptr=ptr->next;
34: }
35: ptr->next=newnode;
36: newnode->next=NULL;
37: }
38: printf("Enter the data: ");
39: scanf("%d",&num);
40: }
41: }
42:
43: void displaylist() //Display List
44: {
45: struct node *ptr;
46: if (head==NULL)
47: {
48: printf("\nList is Empty. Please create a list.");
49: }
50: else
51: {
52: ptr=head;
53: printf("\n");
54: while (ptr!=NULL)
55: {
56: printf("%d ",ptr->data);
57: ptr=ptr->next;
58: }
59: }
60: }
61:
62: void insertbegin() //Insert at beginning
63: {
64: struct node *newnode;
65: int num;
66: printf("\nEnter the data: ");
67: scanf("%d",&num);
68: newnode=(struct node *)malloc(sizeof(struct node));
69: newnode->data=num;
70: newnode->next=head;
71: head=newnode;
72: }
73:
74: void insertend() //Insert at end
75: {
76: struct node *ptr, *newnode;
77: int num;
78: printf("\nEnter the data: ");
79: scanf("%d",&num);
80: newnode=(struct node *)malloc(sizeof(struct node));
81: newnode->data=num;
82: newnode->next=NULL;
83: ptr=head;
84: while(ptr->next!=NULL)
85: {
86: ptr=ptr->next;
87: }
88: ptr->next=newnode;
89: }
90:
91: void insertbefore() //Insert before a value
92: {
93: struct node *newnode, *ptr, *preptr;
94: int num, val;
95: printf("\nEnter the value before which the data will be inserted: ");
96: scanf("%d",&val);
97: printf("\nEnter the data: ");
98: scanf("%d",&num);
99: newnode=(struct node *)malloc(sizeof(struct node));
100: newnode->data=num;
101: ptr=head;
102: while (ptr->data!=val)
103: {
104: preptr=ptr;
105: ptr=ptr->next;
106: }
107: preptr->next=newnode;
108: newnode->next=ptr;
109: }
110:
111: void insertafter() //Insert after a value
112: {
113: struct node *newnode, *ptr, *postptr;
114: int num, val;
115: printf("\nEnter the value after which the data will be inserted: ");
116: scanf("%d",&val);
117: printf("\nEnter the data: ");
118: scanf("%d",&num);
119: newnode=(struct node *)malloc(sizeof(struct node));
120: newnode->data=num;
121: ptr=head;
122: postptr=ptr->next;
123: while (ptr->data!=val)
124: {
125: ptr=ptr->next;
126: postptr=ptr->next;
127: }
128: ptr->next=newnode;
129: newnode->next=postptr;
130: }
131:
132: void deletebegin() //Delete beginning
133: {
134: struct node *ptr;
135: ptr=head;
136: head=head->next;
137: free(ptr);
138: }
139:
140: void deleteend() //Delete End
141: {
142: struct node *preptr, *ptr;
143: ptr=head;
144: while (ptr->next!=NULL)
145: {
146: preptr=ptr;
147: ptr=ptr->next;
148: }
149: preptr->next=NULL;
150: free(ptr);
151: }
152:
153: void deletenode() //Delete a given node
154: {
155: struct node *preptr, *ptr;
156: int val;
157: printf("\nEnter the value which you want to delete: ");
158: scanf("%d",&val);
159: ptr=head;
160: while (ptr->data!=val)
161: {
162: preptr=ptr;
163: ptr=ptr->next;
164: }
165: preptr->next=ptr->next;
166: free(ptr);
167: }
168:
169: void deletelist() //Delete List
170: {
171: struct node *ptr, *curr;
172: ptr=head;
173: while (ptr->next!=NULL)
174: {
175: curr=ptr;
176: ptr=ptr->next;
177: free(curr);
178: }
179: head=NULL;
180: }
181:
182: void sortlist() //Sorting the list
183: {
184: struct node *ptr1, *ptr2;
185: int temp;
186: ptr1=head;
187: while (ptr1!=NULL)
188: {
189: ptr2=ptr1->next;
190: while (ptr2!=NULL)
191: {
192: if (ptr1->data>ptr2->data)
193: {
194: temp=ptr1->data;
195: ptr1->data=ptr2->data;
196: ptr2->data=temp;
197: }
198: ptr2=ptr2->next;
199: }
200: ptr1=ptr1->next;
201: }
202: }
203:
204: int main()
205: {
206: int option;
207: do
208: {
209: printf("\n\n\n\n******** MAIN MENU ********\n");
210: printf("\n1: Create list");
211: printf("\n2: Display the list");
212: printf("\n3: Add a node at the beginning");
213: printf("\n4: Add a node at the end");
214: printf("\n5: Add a node before a given node");
215: printf("\n6: Add a node after a given node");
216: printf("\n7: Delete from the beginning");
217: printf("\n8: Delete from the end");
218: printf("\n9: Delete a given node");
219: printf("\n10: Delete the entire list");
220: printf("\n11: Sort the list");
221: printf("\n12: Clear the screen");
222: printf("\n13: Exit");
223: printf("\n\n***************************\n");
224: printf("\nEnter your option: ");
225: scanf("%d",&option);
226: switch (option)
227: {
228: case 1:
229: createlist();
230: printf("\nList created...");
231: break;
232: case 2:
233: displaylist();
234: break;
235: case 3:
236: insertbegin();
237: printf("\nNode added...");
238: break;
239: case 4:
240: insertend();
241: printf("\nNode added...");
242: break;
243: case 5:
244: insertbefore();
245: printf("\nNode added...");
246: break;
247: case 6:
248: insertafter();
249: printf("\nNode added...");
250: break;
251: case 7:
252: deletebegin();
253: printf("\nFirst node deleted...");
254: break;
255: case 8:
256: deleteend();
257: printf("\nLast node deleted...");
258: break;
259: case 9:
260: deletenode();
261: printf("\nNode deleted...");
262: break;
263: case 10:
264: deletelist();
265: printf("\nList deleted...");
266: break;
267: case 11:
268: sortlist();
269: printf("\nList sorted...");
270: break;
271: case 12:
272: system("cls");
273: break;
274: case 13:
275: exit(1);
276: default :
277: printf("WRONG INPUT :( Press ENTER to reenter your option...");
278: getch();
279: }
280: }while (1);
281: }
SCREENSHOTS OF
OUTPUT
ACKNOWLEDGEMENT
I would like to acknowledge and express my sincere
gratitude to my computer teachers for their invaluable
guidance, support, and encouragement throughout the
course of this project. Their insights and expertise were
instrumental in helping me navigate the challenges and
complexities of this undertaking.
Sincerely,
Soham Barman