forked from fnplus/interview-techdev-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked List C++
102 lines (99 loc) · 1.53 KB
/
Linked List 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
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{int info;
node*next;}*newptr,*start=NULL,*rear=NULL,*save;
node* create_node(int);
void ins_beg(node*);
void ins_end(node*);
void del_beg();
void disp(node*);
void main()
{system("cls");
int choice=-1,data;
while(choice!='5')
{cout<<"Enter operation to be performed on data structure: \n";
cout<<"1.Insert at beginning(LIFO)\n"<<"2.Insert at end(FIFO)\n"<<"3.Delete from beginning\n"<<"4.Display Data Structure\n"<<"5.Exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Insert information:\n";
cin>>data;
newptr=create_node(data);
if(newptr==NULL)
{cout<<"Could not create node! Aborting!";
exit(0);}
else
{ins_beg(newptr);}
break;
case 2:
cout<<"Insert information:\n";
cin>>data;
newptr=create_node(data);
if(newptr==NULL)
{cout<<"Could not create node!Aborting!";
exit(0);}
else
{ins_end(newptr);}
break;
case 3:
del_beg();
break;
case 4:
cout<<"Displaying from Start:\n";
disp(start);
break;
case 5:
system("pause");
exit(0);
};
};
getch();
}
node*create_node(int inf)
{node*ptr;
ptr=new node;
ptr->info=inf;
ptr->next=NULL;
return ptr;
}
void ins_beg(node*ptr)
{
if(start==NULL)
{
start=ptr;
}
else
{save=start;
start=ptr;
start->next=save;
}
}
void ins_end(node*ptr)
{
if(start==NULL)
{start=rear=ptr;}
else
{rear->next=ptr;
rear=ptr;
}
}
void del_beg()
{if(start==NULL)
{cout<<"Underflow!Aborting!";
system("pause");
exit(0);}
else
{node*ptr=start;
start=start->next;
delete ptr;
}}
void disp(node*ptr)
{while(ptr!=NULL)
{cout<<ptr->info<<"->";
ptr=ptr->next;
}
cout<<"!!!\n";
}