Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated version of stack linked list #2036

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 39 additions & 27 deletions data_structures/stack_using_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
#include <iostream>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <iostream>
#include <iostream> /// for I/O operations


struct node {
struct node
{
int val;
node *next;
};

node *top_var;

void push(int x) {
void push(int x)
{
node *n = new node;
n->val = x;
n->next = top_var;
top_var = n;
}

void pop() {
if (top_var == nullptr) {
void pop()
{
if (top_var == nullptr)
{
std::cout << "\nUnderflow";
} else {
}
else
{
node *t = top_var;
std::cout << "\n" << t->val << " deleted";
std::cout << "\n"
<< t->val << "Deleted";
top_var = top_var->next;
delete t;
}
}

void show() {
void show()
{
node *t = top_var;
while (t != nullptr) {
while (t != nullptr)
{
std::cout << t->val << "\n";
t = t->next;
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @brief Main function
* @returns 0 on exit
*/

int main() {
int main()
{
int ch = 0, x = 0;
do {
do
{
std::cout << "\n0. Exit or Ctrl+C";
std::cout << "\n1. Push";
std::cout << "\n2. Pop";
std::cout << "\n3. Print";
std::cout << "\nEnter Your Choice: ";
std::cin >> ch;
switch (ch) {
case 0:
break;
case 1:
std::cout << "\nInsert : ";
std::cin >> x;
push(x);
break;
case 2:
pop();
break;
case 3:
show();
break;
default:
std::cout << "Invalid option!\n";
break;
switch (ch)
{
case 0:
break;
case 1:
std::cout << "\nInsert : ";
std::cin >> x;
push(x);
break;
case 2:
pop();
break;
case 3:
show();
break;
default:
std::cout << "Invalid option!\n";
break;
Comment on lines +45 to +73
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use tests

}
} while (ch != 0);

Expand Down