forked from harsh2102/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked List
133 lines (121 loc) · 2.28 KB
/
Linked List
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next ;
}*head,*tail;
/* Code for insertion at the beginning*/
void insert_at_beg ()
{
struct node *newnode ;
newnode= (struct node*)malloc(sizeof(struct node));
printf("Enter Data ");
scanf("%d",&(*newnode).data);
newnode->next=head;
head=newnode;
}
/* Code for insertion at end*/
void insert_at_end()
{
struct node *newnode ;
newnode= (struct node*)malloc(sizeof(struct node));
printf("Enter Data ");
scanf("%d",&(*newnode).data);
newnode->next=tail->next;
tail->next=newnode;
tail=newnode;
}
/* Code for insertion at ant specific position */
void insert_at_pos()
{
int pos;
int count;
printf("Enter the position");
scanf("%d",&pos);
int i=1;
struct node *temp;
temp=head;
if (i<pos)
{
temp=temp->next;
}
struct node *newnode ;
newnode= (struct node*)malloc(sizeof(struct node));
printf("Enter Data ");
scanf("%d",&(*newnode).data);
newnode->next=temp->next;
temp->next=newnode;
}
/* Code for Displaying Elements*/
void display()
{
tail= head;
while(tail->next=head);
{
printf("%d",tail->data);
printf("\n");
tail=tail->next;
}
printf("%d",tail->data);
}
int main ()
{
head=0;
int dec =1;
int count =0;
while(dec)
{
/* Creating node*/
struct node *newnode ;
newnode= (struct node*)malloc(sizeof(struct node));
printf("Enter Data ");
scanf("%d",&(*newnode).data);
newnode->next=0;
count++;
if(head==0)
{
head=newnode;
tail=newnode;
}
else
{
tail->next=newnode;
tail=newnode;
}
printf("Do you want to continue(0,1)?");
scanf("%d",&dec);
newnode->next=head;
}
do
{
int choice;
int flag =0;
printf("Enter the Choice \n");
printf("Enter 1 to insert at beginning \n");
printf("Enter 2 to insert at end\n");
printf("Enter 3 to insert at specific position\n");
printf("Enter 4 to display element\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_at_beg();
break;
case 2:insert_at_end();
break;
case 3: printf("Postion should be less than %d",count);
printf("\n");
insert_at_pos();
break;
case 4:display();
break;
default:
flag=1;
break;
if (flag ==0)
continue;
if(flag==1)
break;
}
}while(1);
}