是一种线形的存储结构
链表和数组一样,也是将一组同类型的数据组织在一起的一种数据结构
不同的是
数组采用的是顺序存储,依靠数组的首地址和元素的相对地址(下标)来实现访问。
优点是访问方便快捷,而缺点是数组是静态的,不利于实现元素的动态增减。
而链表采用的是离散存储,依靠节点间的指向下一个节点的指针来实现访问。
其优缺点和数组相反
SLIST *creatlist(){
SLIST *head,*tail,*cnew
head=NULL
int num
printf("输入数据(以-1结束):")
while(1)
{
scanf("%d",&num)
if(num==-1)//输入为-1表示输入结束
break
cnew=(SLIST*)malloc(sizeof(SLIST))
cnew->data=num
cnew->next=NULL
if(head==NULL)//若为空则将头节点指向新节点
head=cnew
else
tail->next=cnew//将当前节点的next指向新的节点
tail=cnew
}
return head
}
void outlist(SLIST *h)
{
/*这里输出链表中各个数据*/
SLIST *p
if(h==NULL)
{
printf("链表为空,没有数据\n")
return
}
printf("\n-----链表的数据元素------\n")
for(p=hp!=NULLp=p->next)
printf("%d ",p->data)
printf("\n")
}
不知道 你为啥问两遍
实测编译通过 望采纳
删除节点函数:
SLIST *deletelist(SLIST *p,int *pvalue){
SLIST *todel, *head = p
while(p && p->next) {
if (p->next->data == *pvalue) {
todel = p->next
p->next = todel->next
free(todel)
} else {
p = p->next
}
}
return head
}
输出函数:
void outlist(SLIST *h){
/*这里输出链表中各个数据*/
h = h->next
while (h) {
printf("%d", h->data)
h = h->next
if (h) {
printf(" ")
}
}
printf("\n")
}
先提交回答,然后我检查一下
---------------------------------
修改了一些bug,现在是正常的了,之前没有注意head节点是个特殊节点
另外,你的main函数里,缺少了 int a[N] 的定义,不知道是不是你漏打了,没有这个定义编译不过去的