-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertlist.c
More file actions
99 lines (99 loc) · 1.87 KB
/
insertlist.c
File metadata and controls
99 lines (99 loc) · 1.87 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head=NULL,*first=NULL;
void insertbeg()
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node *));
printf("Enter the data : ");
scanf("%d",&ptr->data);
ptr->link=head;
head=ptr;
}
void insertend()
{
struct node *x;
x=(struct node *)malloc(sizeof(struct node *));
printf("Enter the data : ");
scanf("%d",&x->data);
first->link=x;
x->link=NULL;
first=x;
}
void insertafterkey()
{
struct node *ptr,*x;
int key;
x=(struct node *)malloc(sizeof(struct node *));
printf("Enter the data : ");
scanf("%d",&x->data);
ptr=head;
printf("Enter the value after which the value has to be inserted : ");
scanf("%d",&key);
while((ptr)&&(ptr->data!=key))
ptr=ptr->link;
x->link=ptr->link;
ptr->link=x;
}
void printlist()
{
struct node *x;
printf("This is the list :\n");
x=head;
while(x)
{
printf("%d\n",x->data);
x=x->link;
}
}
void main()
{
struct node *x;
int n,i,ch;
printf("Enter the number of elemnts : ");
scanf("%d",&n);
printf("Enter the elements :\n");
for(i=0;i<n;i++)
{
x=(struct node *)malloc(sizeof(struct node *));
scanf("%d",&x->data);
if(head==NULL)
{
x->link=NULL;
first=x;
}
else
x->link=head;
head=x;
}
while(ch!=5)
{
printf("Select any of the choices given below :\n");
printf("1. Enter the data at the beginning of the list :\n");
printf("2. Enter the data at the end of the list :\n");
printf("3. Enter the data after a specific value :\n");
printf("4. Print the list.\n");
printf("5. Exit.\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: insertbeg();
break;
case 2: insertend();
break;
case 3: insertafterkey();
break;
case 4: printlist();
break;
case 5: printf("EXITING.......\n");
break;
default:printf("INVALID ENTRY. TRY AGAIN\n");
break;
}
}
}