Implement Stack in C Singly Linked List

Introduction

Singly linked list is a single-direction data structure.

  • An element in the list is called a node
  • Typically, a node is a C structure includes 2 variables: the value it hold, and a pointer to the next node
  • By singly, it means when we traverse through linked list, we could only do it from one direction, which is from the first node (head) to the last. We cannot simply go back to the last node without iterating the whole list

Implementation

To implement stack in C singular linked list:

  1. Create a structure for node, which include a pointer pointing to the next node
  2. Create a function to add new node to the list. We could add a new node to a linked list at any position, but in stack structure, we will append the new node to the list
  3. Create a function to delete a node with specific value or at specific position. In stack structure, we will delete the last node and return the deleted node

Gist Example

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yu-Ming, CHANG (he/him)

Yu-Ming, CHANG (he/him)

I enjoy the positive mind flow when writing code to solve a problem. This is my journey to become a software developer