Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

Data Structures in C - Stack

Posted on Nov. 10, 2017
data-structures-in-c-stack

In this part of Data Structures in C tutorial series, I will explain what is Stack and I will show you - how to write a Stack from scratch in C. Stack is one of the important data structures. It's the iterative Alternative of recursion. Often in Interviews, candidates are asked to replace a recursive function with the corresponding Stack iterative approach.

A Stack looks like as shown below.


The top of the Stack is pointed by a Top pointer. There are mainly three operations a Stack supports.

  • Push

  • Pop

  • Peek

The Push function pushes an element into the Top of the Stack and Pop function removes an element from the Top of the Stack. The Peek function gives you the element at the Top of the Stack without removing it from the Stack.

Apart from the above basic functions, a Stack should also give facility to the user to see if the Stack is Empty. If the user tries to Pop or Peek from an Empty Stack, the user should be reported with appropriate error message.

A Stack is implemented using a Linked List or an Array. If the Stack is implemented using an Array, the stack will have a maximum size. Therefore, if the user tries to push an element into a Stack which is already full, an appropriate error message should be shown to the user.

If we Push an element into the Stack as shown above, we get the Stack as shown below.


If we do a Pop operation on this Stack, we get back the original Stack. If we do a Peek operation in this Stack, it should return the element 3 keeping the Stack structure intact.

Below is the complete working code for Stack data structures in C using Linked List. I have taken elements as integer. You can modify the code to change the data types of the elements as per your requirement.

#include <stdio.h>
#include <stdlib.h>
struct Stack{
struct node *top;
};
struct node{
int v;
struct node *next;
};
struct Stack * createStack(){
struct Stack *s = (struct Stack*)malloc(sizeof(struct Stack));
s->top = NULL;
return s;
}
int isEmptyStack(struct Stack *s){
if(!s){
printf("Null pointer Exception!");
exit(1);
}
if(!s->top)
return 1;
return 0;
}
int pop(struct Stack *s){
if(!isEmptyStack(s)){
struct node *currTop = s->top;
s->top = s->top->next;
int v = currTop->v;
free(currTop);
return v;
}
printf("The Stack is Empty!");
return -1;
}
void push(struct Stack *s,int v){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->v = v;
newNode->next = s->top;
s->top = newNode;
return;
}
int peek(struct Stack *s){
if(!isEmptyStack(s)){
return s->top->v;
}
printf("The Stack is Empty!");
return -1;
}
int main()
{
struct Stack *s = createStack();
if(isEmptyStack(s)){
printf("Stack is Empty!\n");
}
push(s,2);
push(s,6);
push(s,5);
printf("Poped element from the Stack is: %d\n",pop(s));
push(s,9);
printf("Top element in the Stack is: %d",peek(s));
return 0;
}

If you have any doubt regarding the code or any other concept, please leave your queries in the comment section.

Sharing is Caring!

Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

GET FREE UPDATES


RECOMMENDED POSTS FOR YOU


profile image

Kaushik Baruah


ABOUT

My name is Kaushik Baruah and I am the chief blogger on this Blog and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects. You can follow me on Twitter, Facebook and Google+

GET FREE UPDATES

POPULAR POSTS

Copyright © 2016
About Us

My name is Kaushik Baruah and I am the chief blogger on this Blog and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects.

Get Free Updates