-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
86 lines (73 loc) · 1.6 KB
/
LinkedList.cpp
File metadata and controls
86 lines (73 loc) · 1.6 KB
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
#include <iostream>
#include <string>
using namespace std;
int new_data;
/* Link list node */
struct Node {
int data;
struct Node* next;
};
/* Start with the empty list */
struct Node* head = NULL;
/* Push a new node on the front
of the list. */
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
int getCount(struct Node* head)
{
int count = 0; // Initialize count
struct Node* current = head; // Initialize current
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int getNth(struct Node* head, int n)
{
struct Node* curr = head;
for (int i = 0; i < n - 1 && curr != NULL; i++)
curr = curr->next;
return curr->data;
}
/* Loop that prints out each node */
void display() {
struct Node* ptr;
ptr = head;
int n = getCount(head); // Count nodes
cout << endl;
for (int i = n; i >= 2; i--)
cout << getNth(head, i) << " ";
cout << endl;
}
int main() {
int option;
do {
cout << endl << "---------------Main Menu-------------------" << endl;
cout << "1. Create List" << endl;
cout << "2. Print List" << endl;
cout << "3. Exit" << endl;
cin >> option;
switch (option) {
case 1:
/* Loop to let user add nodes to the list */
while (new_data != -1) {
cin >> new_data;
insert(new_data);
}
break;
case 2:
cout << endl << "All nodes off the Linked List:" << endl;
display();
break;
}
} while (option != 3);
return 0;
}