Answers for "stack using linked list"

C++
1

implement stack using link list in c

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

struct node
{
    int data;
    struct node *next;
};
typedef struct node node;

node *top;

void initialize()
{
    top = NULL;
}

void push(int value)
{
    node *tmp;
    tmp = malloc(sizeof(node));
    tmp -> data = value;
    tmp -> next = top;
    top = tmp;
}

int pop()
{
    node *tmp;
    int n;
    tmp = top;
    n = tmp->data;
    top = top->next;
    free(tmp);
    return n;
}

int Top()
{
    return top->data;
}

int isempty()
{
    return top==NULL;
}

void display(node *head)
{
    if(head == NULL)
    {
        printf("NULL\n");
    }
    else
    {
        printf("%d\n", head -> data);
        display(head->next);
    }
}

int main()
{
    initialize();
    push(10);
    push(20);
    push(30);
    printf("The top is %d\n",Top());
    pop();
    printf("The top after pop is %d\n",Top());
    display(top);
    return 0;
}
Posted by: Guest on September-17-2020
0

stack implementation using linked list in c

/*
 * C Program to Implement a Stack using Linked List
 */
#include <stdio.h>
#include <stdlib.h>
 
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;
 
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
 
int count = 0;
 
void main()
{
    int no, ch, e;
 
    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Dipslay");
    printf("\n 7 - Stack Count");
    printf("\n 8 - Destroy stack");
 
    create();
 
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
 
        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            push(no);
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)
                printf("No elements in stack");
            else
            {
                e = topelement();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            stack_count();
            break;
        case 8:
            destroy();
            break;
        default :
            printf(" Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}
 
/* Create empty stack */
void create()
{
    top = NULL;
}
 
/* Count stack elements */
void stack_count()
{
    printf("\n No. of elements in stack : %d", count);
}
 
/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }
    count++;
}
 
/* Display stack elements */
void display()
{
    top1 = top;
 
    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }
 
    while (top1 != NULL)
    {
        printf("%d ", top1->info);
        top1 = top1->ptr;
    }
 }
 
/* Pop Operation on stack */
void pop()
{
    top1 = top;
 
    if (top1 == NULL)
    {
        printf("\n Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}
 
/* Return top element */
int topelement()
{
    return(top->info);
}
 
/* Check if stack is empty or not */
void empty()
{
    if (top == NULL)
        printf("\n Stack is empty");
    else
        printf("\n Stack is not empty with %d elements", count);
}
 
/* Destroy entire stack */
void destroy()
{
    top1 = top;
 
    while (top1 != NULL)
    {
        top1 = top->ptr;
        free(top);
        top = top1;
        top1 = top1->ptr;
    }
    free(top1);
    top = NULL;
 
    printf("\n All stack elements destroyed");
    count = 0;
}
Posted by: Guest on September-09-2020
0

stack using linked list

/// Stack using Linked List
/// we are using single Linked List and manage using head pointer not tail
#include <bits/stdc++.h>
using namespace std;

/*****************************/

// Template T is generic class which work for any datatype.
template <typename T>

class Node {
 public:
  T data;
  Node<T> *next;

  Node(T data) : data(data), next(NULL) {}
};

//--------------------------------

template <typename T>

class Stack {
  Node<T> *head;
  int size{0};

 public:
  Stack() {
    head = NULL;
    size = 0;
  }

  //-------------------------------- getSize()   - O(1)

  int getSize() { return size; }

  //---------------------------------- isEmpty() - O(1)
  
  bool isEmpty() {
    if (head == NULL) {
      return true;
    }
    return false;
  }
  //---------------------------------- push() - O(1)

  void push(int data) {
    Node<T> *temp = new Node<T>(data);
    temp->next = head;
    head = temp;
    size++;
  }

  //--------------------------------- pop() - O(1)
  void pop() {
    if (head == NULL) {
      cout << "==============" << endl;
      cout << "STACK EMPTY!!!" << endl;
      cout << "==============" << endl;
      return;
    }

    Node<T> *temp = head;
    head = head->next;

    /// free node - isolation step
    temp->next = NULL;
    delete temp;
    size--;
  }

  //---------------------------------- top_element()  - O(1)

  T top() {
    if (head == NULL) {
      cout << "==============" << endl;
      cout << "STACK EMPTY!!!" << endl;
      cout << "==============" << endl;
      return 0;
    }

    return head->data;
  }
};

/*****************************/

int main() {
  
  Stack<int> s;
  s.push(10);
  s.push(20);
  s.push(30);
  s.push(40);
  s.push(50);


  cout << s.getSize() << endl;
  cout << s.top() << endl;
  s.pop();

}
Posted by: Guest on May-04-2021

Browse Popular Code Answers by Language