0% found this document useful (0 votes)
57 views5 pages

Stack Using Linked List 7

The document describes implementing a stack using a linked list with push() and display() functions. The push() function inserts an element into the stack and returns an error if the maximum capacity is reached. The display() function prints all elements of the stack with a newline at the end. Code for main(), push(), and display() functions is provided but push() and display() need to be completed. The execution results show all test cases passing with different inputs to the push() and display() functions.

Uploaded by

Riyaz Ratna
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)
57 views5 pages

Stack Using Linked List 7

The document describes implementing a stack using a linked list with push() and display() functions. The push() function inserts an element into the stack and returns an error if the maximum capacity is reached. The display() function prints all elements of the stack with a newline at the end. Code for main(), push(), and display() functions is provided but push() and display() need to be completed. The execution results show all test cases passing with different inputs to the push() and display() functions.

Uploaded by

Riyaz Ratna
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/ 5

S.No: 7 Exp.

Name: Implementation of Stack using Linked List - push() and display() operations Date:

I D: 20K61A0561     Page No:            


Aim:
Implementation of stack using linked list

Fil l the missing code in the functions push(int x) and display() in the below code.

The function push() inserts an element into the stack and it gives "stack overflow" error if the maximum capacity of the
stack is reached.

The function display() prints al l the elements of the stack.

Note: Do use printf() with '\n' at the end of display().

Source Code:

StackListMain1.c

#include "StackListPushDisplay.c"

int main() {

int op, x;

while(1) {
printf("1.Push 2.Display 3.Exit\n");

printf("Enter your option : ");

scanf("%d",&op);

switch(op) {

case 1:

printf("Enter element : ");

scanf("%d",&x);

push(x);
break;

case 2:

display();

break;

case 3:

exit(0);
}

}
Sasi Institute of Technology and Engineering (Autonomous)

StackListPushDisplay.c

#include <stdio.h>

#include <stdlib.h>

struct stack {

int data;

struct stack *next;

};

typedef struct stack *stk;

stk top = NULL;


stk push(int);

stk push(int x)
{

I D: 20K61A0561     Page No:            


struct stack*temp;

temp=(struct stack*)malloc(sizeof(struct stack));

if(temp==NULL)

printf("Stack is empty.\n");

else

if(top==NULL)

temp->data=x;

temp->next=NULL;

top=temp;

else

temp->data=x;

temp->next=top;

top=temp;

printf("Successfully pushed.\n");

void display();
void display()

struct stack* temp=top;

if(top==NULL)

printf("Stack is empty.\n");

else

printf("Elements of the stack are : ");

while(temp!=NULL)

printf("%d ",temp->data);

temp=temp->next;

Sasi Institute of Technology and Engineering (Autonomous)

printf("\n");

Execution Results - Al l test cases have succeeded!

Test Case - 1

User Output
1.Push 2.Display 3.Exit 1
Test Case - 1
Enter your option : 1

I D: 20K61A0561     Page No:            


Enter element : 1
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 2
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 3
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 4
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 5
Successfully pushed. 2
1.Push 2.Display 3.Exit 2
Enter your option : 2
Elements of the stack are : 5 4 3 2 1 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 5
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 4
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 3
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 2 Sasi Institute of Technology and Engineering (Autonomous)
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 1
Successfully pushed. 2
1.Push 2.Display 3.Exit 2
Enter your option : 2
Elements of the stack are : 1 2 3 4 5 5 4 3 2 1 3
1.Push 2.Display 3.Exit 3
Enter your option : 3

Test Case - 2
Test Case - 2

I D: 20K61A0561     Page No:            


User Output
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 23
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 43
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 23
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 54
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 65
Successfully pushed. 2
1.Push 2.Display 3.Exit 2
Enter your option : 2
Elements of the stack are : 65 54 23 43 23 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 23
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 43
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 55
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Sasi Institute of Technology and Engineering (Autonomous)

Enter your option : 1


Enter element : 87
Successfully pushed. 2
1.Push 2.Display 3.Exit 2
Enter your option : 2
Elements of the stack are : 87 55 43 23 65 54 23 43 23 3
1.Push 2.Display 3.Exit 3
Enter your option : 3

Test Case - 3

User Output
1.Push 2.Display 3.Exit 1
Test Case - 3
Enter your option : 1

I D: 20K61A0561     Page No:            


Enter element : 23
Successfully pushed. 1
1.Push 2.Display 3.Exit 1
Enter your option : 1
Enter element : 45
Successfully pushed. 2
1.Push 2.Display 3.Exit 2
Enter your option : 2
Elements of the stack are : 45 23 3
1.Push 2.Display 3.Exit 3
Enter your option : 3

Sasi Institute of Technology and Engineering (Autonomous)

You might also like