DEV Community

Cover image for Stack Using SLL for beginners
Suman Roy
Suman Roy

Posted on

Stack Using SLL for beginners

Let us write the code for implementing stack using linkedlist
Pre-requisites : brief knowledge of linkedlist operations.
Before we get started let us know what is Stack?
A stack is a data structure which follows the LIFO principle. It is abbreviation for Last-In-First-Out. Technically, the last item that is inserted into the data structure can be taken out first.
Applications for stack can be seen in real life, such as a stack of books, files, the cylindrical container for ping pong balls, the biscuit wrapper,etc.

The operations that can be performed on Stack data structure are push(insertion), pop(deletion) and peek(top element).

The code :
1.Defining the structure for our nodes

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int data;
    struct Node* next;
};
// global top pointer
struct Node* top=NULL;
Enter fullscreen mode Exit fullscreen mode

2.Writing print function for printing the elements
We traverse through the list from top until we reach null and print all the data for the nodes.

void print(){
    struct Node *traverse = top;
    while(traverse!=NULL){
        printf("%d ",traverse->data);
        traverse = traverse->next; // iterate
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

3.Addition(push) of new element(node) to our stack(list)
Pushing new element to stack is same as insertion of node in the beginning of the list.

void Push(int x){
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = x;
    newNode->next = NULL;
    if(top==NULL){
        top = newNode;
        print();
        return;
    }
    // link fix with first node
    newNode->next = top;
    top = newNode;
    printf("Stack after insertion : \n");
    print();
}
Enter fullscreen mode Exit fullscreen mode

4.Deletion(popping) element(node)from the stack(list)
Popping from stack is same as deleting node from the beginning of the list.

void Pop(){
    if(top == NULL){
        printf("Stack is empty!!\n");
        return ;
    }
    struct Node *temp = top->next;  // one node skipped i.e., deleted
    top->next = NULL;
    top = temp;
    printf("Stack after deletion: \n");
    print();
}
Enter fullscreen mode Exit fullscreen mode

5.Peek(show top) element(node)
Peeking is showing the top element of the array if it exists else the stack is empty

void peek(){
    if(top == NULL) printf("Stack empty!\n");
    else printf("Top element is: ",top->data);
}
Enter fullscreen mode Exit fullscreen mode

6.Now we write the main function and interface for the user interaction using switch case

int main(){
    top = NULL;
    int choice;
    printf("Enter 1 for push, 2 for pop, 3 for peek: ");
    scanf("%d",&choice);
    // switch statement
    switch(choice){
        case 1:
            int value;
            printf("Enter the value for insertion : ");
            scanf("%d",&value);
            Push(value);
            break;
        case 2:
            Pop();
            break;
        case 3:
            peek();
            break;
        default: 
                printf("Wrong choice!");
                break;

    }
    return 1;
}
Enter fullscreen mode Exit fullscreen mode

Now, run the program in your editor and make sure to try out different values of insertion and deletion to make sure the edge cases are being followed. If you find any bug, comment down below!
Thank you 👋

Top comments (0)