-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedlist.c
103 lines (100 loc) · 1.51 KB
/
linkedlist.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
#include <stdio.h>
#include <stdlib.h>
typedef struct nodes
{
int value;
struct nodes *nextnode;
};
struct nodes *head = NULL;
int add(int val)
{
struct nodes *node = (struct nodes *)malloc(sizeof(struct nodes));
if (head == NULL)
{
node->value = val;
head = node;
node->nextnode = NULL;
}
else
{
node->value = val;
node->nextnode = head;
head = node;
}
}
int delete (int val)
{
struct nodes *node;
node = head;
if (head == NULL)
{
return 0;
}
if (node->value == val)
{
head = node->nextnode;
}
while (node->nextnode != NULL)
{
if ((node->nextnode)->value == val)
{
node->nextnode = (node->nextnode)->nextnode;
}
node = node->nextnode;
}
return 0;
}
int display()
{
struct nodes *node;
node = head;
while (node->nextnode != NULL)
{
printf("%d ", node->value);
node = node->nextnode;
}
printf("%d\n", node->value);
}
int insertatlast(int val)
{
struct nodes *node;
struct nodes *new = (struct nodes *)malloc(sizeof(struct nodes));
node = head;
while (node->nextnode != NULL)
{
node = node->nextnode;
}
new = node;
new->value = val;
new->nextnode = NULL;
}
int reverse()
{
struct nodes *node;
struct nodes *temp;
struct nodes *temp1;
node = head;
temp = node->nextnode;
node->nextnode = NULL;
while (temp->nextnode != NULL)
{
temp1 = temp->nextnode;
temp->nextnode = node;
node = temp;
temp = temp1;
}
temp->nextnode = node;
head = temp;
}
int main()
{
add(10);
add(30);
add(40);
add(50);
add(60);
display();
delete (40);
reverse();
display();
}