0% found this document useful (0 votes)
9 views

Experiment No 3

Uploaded by

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

Experiment No 3

Uploaded by

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

Experiment No-3

Object :- Write a program to implement stack using array

/* C++ program to implement basic stack

operations */

#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack {

int top;

public:

int a[MAX]; // Maximum size of Stack

Stack() { top = -1; }

bool push(int x);

int pop();

int peek();

bool isEmpty();

};

bool Stack::push(int x)

{
if (top >= (MAX - 1)) {

cout << "Stack Overflow";

return false;

else {

a[++top] = x;

cout << x << " pushed into stack\n";

return true;

int Stack::pop()

if (top < 0) {

cout << "Stack Underflow";

return 0;

else {

int x = a[top--];

return x;

int Stack::peek()

if (top < 0) {
cout << "Stack is Empty";

return 0;

else {

int x = a[top];

return x;

bool Stack::isEmpty()

return (top < 0);

// Driver program to test above functions

int main()

class Stack s;

s.push(10);

s.push(20);

s.push(30);

cout << s.pop() << " Popped from stack\n";

//print top element of stack after popping

cout << "Top element is : " << s.peek() << endl;


//print all elements in stack :

cout <<"Elements present in stack : ";

while(!s.isEmpty())

// print top element in stack

cout << s.peek() <<" ";

// remove top element in stack

s.pop();

return 0;

OUTPUT :-

10 pushed into stack

20 pushed into stack

30 pushed into stack

30 Popped from stack

Top element is : 20

Elements present in stack : 20 10


Object :- Write a program to implement stack using Lisklist

#include<stdio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *next;

};

struct Node *head = NULL;

void push(int val)

//create new node

struct Node *newNode = malloc(sizeof(struct Node));

newNode->data = val;

//make the new node points to the head node

newNode->next = head;

//make the new node as head node

//so that head will always point the last inserted data

head = newNode;

}
void pop()

//temp is used to free the head node

struct Node *temp;

if(head == NULL)

printf("Stack is Empty\n");

else

printf("Poped element = %d\n", head->data);

//backup the head node

temp = head;

//make the head node points to the next node.

//logically removing the node

head = head->next;

//free the poped element's memory

free(temp);

//print the linked list


void display()

struct Node *temp = head;

//iterate the entire linked list and print the data

while(temp != NULL)

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

temp = temp->next;

printf("NULL\n");

int main()

push(10);

push(20);

push(30);

printf("Linked List\n");

display();

pop();

printf("After the pop, the new linked list\n");

display();

pop();

printf("After the pop, the new linked list\n");


display();

return 0;

OUTPUT:-

Linked List

30->20->10->NULL

Poped element = 30

After the pop, the new linked list

20->10->NULL

Poped element = 20

After the pop, the new linked list

10->NULL

You might also like