forked from roerohan/Miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListFunctionality.c
More file actions
205 lines (154 loc) · 5.11 KB
/
LinkedListFunctionality.c
File metadata and controls
205 lines (154 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// A linked list node
struct Node {
char name[50];
int age;
char address[100];
int yearsOfExperience;
struct Node* next;
};
/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_age, char name[50], char address[100], int yearsOfExperience) {
/* 1. allocate Node*/
struct Node* new_node = (struct Node* ) malloc(sizeof(struct Node));
/* 2. put in the age */
new_node -> age = new_age;
new_node -> yearsOfExperience = yearsOfExperience;
strcpy(new_node -> name, name);
strcpy(new_node -> address, address);
/* 3. Make next of new node as head */
new_node -> next = ( * head_ref);
/* 4. move the head to point to the new Node*/
( * head_ref) = new_node;
}
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_age, char name[50], char address[100], int yearsOfExperience) {
/* 1. allocate Node*/
struct Node* new_node = (struct Node* ) malloc(sizeof(struct Node));
struct Node* last = * head_ref; /* used in step 5*/
/* 2. put in the age */
new_node -> age = new_age;
new_node -> yearsOfExperience = yearsOfExperience;
strcpy(new_node -> name, name);
strcpy(new_node -> address, address);
/* 3. This new node is going to be the last node, so make next
of it as NULL*/
new_node -> next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if ( * head_ref == NULL) {
* head_ref = new_node;
return;
}
/* 5. Else traverse till the last Node*/
while (last -> next != NULL)
last = last -> next;
/* 6. Change the next of last Node*/
last -> next = new_node;
return;
}
/* Checks whether the value x is present in linked list */
int search(struct Node* head, int x, int pos) {
// Base case
if (head == NULL)
return -1;
// If key is present in current node, return true
if (head -> age == x)
return pos;
// Recur for remaining list
return search(head -> next, x, ++pos);
}
/* Function to reverse the linked list */
void reverse(struct Node** head_ref)
{
struct Node* first;
struct Node* rest;
/* empty list */
if (*head_ref == NULL)
return;
first = *head_ref;
rest = first->next;
if (rest == NULL)
return;
reverse(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}
void printList(struct Node* node) {
printf("\n The list is printed in the order: age, years of experience, name, address\n");
while (node != NULL) {
printf("[ %d %d %s %s ]->", node -> age, node -> yearsOfExperience, node -> name, node -> address);
node = node -> next;
}
printf("\n");
}
/* Driver program to test above functions*/
int sampleLinkedList(){
struct Node* head = NULL;
append( &head, 6, "Name1", "Address1", 10);
push( &head, 2, "Name2", "Address2", 11);
push( &head, 3, "Name3", "Address3", 9);
append( &head, 10, "Name4", "Address4", 2);
printf("\nSearched age = 10");
int value = search(head, 10, 0);
printf("\nPresent at position: %d", value);
printf("\n Created Linked list is: ");
printList(head);
printf("\nThe reversed list is:");
reverse(&head);
printList(head);
printf("\n");
}
int input(int* age, int* yearsOfExperience, char name[], char address[]){
printf("Enter the following fields respectively: 1) Name, 2) Address, 3) Age, 4) Years of Experience:\n");
scanf("%s%s%d%d", name, address, age, yearsOfExperience);
}
int main() {
/* Start with the empty list */
struct Node* head = NULL;
int choice = 0;
int age = 0, yearsOfExperience = 0;
char name[50];
char address[100];
int key = 0;
do{
printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "1. Create a linked list", "2. Insert an item at the beginning",
"3. Insert an item at the end", "4. Search an item based on age", "5. Reverse the list", "6. Print the list", "0. Exit");
scanf("%d", &choice);
switch(choice){
case 1: sampleLinkedList();
break;
case 2: input(&age, &yearsOfExperience, name, address);
push(&head, age, name, address, yearsOfExperience);
break;
case 3: input(&age, &yearsOfExperience, name, address);
append(&head, age, name, address, yearsOfExperience);
break;
case 4:
printf("\nEnter the age to be searched: ");
scanf("%d", &key);
int index = search(head, key, 0);
if(index==-1){
printf("Element not found");
}
else{
printf("Element found at position %d", index+1);
}
break;
case 5: reverse(&head);
printList(head);
break;
case 6: printList(head);
break;
case 0: exit(0);
break;
default: "You've entered a wrong number";
}
}while(choice!=0);
return 0;
}