Stack Linked List Example Program Using Functions in C


Definition

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Stack Linked List Operations

  • Push data in linked list Stack top position
  • Pop data in linked list Stack based top postion
  • Display Nodes in Linked List Stack
  • Count Nodes in Linked List Stack

Stack Linked List Example C Program

/* Stack Program, Singly Linked List Example - Example Program Using Functions in C*/
/* Data Structure Programs,Stack Programs,C Linked List Examples */
/* Stack Singly Linked List Example - Push,Pop,Display and Count Operations*/

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

#define MAX_SIZE 5

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

void push();
void display();
void pop();
int count();

typedef struct node DATA_NODE;

DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;

int main() {
  int option = 0;

  printf("Stack Linked List Example - All Operations\n");

  while (option < 5) {

    printf("\nOptions\n");
    printf("1 : Push into Linked List Stack\n");
    printf("2 : Pop from Linked List Stack\n");
    printf("3 : Display Linked List Stack\n");
    printf("4 : Count Linked List Stack\n");
    printf("Others : Exit()\n");
    printf("Enter your option:");
    scanf("%d", &option);
    switch (option) {
      case 1:
        push();
        break;
      case 2:
        pop();
        break;
      case 3:
        display();
        break;
      case 4:
        count();
        break;
      default:
        break;
    }
  }

  return 0;
}

void push() {
  int pos;
  pos = count();
  if (pos == MAX_SIZE) {
    printf("\nStack Linked List Is Full \n");
  } else {
    printf("\nEnter Element for Push Linked List : \n");
    scanf("%d", &data);

    temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));

    temp_node->value = data;

    if (first_node == 0) {
      first_node = temp_node;
    } else {
      head_node->next = temp_node;
    }
    temp_node->next = 0;
    head_node = temp_node;
    fflush(stdin);
  }
}

void pop() {
  int pos, i = 0;
  pos = count();
  temp_node = first_node;

  if (pos > 0) {
    if (pos == 1) {
      temp_node = temp_node -> next;
      first_node = temp_node;
      printf("\n Pop from Linked List Successfully \n\n");
    } else {
      while (temp_node != 0) {
        if (i == (pos - 1)) {
          prev_node->next = temp_node->next;
          head_node = prev_node;
          printf("\n Pop from Linked List Successfully \n\n");
          break;
        } else {
          i++;
          prev_node = temp_node;
          temp_node = temp_node -> next;
        }
      }
    }
  } else
    printf("\Empty Linked List Stack\n\n");
}

void display() {
  int count = 0;
  temp_node = first_node;
  printf("\nDisplay Stack : Linked List : \n");
  while (temp_node != 0) {
    printf("# %d # ", temp_node->value);
    count++;
    temp_node = temp_node -> next;
  }
  printf("\nNo Of Items In Linked List Stack : %d\n", count);
}

int count() {
  int count = 0;
  temp_node = first_node;
  while (temp_node != 0) {
    count++;
    temp_node = temp_node -> next;
  }
  printf("\nNo Of Items In Linked List Stack : %d\n", count);
  return count;
}

Sample Ouput:

Stack Linked List Example - All Operations

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 0

Enter Element for Push Linked List :
11

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 1

Enter Element for Push Linked List :
22

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 2

Enter Element for Push Linked List :
33

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 3

Enter Element for Push Linked List :
44

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 33 # # 44 #
No Of Items In Linked List Stack : 4

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:4

No Of Items In Linked List Stack : 4

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:2

No Of Items In Linked List Stack : 4

 Pop from Linked List Successfully


Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 33 #
No Of Items In Linked List Stack : 3

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:2

No Of Items In Linked List Stack : 3

 Pop from Linked List Successfully


Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 2

Enter Element for Push Linked List :
899

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 899 #
No Of Items In Linked List Stack : 3

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:5