TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
linkedlist_implentation_usingarray.cpp
Go to the documentation of this file.
1
12
#include <iostream>
13
14
struct
Node
{
15
int
data;
16
int
next;
17
};
18
19
Node
AvailArray
[100];
20
21
int
head = -1;
22
int
avail = 0;
23
void
initialise_list() {
24
for
(
int
i = 0; i <= 98; i++) {
25
AvailArray
[i].next = i + 1;
26
}
27
AvailArray
[99].next = -1;
// indicating the end of the linked list.
28
}
29
32
int
getnode
() {
33
int
NodeIndexToBeReturned = avail;
34
avail =
AvailArray
[avail].next;
35
return
NodeIndexToBeReturned;
36
}
37
42
void
freeNode
(
int
nodeToBeDeleted) {
43
AvailArray
[nodeToBeDeleted].next = avail;
44
avail = nodeToBeDeleted;
45
}
46
50
void
insertAtTheBeginning
(
int
data
) {
51
int
newNode =
getnode
();
52
AvailArray
[newNode].data =
data
;
53
AvailArray
[newNode].next = head;
54
head = newNode;
55
}
56
57
void
insertAtTheEnd(
int
data
) {
58
int
newNode =
getnode
();
59
int
temp = head;
60
while
(
AvailArray
[temp].next != -1) {
61
temp =
AvailArray
[temp].next;
62
}
63
// temp is now pointing to the end node.
64
AvailArray
[newNode].data =
data
;
65
AvailArray
[newNode].next = -1;
66
AvailArray
[temp].next = newNode;
67
}
68
69
void
display() {
70
int
temp = head;
71
while
(temp != -1) {
72
std::cout <<
AvailArray
[temp].data <<
"->"
;
73
temp =
AvailArray
[temp].next;
74
}
75
std::cout <<
"-1"
<< std::endl;
76
}
77
79
int
main
() {
80
initialise_list();
81
int
x, y, z;
82
for
(;;) {
83
std::cout <<
"1. Insert At The Beginning"
<< std::endl;
84
std::cout <<
"2. Insert At The End"
<< std::endl;
85
std::cout <<
"3. Display"
<< std::endl;
86
std::cout <<
"4.Exit"
<< std::endl;
87
std::cout <<
"Enter Your choice"
<< std::endl;
88
std::cin >> z;
89
switch
(z) {
90
case
1:
91
std::cout <<
"Enter the number you want to enter"
<< std::endl;
92
std::cin >> x;
93
insertAtTheBeginning
(x);
94
break
;
95
case
2:
96
std::cout <<
"Enter the number you want to enter"
<< std::endl;
97
std::cin >> y;
98
insertAtTheEnd(y);
99
break
;
100
case
3:
101
std::cout
102
<<
"The linked list contains the following element in order"
103
<< std::endl;
104
display();
105
break
;
106
case
4:
107
return
0;
108
default
:
109
std::cout <<
"The entered choice is not correct"
<< std::endl;
110
}
111
}
112
113
return
0;
114
}
data
int data[MAX]
test data
Definition
hash_search.cpp:24
freeNode
void freeNode(int nodeToBeDeleted)
Definition
linkedlist_implentation_usingarray.cpp:42
getnode
int getnode()
Definition
linkedlist_implentation_usingarray.cpp:32
main
int main()
Definition
linkedlist_implentation_usingarray.cpp:79
AvailArray
Node AvailArray[100]
array that will act as nodes of a linked list.
Definition
linkedlist_implentation_usingarray.cpp:19
insertAtTheBeginning
void insertAtTheBeginning(int data)
Definition
linkedlist_implentation_usingarray.cpp:50
Node
Definition
linkedlist_implentation_usingarray.cpp:14
data_structures
linkedlist_implentation_usingarray.cpp
Generated by
1.12.0