Completely Solved C, C++ Programs Assignment.




Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

C program for implementation of a stack using the linked list

Filed Under: ,

Implementation of Stack using Linked List Initially the list is empty, so the top pointer is NULL. The push function takes a pointer to an existing list as the first parameter and a data value to be pushed as the second parameter, creates a new node by using the data value, and adds it to the top of the existing list. A pop function takes a pointer to an existing list as the first parameter, and a pointer to a data object in which the popped value is to be returned as a second parameter. Thus it retrieves the value of the node pointed to by the top pointer, takes the top point to the next node, and destroys the node that was pointed to by the top.
If this strategy is used for creating a stack with the previously used four data values: 10, 20, 30, and 40, then the stack is created as shown in Figure 2.
Program C program for implementation of a stack using the linked list
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *push(struct node *p, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
/* creates new node
using data value
passed as parameter */
if(temp==NULL)
{
printf("No Memory available Errorn");
exit(0);
}
temp->data = value;
temp->link = p;
p = temp;
return(p);
}

struct node *pop(struct node *p, int *value)
{
struct node *temp;
if(p==NULL)
{
printf(" The stack is empty can not pop Errorn");
exit(0);
}
*value = p->data;
temp = p;
p = p->link;
free(temp);
return(p);
}

void main()
{
struct node *top = NULL;
int n,value;
clrscr();
do
{
do
{
printf("Enter the element to be pushedn");
scanf("%d",&value);
top = push(top,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to pop an elementn");
scanf("%d",&n);
while( n == 1)
{
top = pop(top,&value);
printf("The value poped is %dn",value);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be pushed
10
Enter 1 to continue
1
Enter the element to be pushed
20
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 20
Enter 1 to pop an element
1
The value poped is 10
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
30
Enter 1 to continue
1
Enter the element to be pushed
40
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 40
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
50
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 50
Enter 1 to pop an element
1
The value popped is 30
Enter 1 to pop an element
0
Enter 1 to continue
0
Back to main directory:  Data Structure 



C program to implement a stack using an array

Filed Under: ,

Welcome to mycomputerscience.net! your source for free data structures online course,data structure final exam,computer data structures and interactive data structure visualizations,array implementation of stack.
Array implementation of Stack When an array is used to implement a stack, the push and pop operations are realized by using the operations available on an array. The limitation of an array implementation is that the stack cannot grow and shrink dynamically as per the requirement.
Program A complete C program to implement a stack using an array.
#include <stdio.h>
#define MAX 10 /* The maximum size of the stack */
#include <stdlib.h>
void push(int stack[], int *top, int value)
{
if(*top < MAX )
{
*top = *top + 1;
stack[*top] = value;
}
else
{
printf("The stack is full can not push a valuen");
exit(0);
}
}
void pop(int stack[], int *top, int * value)
{
if(*top >= 0 )
{
*value = stack[*top];
*top = *top - 1;
}
else
{
printf("The stack is empty can not pop a valuen");
exit(0);
}
}
void main()
{
int stack[MAX];
int top = -1;
int n,value;
do
{
do
{
printf("Enter the element to be pushedn");
scanf("%d",&value);
push(stack,&top,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
while( n == 1)
{
pop(stack,&top,&value);
printf("The value poped is %dn",value);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output:
Enter the element to be pushed
10
Enter 1 to continue
1
Enter the element to be pushed
20
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 20
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
40
Enter 1 to continue
1
Enter the element to be pushed
50
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 50
Enter 1 to pop an element
1
The value popped is 40 Enter 1 to pop an element
1
The value popped is 10 Enter 1 to pop an element
0
Enter 1 to continue
0
Back to main directory:  Data Structure 



Concept of Stack

Filed Under: ,

Welcome to mycomputerscience.net! your source for free data structures online course,data structure final exam,computer data structures and interactive data structure visualizations,use of stack in computer science.
[stack] A stack is simply a list of elements with insertions and deletions permitted at one end—called the stack top. That means that it is possible to remove elements from a stack in reverse order from the insertion of elements into the stack. Thus, a stack data structure exhibits the LIFO (last in first out) property. Push and pop are the operations that are provided for insertion of an element into the stack and the removal of an element from the stack, respectively. Shown in Figure 1 are the effects of push and pop operations on the stack.
Figure 1: Stack operations.
Since a stack is basically a list, it can be implemented by using an array or by using a linked representation.
There are many applications requiring the use of the data structures stacks and queues. The most striking use of a data structure stack is the runtime stack that a programming language uses to implement a function call and return. Similarly, one of the important uses of a data structure queue is the process queue maintained by the scheduler. Both these data structures are modified versions of the list data structure, so they can be implemented using arrays or linked representation.
Back to main directory:  Data Structure