-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked list.c
155 lines (140 loc) · 2.1 KB
/
linked list.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;
void create(int x)
{
node *t=(node *)malloc(sizeof(node));
t->data=x;
t->next=NULL;
if(start == NULL)
start=t;
else
{
node *s=start;
while(s->next!=NULL)
s=s->next;
s->next=t;
}
}
void display()
{
node *s=start;
while(s!=NULL)
{
printf("%d->",s->data);
s=s->next;
}
printf("NULL");
}
void atbeg(int x)
{
node *t=(node *)malloc(sizeof(node));
t->data=x;
t->next=NULL;
node *temp=start;
t->next=start;
start=t;
}
void atpos(int p,int x)
{
node *t=(node *)malloc(sizeof(node));
t->data=x;
t->next=NULL;
node *temp=start;
while(temp->data!=p)
{
temp=temp->next;
}
t->next=temp->next;
temp->next=t;
}
void delatstart()
{
node *t=start;
start=start->next;
free(t);
}
void delatpos(int p)
{
node *t1=start,*t2;
while(t1->data!=p)
{
t2=t1;
t1=t1->next;
}
t2->next=t1->next;
free(t1);
}
void delatend()
{
node *t1=start,*t2;
while(t1->next!=NULL)
{
t2=t1;
t1=t1->next;
}
t2->next=NULL;
free(t1);
}
void sort()
{
node *t1=start,*t2=start;
while(t1->next!=NULL)
{
t2=start;
while(t2->next!=NULL)
{
if(t2->data>t2->next->data)
{
int t=t2->data;
t2->data=t2->next->data;
t2->next->data=t;
}
t2=t2->next;
}
t1=t1->next;
}
}
void reverse()
{
node *t1=start,*t2=start->next,*t3=t2;
while(t3!=NULL)
{
t3=t2->next;
t2->next=t1;
t1=t2;
t2=t3;
}
start->next=NULL;
start=t1;
}
void main()
{
int n,i,a;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
create(a);
}
display();
atbeg(7);
printf("\n");
display();
printf("\n");
atpos(5,1);
display();
printf("\n");
sort();
display();
printf("\n");
reverse();
display();
getch();
}