0% found this document useful (0 votes)
21 views2 pages

Dsa Ass 2 Ques '1

Uploaded by

gargvanshika001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Dsa Ass 2 Ques '1

Uploaded by

gargvanshika001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 /*

2 Problem Statement: WAP to create a linked list to hold data and link to another node.
3 Create a menu based program to perform following tasks:
4 a. Insert first node
5 b. Insert last node
6 c. Delete first node
7 d. Delete last node
8 e. Insert node at location
9 f. Delete a specific node
10 g. Searching a value
11 h. Displaying the list
12 Programmer Name: vanshika garg
13 Enrollment No.: 35350402023
14 Date: 11-04-2024
15 */
16 #include<stdio.h>
17 #include<stdlib.h>
18
19 struct student {
20 int roll, marks;
21 struct student* next;
22 };
23
24 struct student* head, * temp;
25
26 void insert_at_beg() {
27 temp = (struct student*)malloc(sizeof(struct student));
28 printf("Enter roll, marks: ");
29 scanf("%d %d", &temp->roll, &temp->marks);
30 temp->next = NULL;
31 if (head == NULL)
32 head = temp;
33 else {
34 temp->next = head;
35 head = temp;
36 }
37 }
38
39 void insert_at_end() {
40 temp = (struct student*)malloc(sizeof(struct student));
41 printf("Enter roll, marks: ");
42 scanf("%d %d", &temp->roll, &temp->marks);
43 temp->next = NULL;
44 if (head == NULL)
45 head = temp;
46 else {
47 struct student* ptr = head;
48 while (ptr->next != NULL)
49 ptr = ptr->next;
50 ptr->next = temp;
51 }
52 }
53
54 void insert_at_user_location(int n) {
55 temp = (struct student*)malloc(sizeof(struct student));
56 printf("Enter roll, marks: ");
57 scanf("%d %d", &temp->roll, &temp->marks);
58 temp->next = NULL;
59 struct student* ptr = head;
60 for ( int i = 1; i < n; i++)
61 ptr = ptr->next;
62 temp->next = ptr->next;
63 ptr->next = temp;
64 }
65
66 void delete_at_beg() {
67 if (head == NULL) {
68 printf("List is empty\n");
69 return;
70 }
71 temp = head;
72 head = head->next;
73 free(temp);
74 }
75
76 void delete_at_last() {
77 if (head == NULL) {
78 printf("List is empty\n");
79 return;
80 }
81 struct student* ptr = head;
82 temp = ptr->next;
83 if (temp == NULL) {
84 free(ptr);
85 head = NULL;
86 return;
87 }
88 while (temp->next != NULL) {
89 ptr = ptr->next;
90 temp = temp->next;
91 }
92 ptr->next = NULL;
93 free(temp);
94 }
95
96 void delete_at_user_location(int n) {
97 if (head == NULL) {
98 printf("List is empty\n");
99 return;
100 }
101 struct student* ptr = head;
102 temp = ptr->next;
103 if (n == 1) {
104 head = head->next;
105 free(ptr);
106 return;
107 }
108 for (int i = 1; i < n - 1; i++) {
109 ptr = ptr->next;
110 temp = temp->next;
111 }
112 ptr->next = temp->next;
113 free(temp);
114 }
115
116 void display() {
117 temp = head;
118 while (temp != NULL) {
119 printf("Roll_no=%d Marks=%d\n", temp->roll, temp->marks);
120 temp = temp->next;
121 }
122 }
123
124 int main() {
125 head = NULL;
126 int ch, pos;
127 while (1) {
128 printf("Menu \n 1. Insert node at beginning\n 2. Insert node at end\n 3. Insert
node at user-specified location\n 4. Delete node from beginning\n 5. Delete node from
end\n 6. Delete node from user-specified location\n 7. Display list\n 8. Exit\n");
129 printf("Enter your choice: ");
130 scanf("%d", &ch);
131
132 if (ch == 1) {
133 insert_at_beg();
134 } else if (ch == 2) {
135 insert_at_end();
136 } else if (ch == 3) {
137 printf("Enter position: ");
138 scanf("%d", &pos);
139 insert_at_user_location(pos);
140 } else if (ch == 4) {
141 delete_at_beg();
142 } else if (ch == 5) {
143 delete_at_last();
144 } else if (ch == 6) {
145 printf("Enter position: ");
146 scanf("%d", &pos);
147 delete_at_user_location(pos);
148 } else if (ch == 7) {
149 display();
150 } else if (ch == 8) {
151 exit(0);
152 } else {
153 printf("Invalid choice\n");
154 }
155 }
156 return 0;
157 }
158

You might also like