定义节点:
typedef struct Node {int data;Node* pNext;}NODE, *PNODE;
细节说明,PNode 就代表struct Node* ,上面的表单是也可以写成如下形式,含义是一样的
typedef struct Node {int data;Node* pNext;}*PNODE,NODE ;
算法操作
1. 创建链表
//1.初始化链表PNODE create_list(void) {int len,val;printf("%s", "请输入你要生成链表的长度\n");scanf("%d", &len);PNODE pHead = (PNODE)malloc(sizeof(NODE));//创建一个头结点PNODE pTail = pHead;//永远指向最后一个pTail->pNext = NULL;for (int i = 0; i < len; i++){printf("请输入第%d个变量的数值 ", i);scanf("%d", &val);PNODE p = (PNODE)malloc(sizeof(NODE));if (NULL == p) {printf("分配内存失败");}p->data = val;p->pNext = NULL;pTail->pNext = p;//尾节点指向挂在Ptail上pTail = p;//pTail 就是尾节点了}return pHead;}
2. 显示链表数据
//2.输出void show_list(PNODE pHead) {PNODE p = pHead->pNext;//第一个节点地址while (p != NULL){printf("%d\n", p->data);p = p->pNext;}printf("\n");return;}
3. 运行测试
int main(){PNODE pHead = NULL;//代表 Struct Node* pHead=NULL;pHead = create_list();//show_list(pHead);return 0;}
人生就是一场旅行,不在乎目的地,