-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_list_First_end.c
101 lines (98 loc) · 1.63 KB
/
Linked_list_First_end.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
//Implement
//CODE:~
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void ins_first(int);
void ins_last(int);
struct node
{
int info;
struct node *link;
}*first;
void main()
{
int item,choice;
do
{
printf("\n(1)Insert at First\n(2)Insert at End\n(3)Display\n(4)Exit");
printf("\n\nEnter Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Item :");
scanf("%d",&item);
ins_first(item);
break;
case 2:
printf("Enter Item :");
scanf("%d",&item);
ins_last(item);
break;
case 3:
display();
}
} while(choice>0&&choice<4);
}
void ins_first(int item)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Memory not allocated");
return;
}
newnode->info = item;
if(first == NULL)
{
newnode->link = NULL;
first = newnode;
}
else
{
newnode->link = first;
first = newnode;
}
}
void ins_last(int item)
{
struct node *newnode, *temp;
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\nMemory not allocated");
return;
}
newnode->node = item;
newnode->link = NULL;
if(first == NULL)
{
first = newnode;
return;
}
temp = first;
while(temp->link!=NULL)
{
temp = temp->link;
}
temp->link = newnode;
return;
}
void display()
{
struct node *temp;
if(first == NULL)
{
printf("\n\nLinked List is Empty");
return;
}
temp = first;
while(temp!=NULL)
{
printf("\n\t%d",temp->info);
temp = temp->link;
}
}
//OUTPUT:~