-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_a_NODE_at_the_Begining.cpp
114 lines (108 loc) · 2.08 KB
/
insert_a_NODE_at_the_Begining.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
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
using namespace std;
struct node
{
int info;
struct node *next;
} * start;
class list
{
public:
node *create_node(int);
void insert_begin();
void display();
list()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
list sl;
start = NULL;
while (1)
{
cout << "_____MENU_____\n\n1__Insert Node at beginning\n2__Display Linked List\n3__Exit\n\n>>>";
cin >> choice;
switch (choice)
{
case 1:
cout << "Inserting Node at Beginning: \n"
<< endl;
sl.insert_begin();
cout << endl;
break;
case 2:
cout << "Display elements of link list\n"
<< endl;
sl.display();
cout << endl;
break;
case 3:
cout << "Exiting...\n"
<< endl;
exit(1);
break;
default:
cout << "Wrong choice\n"
<< endl;
}
}
}
node *list::create_node(int value)
{
struct node *temp, *s;
temp = new (struct node);
if (temp == NULL)
{
cout << "Memory not allocated " << endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
void list::insert_begin()
{
int value;
cout << "Enter the value to be inserted: ";
cin >> value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout << "\nElement Inserted at beginning\n"
<< endl;
}
void list::display()
{
struct node *temp;
if (start == NULL)
{
cout << "\nThe List is Empty\n"
<< endl;
return;
}
temp = start;
cout << "\nElements of list are: \n"
<< endl;
while (temp != NULL)
{
cout << temp->info << "\t";
temp = temp->next;
}
cout<<"\n\n";
}