-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListStackcallByReferenceWRONG.cpp
More file actions
96 lines (83 loc) · 2.07 KB
/
LinkedListStackcallByReferenceWRONG.cpp
File metadata and controls
96 lines (83 loc) · 2.07 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
87
88
89
90
91
92
93
94
95
96
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void printLinkedList(Node* head){
Node* link = head;
while(link != NULL){
cout<<link->data<<" ";
link = link->next;
}
cout<<endl;
}
void push(Node* head, int num){
Node* link = head;
while(link->next != NULL){
link = link->next;
}
link->next = new Node;
link = link->next;
link->data = num;
}
void pop(Node* head){
Node* link = head;
if(link == NULL){
cout<<"Linked List is empty, cannot pop anything! "<<endl;
} else if(link->next == NULL){
delete head;
head = NULL;
} else {
Node* temp = head;
while(temp->next != NULL){
cout<<"temp--inside ";
printLinkedList(temp);
cout<<"link--inside ";
printLinkedList(link);
link = temp;
temp = temp->next;
}
cout<<"temp--outside ";
printLinkedList(temp);
cout<<"link--outside ";
printLinkedList(link);
link->next = NULL;
delete temp;
temp = NULL;
}
}
int main(){
Node* head = new Node();
int array[] = {7,5,2,1,9,7};
int n = (sizeof(array))/sizeof(array[0]);
Node* link = head;
for(int i = 0; i < n; i++){
link->data = array[i];
if(i < (n-1)){
link->next = new Node();
link = link->next;
}
}
int choice = 0;
printLinkedList(head);
cout<<endl;
cout<<"^ This is the Linked List "<<endl<<endl;
while(choice != 3){
cout<<"Enter 1 to push a number , 2 to pop and 3 to exit "<<endl;
cin>>choice;
if(choice == 1){
int numToPush;
cout<<"Enter a number to push ";
cin>>numToPush;
push(head, numToPush);
printLinkedList(head);
} else if(choice == 2){
pop(head);
printLinkedList(head);
} else {
cout<<endl<<"Exiting "<<endl;
break;
}
}
}