Skip to main content

stack

Introduction:

The stack is a linear data structure. A stack can be conceptualized by a stack of dishes, kept one upon another. As the dish which is at the top has to be removed at the first to get the depth of the stack, the stack is called a first in last out or last in first out structure. This is abbreviated as LIFO.

A stack, as it can be imagined as a one-side open tube, has one opening only and therefore it has to store this opening as a pointer when it is implemented. This pointer is named "top".

A stack has 5 normal functions to work with. It has a peek function which helps to look at the top of the stack and know the value. It has a push function to push a value from the top. And then there is a pop function which deletes the top value each time it is called. The other two functions are rather technical as such isempty() is a standard function to check whether the stack is empty or not and the isfull() is a similar one to check whether the stack is full or not.

#include <stdio.h>
#include <stdlib.h>
#define SIZE  10   //observe how macro defination is used
int count=0;          //global initialisation of count to count no of nodes
//define the type you want to use
typedef struct node{
int value;
struct stack *next;
struct stack *prev;
}stack;
//defining the global bottom and top. Because of the LIFO structure, top is the tail in linked list
stack *bottom=NULL;
stack *top=NULL;
//createnode function as always for creating each node
stack *createnode(int a)
{
stack *newstack;
newstack=(stack*)calloc(1,sizeof(stack));
newstack->value=a;
newstack->prev=NULL;
newstack->next=NULL;
return newstack;
}
//isempty function. checks if the stack is empty
int isempty()
    {
       if(count==0)
       {return 1;}
       else
       {return 0;}
    }
//isfull function. checks if the stack is full
int isfull()
    {
    if(count==SIZE)
    {return 1;}
    else
    {return 0;}
    }
//push, like the addnode function in list. pushes a value each time on top of the stack
void push(int a) //void as no need to return
{   if(isfull()==1) //first check if full
   {
    printf("overflow condition: stack full");
   }
else                // this is the portion which does the push
   {count+=1;           //count for how many nodes we are putting
stack *newstack = createnode(a);
if(bottom==NULL)          //if bottom is null, then make it head and tail
{
bottom=newstack;
top=bottom;
}
else                               // if the linked list has some nodes already, then this will work
{
top->next=newstack;
newstack->prev=top;
top=newstack;
}
   }
}
int peek()                     //peek to see the value on the top
   {
printf("%d\n",top->value);
   }
int pop()                    //pop function to delete the top node
    {   if(isempty()==1)             //check if there is at all any node, if not say it is underflow
        {
        printf("underflow condition:empty stack");
        }
        else                               //if there is only one node present, then this block to be used, because its           {if(count==1)             //not same as deleting 1 node with more than 2 noded linked list
        {   int c;
        c=top->value;
        top=NULL;
        printf("popped value is:%d\n",c);
        count=count-1;
        }
        else                             //deleting node from a linked list with more than 2 nodes
    {int c;
    c=top->value;
top=top->prev;
top->next=NULL;
printf("popped value is:%d\n",c);
count=count-1;
    }
       }
    }
// driver code , the main function
int main(void) {
stack ruby; int i;               // initiating a stack, and the integer i for looping
for(i=0;i<12;i++)
{push(i);
printf("%d\n",count);
}
isfull();
pop();
pop();
pop();
pop();
pop();
pop();
pop();
pop();
pop();
pop();
pop();
int c=isempty();
printf("%d",c);
printf("%d",count);
return 0;
}
//this below is the output from https://ideone.com/.
output:
  • Success #stdin #stdout 0s 9424KB
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    overflow condition: stack full10
    overflow condition: stack full10
    popped value is:9
    popped value is:8
    popped value is:7
    popped value is:6
    popped value is:5
    popped value is:4
    popped value is:3
    popped value is:2
    popped value is:1
    popped value is:0
    underflow condition:empty stack10


If you want to check some application of stack, please look here monk and prisoner of azkaban problem(application of stack). Also one of the standard stack application is: infix to postfix and vice versa.
For deeper understanding of data structures and algorithm, I recommend reading this following book:

Caution/disclaimer: This post only depicts a idea for the programs, and are not fit for all the marginal cases like values above 10^6, inputs other than expected formats and some other things. So please take just the idea from the codes and try to correct this more. You can mail me better versions too. 

Comments

Popular posts from this blog

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme