- 头结点是在链表的开始结点之前附加的一个结点。可以使得无论链表是否为空,头节点总是非空
-
头指针:指向链表的第一个节点(或者说是链表中第一个结点的存储位置叫做头指针),如果链表有头结点,则是指向头结点的指针 。
在访问链表时,只能顺序访问,我们要先找到链表的头指针,因为它是指向第一个节点的指针,只有找到第一个节点之后 才能通过它访问后续节点,之后的每一个结点,其实就是上一个的后继指针指向的位置。
头指针具有标识作用,所以常用链表名称命名头指针。
无论链表是否为空,头指针均不为空。头指针是链表的必要元素。
-
以下为实现带头节点的简单有序链表的创建与查询修改(插入或删除)的代码
#include <stdio.h> #include <stdlib.h> #define LEN sizeof(structNmb) struct Nmb { int number; int xuhao; struct Nmb *next; }; struct Nmb *creat(void);//创建链表 void search_1(const struct Nmb *p, int n); //查找序号为n的结点,并输出其内容 void search_2(const struct Nmb *p, int x); //查找score为x的结点,并输出其内容 struct Nmb *insert(struct Nmb *p, int n, int x); //在序号为n的结点后插入一个新的结点,并可输出修改后的链表 struct Nmb *deleting(struct Nmb *p, int n); //删除序号为n的结点,并可输出修改后的链表 void main() { struct Nmb *head = creat(); struct Nmb *p = head->next; struct Nmb *temp = p; int n, x, a, score, b; scanf("%d", &n); scanf("%d", &x); scanf("%d", &a); scanf("%d", &score); scanf("%d", &b); while (1) { printf("%d ", p->number); p = p->next; if (p->next == NULL) { printf("%d\n", p->number); p = temp; break; } } search_1(head, n); search_2(head, x); insert(head, a, score); p = temp; while (1) { printf("%d ", p->number); p = p->next; if (p->next == NULL) { printf("%d\n", p->number); p = temp; break; } } deleting(head, b); p = head->next; while (1) { printf("%d ", p->number); p = p->next; if (p->next == NULL) { printf("%d\n", p->number); p = temp; break; } } } struct Nmb *creat(void) { int cunt = 1; struct Nmb *p, *idx, *temp; struct Nmb *head = (struct Nmb *)malloc(sizeof(struct Nmb)); idx = head; head->next = NULL; while (1) { p = (struct Nmb *) malloc(sizeof(struct Nmb)); scanf("%d", &p->number); if (p->number == 0) break; p->xuhao = cunt; p->next = NULL; idx->next = p; idx = p; cunt++; } ; getchar(); return head; } void search_1(const struct Nmb *p, int n) { struct Nmb *f = p->next; int cunt = 0; while (f != NULL) { if (f->xuhao == n) { printf("%d\n", f->number); cunt++; break; } f = f->next; } if (cunt == 0) printf("链表中无成员满足条件\n"); } void search_2(const struct Nmb *p, int x) { struct Nmb *f = p->next; int cunt = 0; while (1) { if (f->number == x) { printf("%d %d\n", f->xuhao, f->number); cunt++; break; } f = f->next; } if (cunt == 0) printf("链表中无成员满足条件\n"); } struct Nmb *insert(struct Nmb *p, int n, int x) { struct Nmb *head = p; struct Nmb *temp = (struct Nmb *)malloc(sizeof(struct Nmb)); //改变插入后的链表的各成员的序号 while (1) { if (p->xuhao >= n + 1) { p->xuhao++; } if (p->next == NULL) { p->xuhao++; break; } p = p->next; } p = head; //指针退回表头 //以下插入你想要的成员 while (1) { if (p->xuhao == n) { temp->next = p->next; temp->number = x; temp->xuhao = n + 1; p->next = temp; break; } p = p->next; } getchar(); return head; } struct Nmb *deleting(struct Nmb *p, int n) { struct Nmb *head = p; struct Nmb *temp = p->next; struct Nmb *idx = NULL; //若删除结点为头节点 if (n == 1) { head->next = temp->next; free(temp); } else { while (1) { if (p->xuhao == n - 1) idx = p; if (p->xuhao == n) { idx ->next = p->next; idx = p; p = p->next; free(idx); break; } p = p->next; } } //修改删除结点后的链表序号 p = head; while (1) { if (p->xuhao > n) { p->xuhao--; } if (p->next == NULL) break; p = p->next; } return head; }
带头节点的简单有序链表的创建和查询修改
最新推荐文章于 2022-10-23 10:37:59 发布