-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathlinkedlist.cpp
103 lines (103 loc) · 1.64 KB
/
linkedlist.cpp
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
#include"conio.h"
#include"stdio.h"
#include"alloc.h"
#include"stdlib.h"
struct node
{ int info;
struct node *link;
};
struct node* START=NULL;
struct node* creatnode()
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
return n;
}
void insertnode() //insertion at the last
{
struct node *temp,*t;
temp=creatnode();
printf("enter a number: ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL)
START=temp;
else
{ t=START;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
}
void deletnode() //deletion of first node
{
struct node *r;
if(START==NULL)
printf("list is empty");
else
{ r=START;
START=START->link;
}
free(r);
}
void deletenodelast() //deletion of last node
{
struct node *t;
if(START==NULL)
printf("list is empty");
else
{ t=START;
while(t->link!=NULL)
t=t->link;
t->info=NULL;
}
}
void viewlist()
{
struct node *t;
if(START==NULL)
printf("list is empty");
else
{
t=START;
while(t!=NULL)
{
printf("%d\t",t->info);
t=t->link;
}
}
}
int menu()
{
int ch;
printf("\n1.Add value to the list");
printf("\n2.Delete the first value");
printf("\n3.Delete the last value");
printf("\n4.View list");
printf("\n5.Exit");
printf("\nenter ur choise : ");
scanf("%d",&ch);
return(ch);
}
void main()
{
while(1)
{
clrscr();
switch(menu())
{
case 1: insertnode();
break;
case 2: deletnode();
break;
case 3:deletenodelast();
break;
case 4: viewlist();
break;
case 5: exit(0);
default:
printf("invalid choise");
}
getch();
}
}