Skip to content

Commit 6984840

Browse files
authored
Add files via upload
1 parent bff07f5 commit 6984840

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

main.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
//This could be in the linked_list class as well
5+
class node{
6+
public:
7+
int data;
8+
node * next;
9+
//Constructor for basic node
10+
node(int x){
11+
data = x;
12+
next = NULL;
13+
}
14+
};
15+
16+
class linked_list{
17+
public:
18+
node *head;
19+
node *tail;
20+
public:
21+
linked_list(){ //Linked list constructor
22+
head = NULL;
23+
tail = NULL;
24+
}
25+
~linked_list(){
26+
while (head != NULL) {
27+
pop();
28+
//print();
29+
}
30+
31+
}
32+
void push(int value){
33+
if(head == NULL){
34+
head = new node(value);
35+
tail = head;
36+
}
37+
else{
38+
tail->next = new node(value);
39+
tail = tail->next;
40+
}
41+
cout << "Added: " << value << endl;
42+
}
43+
44+
void pop(){
45+
node * temp;
46+
node * prev;
47+
temp = head;
48+
if (temp == NULL)
49+
return;
50+
else if (temp->next == NULL) {
51+
head = NULL;
52+
tail = NULL;
53+
delete temp;
54+
}
55+
else {
56+
prev = temp;
57+
temp = temp->next;
58+
while (temp->next != NULL) {
59+
prev = temp;
60+
temp = temp->next;
61+
}
62+
63+
64+
65+
tail = prev;
66+
tail->next = NULL;
67+
delete temp;
68+
}
69+
}
70+
71+
72+
void print(){
73+
node * p;
74+
p = head;
75+
while(p != NULL){
76+
cout << p->data << "--";
77+
p = p->next;
78+
}
79+
cout << "END\n";
80+
}
81+
};
82+
83+
84+
int main(void){
85+
linked_list test;
86+
test.push(4);
87+
test.push(8);
88+
test.push(12);
89+
test.push(16);
90+
test.push(20);
91+
test.push(24);
92+
test.print();
93+
94+
/*
95+
test.pop();
96+
test.print();
97+
test.pop();
98+
test.print();
99+
test.pop();
100+
test.print();
101+
test.pop();
102+
test.print();
103+
test.pop();
104+
test.print();
105+
test.pop();
106+
test.print();
107+
test.pop();
108+
test.print();
109+
*/
110+
111+
112+
113+
return(0);
114+
}
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+

0 commit comments

Comments
 (0)