typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node;
find(head,i)
查找\(q\)结点,查不到打印报错信息p->next = head;
head = p;
p->next = q->next;
q->next=p;
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node; node *insert(node *head, datatype x, int i) { node *p, *q; q = find(head, i); // 查找第i个结点 if (!q && i != 0) { printf("\n找不到第%d个结点,不能插入%d!", i, x); } else { p = (node *) malloc(sizeof(node)); // 分配空间 p->info = x; // 设置新结点 if (i == 0) // 插入的结点做为单链表的第一个结点 { p->next = head; head = p; } else { p->next = q->next; // 后插 q->next = p; } } return head; }
head = head->next;
free(p)
pre->next = q->next;
free(p)
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node; node *dele(node *head, datatype x) { node *pre = NULL, *p; if (!head) { printf("单链表是空的"); return head; } p = head; while (p && p->info != x) // 寻找被删除结点p { pre = p; // pre指向p的前驱结点 p = p->next; } if (p) { if (!pre) // 被删除结点没有上一个结点,则是要删除的是第一个结点 { head = head->next; } else { pre->next = p->next; } free(p) } return head; }
head->next
指示的typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node;
find(head,i)
查找带头结点的单链表中的第 \(i\) 个结点( \(i=0\) 表示新结点插入在头结点以后)p->next = q->next;
q->next = p;
p->next = q->next;
q->next = p;
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node; node *insert(node *head, datatype x, int i) { node *p, *q; q = find(head, i); // 查找带头结点的单链表中的第 i 个结点,i=0 时表示新结点插入在头结点以后 if (!q) // 没有找到 { printf("\n带头结点的单链表中不存在第%d个结点!不能插入%d!", i, x); return head; } p = (node *) malloc(sizeof(node)); // 为准备插入的新结点分配空间 p->info = x; // 为新结点设置值 p->next = q->next; q->next = q; // i=0 时,本语句等价于 head->next=p return head; }
pre->next = q->next;
free(q)
pre->next = q->next;
free(q)
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node; node *dele(node *head, datatype x) { node *pre = head, *q; // pre 指向头结点 q = head->next; // q 从带头结点的单链表的第一个实际结点开始找值为 x 的结点 while (q && q->info != x) // 循环查找值为 x 的结点 { pre = q; // pre 指向 q 的前驱 q = q->next; } if (q) { pre->next = q->next; // 删除 free(q); // 释放内存空间 } return head; }
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node;
typedef int datatype; typedef struct dlink_node { datatype info; struct dlink_node *llink, *rlink; } dnode;
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node;
typedef int datatype; typedef struct link_node { datatype info; struct link_node *next; } node; typedef struct { node *front, *rear; // 定义队首和队尾指针 } queue;
设计一个算法,求一个单链表中的结点个数node
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; int count(linklist head) { int c = 0; linklist p = head; // head为实际的第一个结点 while (p) // 计数 { c++; p = p->next; } return c; }
设计一个算法,求一个带头结点单链表中的结点个数c++
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; int count(linlist head) { int c = 0; linklist = head->next; // head->next 为实际的第一个结点 while (p) // 计数 { c++; p = p->next; } return c; }
设计一个算法,在一个单链表中值为 y 的结点前面插入一个值为 x 的结点。即便值为 x 的新结点成为值为 y 的结点的前驱结点算法
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; void insert(linklist head, int y, int c) { linklist pre, p, s; // 假设单链表带头结点 pre = head; p = head->next; while (p && p->data != y) { pre = p; p = p->next; } if (p) // 找到了值为 y 的结点,即 p == y { s = (linklist) malloc(sizeof(linknode)); s->data = x; s->next = p; pre->next = s; } }
设计一个算法,判断一个单链表中各个结点值是否有序函数
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; int issorted(linklist head, char c) // c='a' 时为升序,c='d' 时为降序 { int flag = 1; linklist p = head->next; switch (c) { case 'a': // 判断带头结点的单链表 head 是否为升序 while (p && p->next && flag) { if (p->data <= p->next->data) p = p->next; else flag = 0; } break; case 'd': // 判断带头结点的单链表 head 是否为降序 while (p && p->next && flag) { if (p->data >= p->next->data) p = p->next; else flag = 0 } break; } return flag; }
设计一个算法,利用单链表原来的结点空间将一个单链表就地转置spa
head->next
保留上一个 \(q\) 的状态head->next;
head->next;
始终指向上一个 \(q\)typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; void verge(linklist head) { linlist p, q; p = head->next; head->next = NULL; while (p) { q = p; p = p->next; q->next = head->next; // 经过 head->next 保留上一个 q 的状态 head->next = q; } }
设计一个算法,将一个结点值天然数的单链表拆分为两个单链表,原表中保留值为偶数的结点,而值为奇数的结点按它们在原表中的相对次序组成一个新的单链表设计
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; linklist sprit(linklist head) { linklist L, pre, p, r; L = r = (linklist) malloc(sizeof(linknode)); r->next = NULL; pre = head; p = head->next; while (p) { if (p->data % 2 == 1) // 删除奇数值结点,并用 L 链表保存 { pre->next = p->next; r->next = p; r = p; // 这样使得 r 变成了 r->next p = pre->next; // 这样使得 p 变成了 head->next->next } else // 保留偶数值结点 { pre = p; // 书中的貌似多余操做 p = p->next; } } r->next = NULL; // 置返回的奇数链表结束标记 return L; }
设计一个算法,对一个有序的单链表,删除全部值大于 x 而不大于 y 的结点指针
typedef struct node { int data; struct node *next; } linknode; typedef linknode *linklist; void deletedata(linklist head, datatype x, datatype y) { linklist pre = head, p, q; p = head->next; // 找第 1 处大于 x 的结点位置 while (p && p->data <= x) { pre = p; p = p->next; } // 找第 1 处小于 y 的位置 while (p && p->data <= y) p = p->next; // 删除大于 x 而小于 y 的结点 q = pre->next; pre->next = p; // 小于 x 的第一个结点指向大于 y 的第一个结点 pre = q->next; // 释放被删除结点所占用的空间 while (pre != p) { // 此时 p 已经指向了大于 y 的第一个结点 free(q); q = pre; pre = pre->next; } }
node *p = head; while (p && p->info != x) p = p->next; return p;