forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from aayush105/linkedlist
add linkedlist
- Loading branch information
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
template <class T> | ||
class Node | ||
{ | ||
public: | ||
T data; | ||
Node *next; | ||
}; | ||
|
||
template <class T> | ||
class Stack | ||
{ | ||
private: | ||
Node<T> *top; | ||
T item; | ||
|
||
public: | ||
Stack() | ||
{ | ||
top = NULL; | ||
} | ||
|
||
void push() | ||
{ | ||
cout << "Enter a data to push: "; | ||
cin >> item; | ||
Node<T> *newNode = new Node<T>; | ||
newNode->data = item; | ||
|
||
newNode->next = top; | ||
top = newNode; | ||
} | ||
|
||
void pop() | ||
{ | ||
if (top != NULL) | ||
{ | ||
Node<T> *temp = top; | ||
top = top->next; | ||
cout << "Popped: " << temp->data << endl; | ||
delete temp; | ||
} | ||
else | ||
{ | ||
cout << "Stack is Empty" << endl; | ||
} | ||
} | ||
|
||
void print() | ||
{ | ||
Node<int> *temp = top; | ||
while (temp != NULL) | ||
{ | ||
cout << temp->data << " "; | ||
temp = temp->next; | ||
} | ||
cout << endl; | ||
} | ||
}; | ||
|
||
void menu() | ||
{ | ||
cout << "------------------" << endl; | ||
cout << "MAIN MENU" << endl; | ||
cout << "1. Push" << endl; | ||
cout << "2. Pop" << endl; | ||
cout << "3. Display" << endl; | ||
cout << "4. Exit" << endl; | ||
cout << "Choose your choice: "; | ||
} | ||
|
||
int main() | ||
{ | ||
Stack<int> st; | ||
bool status = true; | ||
int choice; | ||
|
||
do | ||
{ | ||
menu(); | ||
|
||
cin >> choice; | ||
|
||
switch (choice) | ||
{ | ||
case 1: | ||
st.push(); | ||
break; | ||
|
||
case 2: | ||
st.pop(); | ||
|
||
break; | ||
|
||
case 3: | ||
st.print(); | ||
break; | ||
|
||
case 4: | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} while (choice != 4); | ||
|
||
return 0; | ||
} |