0% found this document useful (0 votes)
15 views29 pages

Stack pp2

Stack notes

Uploaded by

Young Uther
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)
15 views29 pages

Stack pp2

Stack notes

Uploaded by

Young Uther
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/ 29

Implementing a stack with a linked list:

➢In fact, a stack linked-list is much simpler than most linked-list


implementations:
➢it requires that we implement a linked-list where only the head node
or element can be removed, or popped, and a node can only be
inserted by becoming the new head node.
➢Using a linked list is one way to implement a stack so that it can
handle essentially any number of elements.
struct stack

int data;

struct stack *next;

stack
Push ():
➢operation both initializes an empty stack, and adds a new node to the stack and a
non-empty one.
➢It works by receiving a data value to push onto the stack, along with a target
stack, creating a new node by allocating memory for it, and then inserting it into
a linked list as the new head.
Pop ():
➢operation removes the head from the linked list, and assigns the
pointer to the head to the previous second node.
➢It check whether the list is empty before popping from it:
EXAMPLE PROGRAM TO IMPLEMENT A STACK USING A LINKED LIST

#include<iostream>

using namespace std;

struct node

int info;

struct node *ptr;

*top,*top1,*temp;
int topelement();

void push(int data);

void pop();

void empty();

void display();

void destroy();

void stack_count();

void create();
int count = 0;
int main()

int no, ch, e;

cout<<"\n 1 - Push";

cout<<"\n 2 - Pop";

cout<<"\n 3 - Top";

cout<<"\n 4 - Empty";

cout<<"\n 5 - Exit";

cout<<"\n 6 - Dipslay";

cout<<"\n 7 - Stack Count";

cout<<"\n 8 - Destroy stack";

create();
while (1)

cout<<"\n Enter choice :";

cin>>ch;

switch (ch)

case 1:

cout<<"Enter data :";

cin>>no;

push(no);

break;

case 2:

pop();

break;
case 3:

if (top == NULL)

cout<<"No elements in stack";

else

e = topelement();

cout<<"\n Top element is:"<<e;

break;

case 4:

empty();

break;

case 5:

exit(0);
case 6:

display();

break;

case 7:

stack_count();

break;

case 8:

destroy();

break;

default :

cout<<"Wrong choice, Please enter correct choice ";

break; } } }
/* Create empty stack */

void create()

top = NULL;

/* Count stack elements */

void stack_count()

cout<<"\n No. of elements in stack are:"<<count;

}
void push(int data)

if (top == NULL)

top =new node;

top->ptr = NULL;

top->info = data;

}
else

temp =new node;

temp->ptr = top;

temp->info = data;

top = temp;

count++;

}
/* Display stack elements */

void display()

top1 = top;

if (top1 == NULL)

cout<<"Stack is empty";

return;

}
while (top1 != NULL)
{
cout<<top1->info<<endl;
top1 = top1->ptr;
}
}
/* Pop Operation on stack */

void pop()

top1 = top;

if (top1 == NULL)

cout<<"\n Error : Trying to pop from empty stack";

return;

}
else

top1 = top1->ptr;

cout<<"\n Popped value is:"<<top->info;

delete top;

top = top1;

count--;

/* Return top element */

int topelement()

return(top->info);

}
/* Check if stack is empty or not */

void empty()

if (top == NULL)

cout<<"\n Stack is empty";

else

cout<<"\n Stack is not empty contains elements"<<count;

}
/* Destroy entire stack */

void destroy()

top1 = top;

while (top1 != NULL)

top1 = top->ptr;

delete top;

top = top1;

top1 = top1->ptr;

delete top1;
top = NULL;

cout<<"\n All stack elements destroyed";

count = 0;

}
Applications of Stack:

➢Stack is used directly and indirectly in the following fields:

• To evaluate the expressions (postfix, infinix,prefix)


• To keep the page-visited history in a Web browser
• To perform the undo sequence in a text editor
• Used in recursion
• To pass the parameters between the functions in a C program
• Can be used as an auxiliary data structure for implementing algorithms
• Can be used as a component of other data structures
RECURSION

• Any programming language allows any of its functions to call itself multiple times in a program. Here, any
function that happens to call itself again and again (directly or indirectly), unless the program satisfies some
specific condition/subtask is called a recursive function.
• Recursion is the technique of making a function call itself. This technique provides a way to break
complicated problems down into simple problems which are easier to solve.
PROGRAM TO FIND SUM OF NATURAL NUMBERS USING RECURSION

#include <iostream>

using namespace std;

int sum(int n);

int main() {

int number, result;

cout<<"Enter a positive integer:";

cin>>number;

result = sum(number);

cout<<result;

return 0;

}
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
FIND FACTORIAL OF A NUMBER USING RECURSION

#include<iostream>

using namespace std;

int fact(int a); /* Definition of Function */

int main()

int number, result;

cout<<"Please enter a non-negative number here:";

cin>>number;

result = fact(number); /* Function Calling in a Normal way */

cout<<"factotial of the number is:"<<result;


int fact(int a) /* Definition of Function */

int x=1;

if(a <= 0)

return(1);

else

x = a * fact(a-1); /* Function Call Recursively as the fact() calls itself in the program */

return(x);

}
Advantages and Disadvantages of Recursion

• Recursion makes program elegant. However, if performance is vital, use loops


instead as recursion is usually much slower.

• That being said, recursion is an important concept. It is frequently used in data


structure and algorithms For example, it is common to use recursion in problems
such as tree traversal.

You might also like