Emplement of Stack Using

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

#Design and Implement of stack using Array

#include <stdio.h>
#define SIZE 10
int stack[SIZE];
int top = -1;
void push(int value)
{
if(top<SIZE-1)
{
if (top < 0)
{
stack[0] = value;
top = 0;
}
else
{
stack[top+1] = value;
top++;
}
}
else
{
printf("Stackoverflow!!!!\n");
}
}
int pop()
{
if(top >= 0)
{
int n = stack[top];
top--;
return n;
}
}
int Top()
{
return stack[top];
}
int isempty()
{
return top<0;
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

int main()
{
push(6);
push(2);
display();
pop();
display();
return 0;
}

Output:
#Design and Implement of stack using Linked List

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *top;
void initialize()
{
top = NULL;
}
void push(int value)
{
node *tmp;
tmp = malloc(sizeof(node));
tmp -> data = value;
tmp -> next = top;
top = tmp;
}
int pop()
{
node *tmp;
int n;
tmp = top;
n = tmp->data;
top = top->next;
free(tmp);
return n;
}
int Top()
{
return top->data;
}
int isempty()
{
return top==NULL;
}

void display(node *head)


{
if(head == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", head -> data);
display(head->next);
}
}

int main()
{
initialize();
push(15);
push(30);
push(52);
printf("The top is %d\n",Top());
pop();
printf("The top after pop is %d\n",Top());
display(top);
return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy