-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistapp.c
More file actions
160 lines (148 loc) · 3.85 KB
/
linkedlistapp.c
File metadata and controls
160 lines (148 loc) · 3.85 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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node* next;
}
node;
void append(node** list_start);
void prepend(node** list_start);
void insert(node** list_start, int list_size);
void delete(node** list_start, int list_size);
void printlist(node** list_start);
int main()
{
node* list_start = NULL;
int list_size = 0;
printf("Welcome to the linked list app by Tanmay!\n\n");
while(1)
{
int input;
printf("What do you wish to do with the list: \n 1) Append a value \n 2) Prepend a value \n 3) Delete a value \n 4) Insert a value \n 5) Print the list \n 6) End program \n\n");
scanf("%d", &input);
switch(input)
{
case 1: append(&list_start); list_size++; break;
case 2: prepend(&list_start); list_size++; break;
case 3: delete(&list_start, list_size); list_size--; break;
case 4: insert(&list_start, list_size); list_size++; break;
case 5: printlist(&list_start); break;
case 6: return 0; break;
default: printf("Input isn't between 1 and 6\n");
}
}
}
void prepend(node** list_start)
{
int value;
printf("\nEnter value to prepend: ");
scanf("%d", &value);
node* new = malloc(sizeof(node));
new -> value = value;
new -> next = *list_start;
*list_start = new;
printf("Value prepended!\n\n");
}
void append(node** list_start)
{
int val;
printf("\nEnter value to append: ");
scanf("%d", &val);
node* new = malloc(sizeof(node));
new -> value = val;
new -> next = NULL;
if(*list_start == NULL)
{
*list_start = new;
printf("Value appended!\n\n");
return;
}
node* current_node = *list_start;
while(current_node -> next != NULL)
{
current_node = current_node -> next;
}
current_node -> next = new;
printf("Value appended!\n\n");
}
void printlist(node** list_start)
{
node* current_node = *list_start;
printf("\nCurrent list: ");
while(current_node != NULL)
{
printf("%d ", current_node -> value);
current_node = current_node -> next;
}
printf("\n");
}
void insert(node** list_start, int list_size)
{
int index, value;
printf("\n Enter the position and value: ");
scanf("%d %d", &index, &value);
if(index > list_size + 1)
{
printf("Position is larger then the list!\n\n");
return;
}
node* new = malloc(sizeof(node));
node* current_node = *list_start;
new -> value = value;
if(index == 1)
{
new -> next = *list_start;
*list_start = new;
printf("Inserted!\n\n");
return;
}
if(index == list_size + 1)
{
new -> next = NULL;
while(current_node -> next != NULL)
{
current_node = current_node -> next;
}
current_node -> next = new;
printf("Inserted!\n\n");
return;
}
int i;
for(i = 1; i < index - 1; i++)
{
current_node = current_node -> next;
}
new -> next = current_node -> next;
current_node -> next = new;
printf("Inserted!\n\n");
}
void delete(node** list_start, int list_size)
{
int index, i;
printf("\nEnter the position of value to delete: ");
scanf("%d", &index);
if(index > list_size)
{
printf("Position is larger then the list!\n\n");
return;
}
if(index == 1)
{
node* current_node = *list_start;
*list_start = current_node -> next;
free(current_node);
printf("Deleted!\n\n");
return;
}
node* current_node = *list_start;
node* temp;
for(i = 1; i < index - 1; i++)
{
current_node = current_node -> next;
}
temp = current_node -> next;
current_node -> next = current_node -> next -> next;
free(temp);
printf("Deleted!\n\n");
}