帮我看下 链表的的问题,看不懂,迷糊。
程序代码:
下面的内容无法理解是什么意思,对于链表很迷糊。。。 刚学链表,老师讲课时写了一大堆,无法理解,帮帮我,该怎么读懂(理解)下面的内容啊????小弟 先谢了! # include <stdio.h> # include <malloc.h> # include <stdlib.h> typedef struct Node { int data; struct Node * pNext; }NODE, *PNODE; PNODE create_list(void); void traverse_list(PNODE pHead); bool is_empty(PNODE pHead); int length_list(PNODE); bool insert_list(PNODE, int, int); bool delete_list(PNODE, int, int *); void sort_list(PNODE); int main(void) { PNODE pHead =NULL; pHead = create_list(); traverse_list(pHead); int len = length_list(pHead); printf ("链表的长度是%d\n", len); sort_list(pHead); traverse_list(pHead); /* if (is_empty(pHead)) printf ("链表为空\n"); else printf ("链表不空\n"); */ return 0; } PNODE create_list(void) { int len; int i; int val; PNODE pHead = (PNODE)malloc(sizeof(NODE)); if (NULL == pHead) { printf ("分配失败,程序终止!\n"); exit(-1); } PNODE pTail = pHead; pTail->pNext = NULL; printf ("请输入您需要生成的链表节点的个数: len = "); scanf ("%d", &len); for (i=0; i<len; ++i) { printf ("请输入第%d个节点的值:", i+1); scanf ("%d", &val); PNODE pNew = (PNODE)malloc(sizeof(NODE)); if (NULL == pNew) { printf ("分配失败,程序终止!\n"); exit(-1); } pNew->data = val; pTail->pNext = pNew; pNew->pNext = NULL; pTail = pNew; } return pHead; } void traverse_list(PNODE pHead) { PNODE p = pHead->pNext; while (NULL != p) { printf ("%d ", p->data); p = p->pNext; } printf ("\n"); return; } bool is_empty(PNODE pHead) { if (NULL == pHead->pNext) return true; else return false; } int length_list(PNODE pHead) { PNODE p = pHead->pNext; int len = 0; while (NULL != p) { ++len; p = p->pNext; } return len; } void sort_list(PNODE pHead) { int i, j, t; int len = length_list(pHead); PNODE p, q; for (i=0,p=pHead->pNext; i<len-1; ++i,p=p->pNext) { for (j=i+1,q=p->pNext; j<len; ++j,q=q->pNext) { if (p->data > q->data) { t = p->data; p->data = q->data; q->data = t; } } } return; }
[ 本帖最后由 wldnscjf 于 2013-1-27 04:05 编辑 ]