As you know that in a stack the push and pop operation always done from the top, so if you imagine each index of stack a linked list than the push operation is nothing more than inserting a node at the front of list and pop operation is nothing but deletion a node from the front of list ( i.e., TOP)
here is the code for you ! Note that we have used power of recursion here to push the elements in the stack !
/******************************************************************************
Stacks Push and POP Operation using Linked List
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#include <stdio.h>
/* Lets create structure of the node*/
struct node
{
int data;
struct node *link;
}*head= NULL; // Creating a global head pointer of struct type
createnode(int n, int item)
{
if(n){
int data ;
struct node *temp;
/* lets create first node using malloc */
temp=(struct node*)malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Memory not allocated ");
return;
}
temp-> data = item;
temp->link=head;
head= temp;
// now suppose second element is 1 more than previous
createnode(n-1,item+1);
return head;
}
}
int main()
{
int n, item;
printf("Enter number of elemens to be pushed in stack\n\n");
scanf("%d", &n);
printf("Enter the item \n\n");
scanf("%d", &item);
struct node *temp1=createnode(n,item); / holding returned value of head which is a pointer
for(int i=0;i<n;i++)
{
head= temp1;
printf("Top %d in the stack is %d\n", i, temp1->data);
temp1= temp1->link;
}
return 0;
}
// The deletion of an element from the stack using concept of linked list is left as an exercise for you !
here is the code for you ! Note that we have used power of recursion here to push the elements in the stack !
/******************************************************************************
Stacks Push and POP Operation using Linked List
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#include <stdio.h>
/* Lets create structure of the node*/
struct node
{
int data;
struct node *link;
}*head= NULL; // Creating a global head pointer of struct type
createnode(int n, int item)
{
if(n){
int data ;
struct node *temp;
/* lets create first node using malloc */
temp=(struct node*)malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Memory not allocated ");
return;
}
temp-> data = item;
temp->link=head;
head= temp;
// now suppose second element is 1 more than previous
createnode(n-1,item+1);
return head;
}
}
int main()
{
int n, item;
printf("Enter number of elemens to be pushed in stack\n\n");
scanf("%d", &n);
printf("Enter the item \n\n");
scanf("%d", &item);
struct node *temp1=createnode(n,item); / holding returned value of head which is a pointer
for(int i=0;i<n;i++)
{
head= temp1;
printf("Top %d in the stack is %d\n", i, temp1->data);
temp1= temp1->link;
}
return 0;
}
// The deletion of an element from the stack using concept of linked list is left as an exercise for you !
No comments:
Post a Comment