Jump to content

Linked list problem

This program is not running.It stops when I insert 1st element.The code is below.

 

#include<stdio.h>
#include<stdlib.h>

struct nod
{
	int data;
	int *next;
};
typedef struct nod node;
node *temp=NULL;
node *start,*newnode;

void insert ()
{
	int ele;
	printf("\n Enter the element ");
	scanf("%d",&ele);
	if(start=NULL)
	{
		newnode=(node *)malloc(sizeof(node));
		newnode->data=ele;
		newnode->next=NULL;
		start=newnode;
		temp=newnode;

	}
	else
	{
		newnode=(node *)malloc(sizeof(node));
		newnode->data=ele;
		newnode->next=NULL;
		temp->next=newnode;
		temp=newnode;
	}
}

void display()
{
	node *temp;
	temp=start;
	while(temp!=NULL)
	{
		printf("%d",&temp->data);
		temp=temp->next;
	}
}

void main()
{	node * start =NULL;
	int ch;
	
	while(1)
	{

	printf("\n Enter your choice ");
	printf("\n1.Insert ");
	printf("\n2.Display");
	printf("\n3.Exit");
	printf("\n enter your choice ");
	scanf("%d",&ch);
	switch(ch)
	{
		case 1 :insert();
				break;
		case 2 :display();
				break;
		case 3 :exit(0);

	}
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

if(start=NULL)

Typo there. To avoid those kind of things it's a good idea to put the literal value on the left 

if(NULL == start)

that way if you miss one of the '=' you get a compile time error instead of a runtime bug.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, fizzlesticks said:

if(start=NULL)

Typo there. To avoid those kind of things it's a good idea to put the literal value on the left 


if(NULL == start)

that way if you miss one of the '=' you get a compile time error instead of a runtime bug.

Thanks for the advice.I will keep in mind next time.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×