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;
}