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

Ds Unit 4

The document provides an overview of stacks, describing their properties, operations (push, pop, peek), and applications in programming. It includes a C program for stack operations using arrays and discusses recursion, its applications, and examples like the Tower of Hanoi. Additionally, it highlights the importance of termination conditions in recursion and compares recursive and iterative solutions.

Uploaded by

ryanyours8
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)
1 views5 pages

Ds Unit 4

The document provides an overview of stacks, describing their properties, operations (push, pop, peek), and applications in programming. It includes a C program for stack operations using arrays and discusses recursion, its applications, and examples like the Tower of Hanoi. Additionally, it highlights the importance of termination conditions in recursion and compares recursive and iterative solutions.

Uploaded by

ryanyours8
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

Stack

1. Stack is an ordered list in which, insertion and deletion can be performed only at one
end that is called top.
2. Stack is a recursive data structure having pointer to its top element.
3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which
is inserted first in the stack, will be deleted last from the stack.
Applications of Stack
1. Recursion
2. Expression evaluations and conversions
3. Parsing
4. Browsers
5. Editors
6. Tree Traversals
Operations on Stack
There are various operations which can be performed on stack
1. Push : Adding an element onto the stack
2. Pop : Removing an element from the stack
3. Peek : Look all the elements of stack without removing them.
Top and its value :

Top position Status of stack

-1 Empty

0 Only one element in the stack

N-1 Stack is full

N Overflow

Array implementation of Stack


In array implementation, the stack is formed by using the array. All the operations
regarding the stack are performed using arrays. Lets see how each operation can be
implemented on the stack using array data structure.
Adding an element onto the stack (push operation)
Adding an element into the top of the stack is referred to as push operation. Push operation
involves following two steps.
1. Increment the variable Top so that it can now refere to the next memory location.
2. Add element at the position of incremented top. This is referred to as adding new
element at the top of the stack.
Stack is overflown when we try to insert an element into a completely filled stack therefore,
our main function must always avoid stack overflow condition.

Deletion of an element from a stack (Pop operation)


Deletion of an element from the top of the stack is called pop operation. The value of the
variable top will be incremented by 1 whenever an item is deleted from the stack. The top
most element of the stack is stored in an another variable and then the top is decremented
by 1. the operation returns the deleted value that was stored in another variable as the
result.
The underflow condition occurs when we try to delete an element from an already empty
stack.
C program
1. #include <stdio.h>
2. int stack[100],i,j,choice=0,n,top=-1;
3. void push();
4. void pop();
5. void show();
6. void main ()
7. {
8.
9. printf("Enter the number of elements in the stack ");
10. scanf("%d",&n);
11. printf("*********Stack operations using array*********");
12.
13. printf("\n----------------------------------------------\n");
14. while(choice != 4)
15. {
16. printf("Chose one from the below options...\n");
17. printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
18. printf("\n Enter your choice \n");
19. scanf("%d",&choice);
20. switch(choice)
21. {
22. case 1:
23. {
24. push();
25. break;
26. }
27. case 2:
28. {
29. pop();
30. break;
31. }
32. case 3:
33. {
34. show();
35. break;
36. }
37. case 4:
38. {
39. printf("Exiting....");
40. break;
41. }
42. default:
43. {
44. printf("Please Enter valid choice ");
45. }
46. };
47. }
48. }
49.
50. void push ()
51. {
52. int val;
53. if (top == n )
54. printf("\n Overflow");
55. else
56. {
57. printf("Enter the value?");
58. scanf("%d",&val);
59. top = top +1;
60. stack[top] = val;
61. }
62. }
63.
64. void pop ()
65. {
66. if(top == -1)
67. printf("Underflow");
68. else
69. top = top -1;
70. }
71. void show()
72. {
73. for (i=top;i>=0;i--)
74. {
75. printf("%d\n",stack[i]);
76. }
77. if(top == -1)
78. {
79. printf("Stack is empty"); }}

Recursion in C-Recursion is the process which comes into existence when a function
calls a copy of itself to work on a smaller problem. Any function which calls itself is called
recursive function, and such function calls are called recursive calls. Recursion involves
several numbers of recursive calls. However, it is important to impose a termination
condition of recursion. Recursion code is shorter than iterative code however it is difficult to
understand.

Recursion cannot be applied to all the problem, but it is more useful for the tasks that can
be defined in terms of similar subtasks. For Example, recursion may be applied to sorting,
searching, and traversal problems.Generally, iterative solutions are more efficient than
recursion since function call is always overhead. Any problem that can be solved recursively,
can also be solved iteratively. However, some problems are best suited to be solved by the
recursion, for example, tower of Hanoi, Fibonacci series, factorial finding, etc.

1. #include <stdio.h>
2. int fact (int);
3. int main()
4. {
5. int n,f;
6. printf("Enter the number whose factorial you want to calculate?");
7. scanf("%d",&n);
8. f = fact(n);
9. printf("factorial = %d",f);
10. }
11. int fact(int n)
12. {
13. if (n==0)
14. {
15. return 0;
16. }
17. else if ( n == 1)
18. {
19. return 1;
20. }
21. else
22. {
23. return n*fact(n-1);
24. }
25. }

The Tower of Hanoi is a mathematical game or puzzle. It consists of three


rods, and a number of disks of different sizes which can slide onto any rod. The puzzle
starts with the disks in a neat stack in ascending order of size on one rod, the smallest
at the top, thus making a conical shape as shown in below image.

The objective of the puzzle is to move the entire stack to another rod, obeying the
following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it
on top of another stack i.e. a disk can only be moved if it is the uppermost disk on
a stack.
3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of
moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of
disks.
#include<stdio.h>

void TOH(int,char,char,char);

void main()
{
int n;
printf("How many plates?");
scanf("%d",&n);
TOH(n,'A','B','C');
}

void TOH(int n,char x,char y,char z)


{
if(n>0)
{
TOH(n-1,x,z,y);
printf("\n%c -> %c",x,y);
TOH(n-1,z,y,x);}}

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