Stacks are linear data structures which means the data is stored in what looks like a line (although vertically). In simple words we can say
A stack is a last in, first out (LIFO) abstract data type and data structure.
Basic usage of stack at the Architecture level is as a means of allocating and accessing memory.
Stack
file:///root/Desktop/Stack_4.jpg
We can only perform two fundamental operations on a stack: push and pop.
The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.
A stack is a restricted data structure, because only a small number of operations are performed on it.
Let us 'C' code for implementing a stack using Singly Linked Lists.
view plainprint?
1. #include
2. #include
3.
4. //Structure containing a Data part & a
5. //Link part to the next node in the List
6. struct Node
7. {
8. int Data;
9. struct Node *Next;
10. }*Head;
11.
12. //Poping from a Stack
13. void popStack()
14. {
15. struct Node *tmp_ptr=NULL, *cur_ptr=Head;
16.
17. if(cur_ptr)
18. {
19. Head = Head->Next;
20. free(cur_ptr);
21. }
22. else
23. printf("\nStack is Empty");
24. }
25.
26. //Pushing into Stack
27. void pushIntoStack(int num)
28. {
29. struct Node *tmp_ptr=NULL;
30.
31. if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
32. tmp_ptr->Data=num;
33. else
34. printf("\nMemory Allocation Failed");
35.
36. if(Head)
37. {
38. tmp_ptr->Next=Head;
39. Head=tmp_ptr;
40. }
41. else { //List is Empty
42. Head=tmp_ptr;
43. Head->Next=NULL;
44. }
45. }
46.
47. //Displaying Stack
48. void displayStack()
49. {
50. struct Node *cur_ptr=Head;
51.
52. if(cur_ptr)
53. {
54. printf("\nElements in Stack:\n");
55. while(cur_ptr)
56. {
57. printf("\t%d\n",cur_ptr->Data);
58. cur_ptr=cur_ptr->Next;
59. }
60. printf("\n");
61. }
62. else
63. printf("\nStack is Empty");
64. }
65.
66. int main(int argc, char *argv[])
67. {
68. int i=0;
69.
70. //Set HEAD as NULL
71. Head=NULL;
72.
73. while(1)
74. {
75. printf("\n####################################################\n");
76. printf("MAIN MENU\n");
77. printf("####################################################\n");
78. printf(" \n1. Push into stack");
79. printf(" \n2. Pop from Stack");
80. printf(" \n3. Display Stack");
81. printf(" \n4. Exit\n");
82. printf(" \nChoose Option: ");
83. scanf("%d",&i);
84.
85. switch(i)
86. {
87. case 1:
88. {
89. int num;
90. printf("\nEnter a Number to push into Stack: ");
91. scanf("%d",&num);
92. pushIntoStack(num);
93. displayStack();
94. break;
95. }
96.
97. case 2:
98. {
99. popStack();
100. displayStack();
101. break;
102. }
103.
104. case 3:
105. {
106. displayStack();
107. break;
108. }
109.
110. case 4:
111. {
112. struct Node *temp;
113.
114. while(Head!=NULL)
115. {
116. temp = Head->Next;
117. free(Head);
118. Head=temp;
119. }
120. exit(0);
121. }
122.
123. default:
124. {
125. printf("\nWrong Option choosen");
126. }
127. }/* end if switch */
128. }/* end of while */
129. }/* end of main */
#include
#include
//Structure containing a Data part & a
//Link part to the next node in the List
struct Node
{
int Data;
struct Node *Next;
}*Head;
//Poping from a Stack
void popStack()
{
struct Node *tmp_ptr=NULL, *cur_ptr=Head;
if(cur_ptr)
{
Head = Head->Next;
free(cur_ptr);
}
else
printf("\nStack is Empty");
}
//Pushing into Stack
void pushIntoStack(int num)
{
struct Node *tmp_ptr=NULL;
if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
tmp_ptr->Data=num;
else
printf("\nMemory Allocation Failed");
if(Head)
{
tmp_ptr->Next=Head;
Head=tmp_ptr;
}
else { //List is Empty
Head=tmp_ptr;
Head->Next=NULL;
}
}
//Displaying Stack
void displayStack()
{
struct Node *cur_ptr=Head;
if(cur_ptr)
{
printf("\nElements in Stack:\n");
while(cur_ptr)
{
printf("\t%d\n",cur_ptr->Data);
cur_ptr=cur_ptr->Next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
//Set HEAD as NULL
Head=NULL;
while(1)
{
printf("\n####################################################\n");
printf("MAIN MENU\n");
printf("####################################################\n");
printf(" \n1. Push into stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display Stack");
printf(" \n4. Exit\n");
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int num;
printf("\nEnter a Number to push into Stack: ");
scanf("%d",&num);
pushIntoStack(num);
displayStack();
break;
}
case 2:
{
popStack();
displayStack();
break;
}
case 3:
{
displayStack();
break;
}
case 4:
{
struct Node *temp;
while(Head!=NULL)
{
temp = Head->Next;
free(Head);
Head=temp;
}
exit(0);
}
default:
{
printf("\nWrong Option choosen");
}
}/* end if switch */
}/* end of while */
}/* end of main */
References:
No comments:
Post a Comment