dimanche 29 mars 2015

program to find the largest element in stack while implementing linked list but the data is not being accesed




#include<stdio.h>
#include<stdlib.h>
//structure//
struct node
{
int data;
struct node *link;
};
//function to push elements into stack//
void push(struct node *top_ref,int x)
{
struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=x;
new->link=top_ref;
top_ref=new;
}
//function to get largest element//
int largest(struct node *top_ref)
{
int temp=0;
while(top_ref!=NULL)
{
if(temp<top_ref->data)
{
temp=top_ref->data;
}
top_ref=top_ref->link;
}
return temp;
}
//function to print the stack//
void print(struct node *top_ref)
{
while(top_ref!=NULL)
{
printf("%d->",top_ref->data);
top_ref=top_ref->link;
}
printf("\n");
}
//main function//
int main()
{
struct node *stack=(struct node *)malloc(sizeof(struct node));
stack=NULL;
push(stack,1);
push(stack,5);
push(stack,6);
push(stack,3);
print(stack);
printf("the largest no. is %d\n",largest(stack));
}


trying to find the largest no. in a stack there is a problem in line 20 ,32 top_ref->data is not being accesed top_ref is the last node of the linked list new to linked list and data structures


the output i am getting is



empty line
the largest no. is 0



Aucun commentaire:

Enregistrer un commentaire