今天学习双向循环链表的操作,参考数据结构相关书籍,欲实现链表的建立、输出、删除、插入与排序,但今天在链表的创建即遇难题,在运行是始终提示错误,百思不得其解。特发上来,望各位高手指教,使用编译器是Dev-C++
源程序如下:
#include <stdio.h>
#include <stdlib.h>
struct linkedlist
{
int dt;
struct linkedlist *prev;
struct linkedlist *next;
};
//void insnod(struct linkedlist *ln,int data);
void createlinked(struct linkedlist *hl,int no);
void printlinked(struct linkedlist *phl);
void delnod(struct linkedlist *hl,int deln);
struct linkedlist *findnod(struct linkedlist *hl,int sdt);
int main(void)
{
struct linkedlist *hd;
int n;
printf("How length of linkedlist:");
scanf("%d",&n);
printf("length is linkedlist:%d\n",n);
createlinked(hd,n);
printlinked(hd);
printf("What data you want delete");
scanf("%d",&n);
printf("Data '%d' will be delete\n");
delnod(hd,n);
system("PAUSE");
return 0;
}
//void insnod(struct linkedlist *ln,int data)
//{
// struct linkedlist *pl;
// pl=findnod(ln,data);
// pl->next->prev=pl->prev;
// pl->prev->next=pl->next;
// free(pl);
//}
void createlinked(struct linkedlist *hl,int no)
{
int i;
struct linkedlist *pl,*pj,*po;
hl=(struct linkedlist *)malloc(sizeof(struct linkedlist));
if(NULL==hl)
{
printf("Out of space!\n");
}
hl->prev=hl;
hl->next=hl;
printf("Pls. input data of linkedlist:");
pj=hl;
for(i=0;i<no;i++)
{
pl=(struct linkedlist *)malloc(sizeof(struct linkedlist));
if(NULL==pl)
{
printf("Out of space!\n");
}
scanf("%d",pl->dt);
pl->next=hl;
pl->prev=pj;
pj->next=pl;
hl->prev=pl;
pj=pl;
}
}
void printlinked(struct linkedlist *phl)
{
struct linkedlist *pl;
int n=0;
pl==phl->next;
printf("H");
while(pl!=phl)
{
n++;
pl=pl->next;
printf("r");
}
printf("linkedlist length:%d\n",n);
// printf("Linkedlist is:");
// while(pl!=phl)
// {
// printf("%d. ",pl->dt);
// pl=pl->next;
// }
// printf(" END\n");
}
void delnod(struct linkedlist *hl,int deln)
{
struct linkedlist *pl;
pl=findnod(hl,deln);
if(NULL!=pl)
{
pl->next->prev=pl->prev;
pl->prev->next=pl->next;
free(pl);
printf("Delete OK!\n");
}
else
{
printf("Can't find the data.\n");
}
}
struct linkedlist *findnod(struct linkedlist *hl,int sdt)
{
struct linkedlist *pl;
pl=hl;
do
{
if(sdt==pl->dt)
break;
else
pl=pl->next;
}
while(pl!=hl);
if(pl!=hl)
return pl;
else
return NULL;
} |