Loading...

Build Stack Data Structure Implementation using Linked List

View: 366    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

Write a program to implement a stack using linked list. In my last post, I have explained the implementation of a stack data structure using an array. In this post, I’ll explain how to implement a stack  data structure using linked list.

Stack Data Structure

Let’s revise some of the terminologies of a stack.

i) Stack worked on LIFO (Last In First Out) Principle. It means insertion and deletion are allowed only at one end. The element which added last must be the first element to be removed.

ii) In Stack, Insertion operation is called Push and deletion operation is called Pop . During push operation if memory is not available then it’s termed as StackOverflow. During pop operation if a stack is empty, then it’s called StackUnderflow

Program to Implement Stack using Linked List

I assumed you have a basic understand of linked list. If you are not familiar with linked list data structure then check my previous posts on linked list.

#include <stdio.h>
#include <stdlib.h>
 
/* Create a node which contains data and next pointer link */
 
struct node{
 
 int data;
 struct node *link;
};
 
struct node *head;
 
void push(int data){
 
        /* Allocate memory */
 
 struct node *newNode = (struct node*)malloc(sizeof(struct node));
 
 /* If memory is full */
 
 if (newNode == NULL) {
 
   printf("Stack Overflow \n");
   return;
 
 }
 
 /* Create new node */
 
 newNode->data = data;
 newNode->link = head;
 
 head = newNode;
 
}
 
void pop() {
 
 /* If there is no element to pop */
 
 if(head == NULL) {
 
   printf("Stack Underflow");
   exit(0);
 
 }
 
 struct node *p = head;
 
         int data;
 data = p->data;
 
 /* Points to next link */
 
 head = head->link;
 free(p);
 
 printf("Popped element is %d\n",data);
}
 
main() {
 
       /*Initially head is assigned null. */
 
 head = NULL;
 
 push(4);
 push(6);
 pop();
 push(8);
 pop();
 
}

Output

Popped element is 6
Popped element is 8

Explanation

In this program first, we pushed element 4 and 6 in a stack. Stack worked on LIFO(Last In First Out) principle. Then a pop method is called, so 6 is popped out. Again we push element 8 and then we called a pop method which popped 8 from a stack.

Build Stack Data Structure Implementation using Linked List

Write a program to implement a stack using linked list. In my last post, I have explained the implementation of a stack data structure using an array. In this post, I’ll explain how to implement a stack data structure using linked list.

Posted on 05-10-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Twitter-like Hashtag Function in PHP

    Twitter-like Hashtag Function in PHP

    View: 0    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    If you ever want a function to hashtag and style words within a string which has '#' next to it like twitter. This piece of code will help

  • PSITS Automated Voting System

    PSITS Automated Voting System

    View: 24    Download: 3   Comment: 0

    Category: Php&mySql     Fields: none

    A free sourcecode for PSITS Automated Voting System develop in PHP programming language. The purposed of the system is to automate the process of voting and maintain the quality of data

  • PHP-University Application System

    PHP-University Application System

    View: 30    Download: 6   Comment: 0

    Category: Php&mySql     Fields: none

    Greeting from Malawi the warm heart of Africa. I developed this university registration system project just to share with the people who can make use of the project like this and at the same time for others like students who can learn from it. This...

  • School Event Management System in PHP/MSQLi

    School Event Management System in PHP/MSQLi

    View: 22    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This School Event Management System can create school events such as Volleyball games, Basketball, Cultural presentation, Election of school officers etc . During school election Instead of having a compile list of candidates and voters this system...

  • Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    View: 27    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This reservation system has the ability to help its customers find available rooms, cottages and even function hall for their convenience . And in here, they will also have the idea of the room rates where they can quickly reserve for their family...

  • Activity log

    Activity log

    View: 20    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Simple program to track user's activity log-in time and online/offline status.

  • Electricks - eCommerce

    Electricks - eCommerce

    View: 22    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Electricks is an e-commerce website built using PHP, Bootstrap and some jQuery plugins. It has all the major functionalities of a typical e-shop. This project was used to present in Southern Luzon State University - Lucban. Feel free to use this code...

  • Simple Email App Using PHP

    Simple Email App Using PHP

    View: 18    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Creating a simple PHP script using mail function for sending out emails.

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd