-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinkedList.c
executable file
·152 lines (149 loc) · 2.31 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
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
#include<stdio.h>
#include<malloc.h>
int counter = 0;
struct node
{
int data;
struct node *next;
}*head = NULL, *temp = NULL;
void insert (int item)
{
struct node *p, *q;
p=(struct node *)malloc(sizeof(struct node));
p->data=item;
p->next=NULL;
if(head==NULL)
{
head = p;
return;
}
q=head;
while(q->next!=NULL)
q=q->next;
q->next =p;
}
void pre (int item)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->data=item;
p->next=head;
head=p;
}
void any (int item)
{
struct node *p,*q;
int count=0;
p=(struct node *)malloc(sizeof(struct node));
p->data=item;
/*
//to find no. elements
q=head;
while(q->next!=NULL)
{
q=q->next;
count++;
}
printf("The number of elements is : %d\n",count);
*/
printf("Enter the position you want to enter : ");
int pos;
scanf("%d",&pos);
q=head;
while(q!=NULL)
{
++count;
if(count==pos-1)
{
/*
temp=q->next;
q->next=p;
p->next=temp;
*/
p->next=q->next;
q->next=p;
break;
}
q=q->next;
}
}
void del ()
{
printf("Enter the position of the node you want to delete : ");
int pos;
scanf("%d",&pos);
struct node *p, *q;
int count = 0;
q=head;
while(q!=NULL)
{
++count;
if(count==pos-1)
{
p=q->next;
q->next=p->next;
free (p);
break;
}
q=q->next;
}
}
void search ()
{
printf("Enter the element you want to search for : ");
int val;
scanf ("%d",&val);
struct node *q;
q=head;
while(q!=NULL)
{
if(q->data==val)
printf("The value has been found !\n");
q=q->next;
}
}
void display ()
{
struct node * p;
p = head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
counter++;
}
}
int main ()
{
int num ;
while(num!=7)
{
printf("Enter the choice : ");
scanf("%d",&num);
if(num==1)
{
int item;
printf("Enter the value you want to push : \n");
scanf("%d",&item);
insert(item);
}
if(num==2){display();}
if(num == 3)
{
int item;
printf("Enter the value you want to push : \n");
scanf("%d",&item);
pre(item);
}
if(num == 4)
{
int item;
printf("Enter the value you want to push : \n");
scanf("%d",&item);
any(item);
}
if(num==5){search();}
if(num==6){del();}
}
return 0;
}