简单的链表问题,求助。
对链表结构不是很了解,每个问号都是一个问题,如下:
程序代码:# include <stdio.h>
# include <stdlib.h>
# define N 8
typedef struct list //这个list是构建链表必须需要的吗?或者说是链表申明所特有必须加上去的?
{
int data;
struct list * next;
}SLIST ; //SLIST不是结构体变量?不然怎么可以在下一行又定义一个*p变量。
//那以上定义的结构体类型名为什么不可以这样定义吗?struct list SLIST{……}
void fun(SLIST * p) //SLIST可以单独用来做类型名,并且可以以此来定义头指针?
{
SLIST *t, *s;
t = p->next; //这句是不是表明t已经存放了头结点指针域的内容(也就是下一个结点的地址)?
s = p;
while (t->next != NULL) //如果上句成立,那这句括号中就是指:首结点指针域的内容不为空则成立?
{
s = t;
t = t->next;
}
printf("%d", t->data);
s->next=NULL;
free(t); //链表结构也是动态的存储数据?
}
SLIST * creatlist(int *a)
{
SLIST *h, *p, *q;
int i;
h = p = (SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{
q = (SLIST *)malloc(sizeof(SLIST));
q->data = a[i]; //这句是不是表明q = &a[i]?
p->next = q; //令p作为头指针?
p = q; //p向后进一位。
} //循环结束了以后,p存储的是最后一个结点的地址,那返回给h的岂不也是最后一个结点的地址?
p->next = 0; //0和NULL可以互换么?
return h;
}
void outlist(SLIST *h)
{
SLIST *p;
p = h->next; //如果h是头指针的话,p就存储了头结点的地址?
if (p == NULL) //判断链表是否是空结点。
printf("\nThe list is NULL!\n");
else
{
printf("\nhead");
do
{
printf("->%d", p->data);
p = p->next;
}while(p != NULL);
printf("->End\n");
}
}
int main(void)
{
SLIST * head;
int a[N] = {11, 12, 15, 18, 19, 22, 25, 29};
head = creatlist(a);
printf("\nOutput from head:\n");
outlist(head);
printf("\nOutput from tail:\n");
while (head->next!=NULL)
{
fun(head);
printf("\n\n");
printf("\nOutput from head again:\n");
outlist(head);
}
return 0;
}[ 本帖最后由 ai8343512 于 2011-8-19 16:17 编辑 ]








