What's wrong with the program?
It went wrong when i print the numbers in the lists. After i prints the last number, it didn't stop, and the program went into wrong memory.
Eager for your answer:

#include <stdio.h>
#include <malloc.h>

#define NULL 0

typedef int ELEMNT_TYPE;
typedef struct node * node_ptr;

struct node
{
	int element;
	node_ptr next;
};

typedef node_ptr position;
typedef node_ptr LIST;

/*

int is_empty(LIST L)
{
	return(L->next==NULL);
}

int is_last(position p, LIST L)
{
	return(p->next==NULL);
}

position find(int x, LIST L)
{
	position p;
	p=L->next;
	while(p!=NULL&&p->element!=x)
		p=p->next;
	return p;
}

void delete(int x, LIST L)
{
	position p, tmp_cell;
	p=find_previous(x, L);
	if(p->next!=NULL)
	{
		tmp_cell=p->next;
		p->next=p->next->next;
		free(tmp_cell);
	}
}

position find_previous(int x, LIST L)
{
	position p;
	p=L;
	while(p->next!=NULL&&p->next->element!=x)
		p=p->next;
	return p;
}

*/


void insert(int x, LIST L, position p)		/*insert after the position p*/
{
	position new_cell;
	new_cell=(position)malloc(sizeof(struct node));
	if(new_cell==NULL)
		printf("Out of space!!!");
	else
	{
		new_cell->element=x;
		new_cell->next=p->next;
		p->next=new_cell;
	}
}

void showlist(LIST L)
{
	while(L->next!=NULL)
	{
		L=L->next;
		printf("%d ", L->element);
	}
}

int main()
{
	int i;
	LIST list=(position)malloc(sizeof(struct node));
	printf("Inserting data...\n");
	for(i=0; i<=10; i++)
		insert(i, list, list);
	printf("This is the list:\n");
	showlist(list);
	printf("Done!\n");
	return 0;
}

What's wrong with the program?

You're missing a NULL in the first node.

int main()
{
   int i;
   LIST list=(position)malloc(sizeof(struct node));
   list->next = NULL;
   printf("Inserting data...\n");
   ...

Thank you, but I think I have found the answer. The problem is that NULL is defined in stdio.h, rather than being 0. I redefined it, which made it dysfunctional.

Thank you, but I think I have found the answer. The problem is that NULL is defined in stdio.h, rather than being 0. I redefined it, which made it dysfunctional.

No, that's NOT the problem. You assume list->next will automatically be NULL after malloc() and that's WRONG. The content of mem chunk malloc() returns is not initialized. That's why the while block in showlist() loops forever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.