0% found this document useful (0 votes)
53 views3 pages

7.1 101. Stack LL C PDF

This C program implements a stack using a linked list. It defines a Node struct with data and next pointer fields. A top pointer points to the head of the list. Functions are defined to push nodes onto the stack by allocating memory and linking to the top, pop nodes off by unlinking and returning the data from the top node, and display the stack by traversing from top to bottom and printing data. The main function demonstrates pushing 3 nodes, displaying the stack, popping a node and printing its data.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

7.1 101. Stack LL C PDF

This C program implements a stack using a linked list. It defines a Node struct with data and next pointer fields. A top pointer points to the head of the list. Functions are defined to push nodes onto the stack by allocating memory and linking to the top, pop nodes off by unlinking and returning the data from the top node, and display the stack by traversing from top to bottom and printing data. The main function demonstrates pushing 3 nodes, displaying the stack, popping a node and printing its data.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stack using Linked List

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
}*top=NULL;

void push(int x)
{
struct Node *t;
t=(struct Node*)malloc(sizeof(struct Node));

if(t==NULL)
printf("stack is full\n");
else
{
t->data=x;
t->next=top;
top=t;
}

int pop()
{
struct Node *t;
int x=-1;

if(top==NULL)
printf("Stack is Empty\n");
else
{
t=top;
top=top->next;
x=t->data;
free(t);
}
return x;
}

void Display()
{
struct Node *p;
p=top;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}

int main()
{
push(10);
push(20);
push(30);

Display();

printf("%d ",pop());

return 0;
}

You might also like