-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlistfunctions.c
162 lines (136 loc) · 3.08 KB
/
linkedlistfunctions.c
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
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
void append(int d, node* p){
int i = 1;
node *w = p, *new = (node*)malloc(sizeof(node)), *temp;
new->data = d;
new->next = NULL;
while (w->next != NULL){
temp = w->next;
w = temp;
i++;
}
new->index = i;
w->next = new;
printf("node added\n");
}
void pop(node *p){
node *w = p, *temp, *sl = p;
while (w->next != NULL){
sl = w;
temp = w->next;
w = temp;
}
if (w->index != 0){
sl->next = NULL;
free(w);
printf("node deleted\n");
}
else{
printf("no nodes exist\n");
}
}
void out(int n, node *p){
node *w = p, *temp;
while ((w->index != n) && (w->next != NULL)){
temp = w->next;
w = temp;
}
if (w->index == n){
printf("data at %d is %d\n", w->index, w->data);
}
else{
printf("index does not exist\n");
}
}
void insert(int d, int n, node *p){
node *w = p, *sl, *temp, *new = (node *)malloc(sizeof(node));
new->data = d;
new->index = n;
while ((w->index != n) && (w->next != NULL)){
sl = w;
temp = w->next;
w = temp;
}
if ((w->index != n) && (w->index != (n-1))){
printf("node cannot be inserted, too high or invalid index\n");
}
else if (w->index == (n-1)){
new->next = NULL;
w->next = new;
printf("node inserted\n");
}
else {
new->next = w;
sl->next = new;
w->index += 1;
printf("node inserted\n");
}
}
void rmnode(int n, node *p){
node *w = p, *temp, *sl;
while ((w->index != n) && (w->next != NULL)){
sl = w;
temp = w->next;
w = temp;
}
if (w->index == n){
sl->next = w->next;
free(w);
printf("node removed\n");
}
else{
printf("the index does not exist\n");
}
}
void search(int d, node *p){
node *w = p, *temp;
int flag = 0;
while (w->next != NULL){
if ((w->data == d) && (w->index != 0)){
flag = 1;
break;
}
else {
temp = w->next;
w = temp;
}
}
if (w->index == 0){
printf("no nodes exist\n");
return;
}
if (((flag == 1) || (w->data == d)) && (w->index != 0)){
printf("data item %d is found at index(es):\n", d);
printf("%d\n", w->index);
while (w->next != NULL){
if (w->data == d){
printf("%d\n", w->index);
}
temp = w->next;
w = temp;
}
if (w->data == d){
printf("%d\n", w->index);
}
}
else{
printf("no matches found\n");
}
}
void printlist(node *p){
node *w = p, *temp;
printf("list:\n\n");
while (w->next != NULL){
if (w->index != 0){
printf("%d\t:\t%d\n", w->index, w->data);
}
temp = w->next;
w = temp;
}
if (w->index != 0){
printf("%d\t:\t%d\n", w->index, w->data);
}
printf("\nlist end\n");
}