-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked lists.c
137 lines (131 loc) · 1.69 KB
/
linked lists.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
#include<stdio.h>
#incude<conio.h>
struct node{
int data;
struct node *node;
struct node *next;
};
struct node *start=NULL;
struct node *makenode(int d);
void insertfront( struct node *n);
void insertfront( struct node *n);
void insertsort( struct node *n);
struct node *search(int d);
struct node *delete(int d);
void traverse(struct node *p);
main()
{
}
struct node *makenode(int d)
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
n->data=d;
n->next=NULL;
return n;
}
void insertfront( struct node *n)
{
if(start==NULL)
{
start=n;
}
else
{
n->next=start;
start=n;
}
}
void insertback( struct node *n)
{
int *p;
if(start==NULL)
{
start=n;
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
}
p->next=n;
n->next=NULL;
}
}
void insertsort( struct node *n)
{
int *p,*q;
if(start==NULL)
{
start=n;
}
p=start;
while((p!=NULL) && (p->data < n-data))
{
q=p;
p=p->next;
}
q->next=n;
n->next=p;
}
struct node *search(int d)
{
int *p,*q;
p=start;
while(p!=NULL)
{
if(p->data==d)
{
return p;
// write print p->data after search
}
else
{
q=p;
p=p->next;
}
}
printf("The searched no. is not found");
}
struct node *delete(int d)
{
int d;
int *p,*q;
p=start;
if(p->data==d)
{
start=p->next;
free(p);
break;
}
else
{
while(p!=NULL)
{
if(p->data==d)
{
q->next=p->next;
free(p);
break;
}
else
{
q=p;
p=p->next;
}
printf("The number you need to delete is not found");
}
}
}
void traverse(struct node *p)
{
p=start;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
getch();
}