-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListStackcallByReferenceRIGHT.cpp
More file actions
86 lines (79 loc) · 2.05 KB
/
LinkedListStackcallByReferenceRIGHT.cpp
File metadata and controls
86 lines (79 loc) · 2.05 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>
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){
//cout<<endl<<"copy"<<&head<<endl;
if(head == NULL){
cout<<"Linked List is empty, cannot pop anything! "<<endl;
} else if(head->next == NULL){
//cout<<endl<<"Pop-condition 2"<<endl;
cout<<"Popped element is "<<endl<<head->data<<endl;
head = NULL;
} else {
//cout<<endl<<"Pop-condition 3"<<endl;
Node* link = head;
while(link->next->next != NULL){
link = link->next;
}
cout<<"Popped element is "<<endl<<link->next->data<<endl;
link->next = NULL;
}
}
int main(){
Node* head = new Node();
//cout<<endl<<"original"<<&head<<endl;
int array[] = {7, 5};
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;
if(head == NULL){
head = new Node();
}
push(head, numToPush);
printLinkedList(head);
} else if(choice == 2){
pop(head);
//printLinkedList(head);
} else {
cout<<endl<<"Exiting "<<endl;
break;
}
}
}