-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
linkedlist_implentation_usingarray.cpp
114 lines (103 loc) · 3 KB
/
linkedlist_implentation_usingarray.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
/**
* \file
* \brief Linked list implementation using Arrays
*
* The difference between the pointer implementation of linked list and array
* implementation of linked list:
* 1. The NULL is represented by -1;
* 2. Limited size. (in the following case it is 100 nodes at max). But we can
* reuse the nodes that are to be deleted by again linking it bacj to the list.
*/
#include <iostream>
struct Node {
int data;
int next;
};
Node AvailArray[100]; ///< array that will act as nodes of a linked list.
int head = -1;
int avail = 0;
void initialise_list() {
for (int i = 0; i <= 98; i++) {
AvailArray[i].next = i + 1;
}
AvailArray[99].next = -1; // indicating the end of the linked list.
}
/** This will return the index of the first free node present in the avail list
*/
int getnode() {
int NodeIndexToBeReturned = avail;
avail = AvailArray[avail].next;
return NodeIndexToBeReturned;
}
/** This function when called will delete the node with
* the index presented as an argument, and will put
* back that node into the array.
*/
void freeNode(int nodeToBeDeleted) {
AvailArray[nodeToBeDeleted].next = avail;
avail = nodeToBeDeleted;
}
/** The function will insert the given data
* into the front of the linked list.
*/
void insertAtTheBeginning(int data) {
int newNode = getnode();
AvailArray[newNode].data = data;
AvailArray[newNode].next = head;
head = newNode;
}
void insertAtTheEnd(int data) {
int newNode = getnode();
int temp = head;
while (AvailArray[temp].next != -1) {
temp = AvailArray[temp].next;
}
// temp is now pointing to the end node.
AvailArray[newNode].data = data;
AvailArray[newNode].next = -1;
AvailArray[temp].next = newNode;
}
void display() {
int temp = head;
while (temp != -1) {
std::cout << AvailArray[temp].data << "->";
temp = AvailArray[temp].next;
}
std::cout << "-1" << std::endl;
}
/** Main function */
int main() {
initialise_list();
int x, y, z;
for (;;) {
std::cout << "1. Insert At The Beginning" << std::endl;
std::cout << "2. Insert At The End" << std::endl;
std::cout << "3. Display" << std::endl;
std::cout << "4.Exit" << std::endl;
std::cout << "Enter Your choice" << std::endl;
std::cin >> z;
switch (z) {
case 1:
std::cout << "Enter the number you want to enter" << std::endl;
std::cin >> x;
insertAtTheBeginning(x);
break;
case 2:
std::cout << "Enter the number you want to enter" << std::endl;
std::cin >> y;
insertAtTheEnd(y);
break;
case 3:
std::cout
<< "The linked list contains the following element in order"
<< std::endl;
display();
break;
case 4:
return 0;
default:
std::cout << "The entered choice is not correct" << std::endl;
}
}
return 0;
}