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

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

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