02链表及其操作
02链表及其操作
1、节点链表结构体
节点结构体
typedef struct ListNode { int data; struct ListNode* next; }ListNode;
链表结构体
typedef struct List { ListNode head; int length; }List;
2、生成节点和链表
生成节点
ListNode *getNewNode(int val); val为节点的数据
ListNode *getNewNode(int val) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->data = val; node->next = NULL; return node; }
生成链表
List *getLinkList();
List *getLinkList() { List *l = (List*)malloc(sizeof(List)); l->head.next = NULL; l->length = 0; return l; }
3、插入节点
int insert(List *l, int ind, int val); l为链表的结构体指针,ind为要插入的位置索引,val为要插入的数值
int insert(List *l, int ind, int val) { if (l == NULL) return 0; if (ind < 0 || ind > l->length) return 0; ListNode *p = &(l->head), *node = getNewNode(val); while (ind--) p = p->next; node->next = p->next; p->next = node; l->length += 1; return 1; }
4、删除节点
int erase(List *l, int ind); l为链表的结构体指针,ind为要删除的节点的索引位置
int erase(List *l, int ind) { if (l == NULL) return 0; if (ind < 0 || ind >= l->length) return 0; ListNode *p = &(l->head), *q; while (ind--) p = p->next; q = p->next; p->next = q->next; free(q); l->length -= 1; return 1; }
5、清除节点
void clear_node(ListNode *node); node为节点的结构体指针
void clear_node(ListNode *node) { if (node == NULL) return; free(node); return; }
6、删除链表
void clear(List *l); l为链表的结构体指针
void clear(List *l) { if (l == NULL) return; ListNode *p = l->head.next, *q; while (p) { q = p->next; clear_node(p); p = q; } free(l); return; }
7、链表反转
void reverse(List *l); l为链表的结构体指针
void reverse(List *l) { ListNode *p = l->head.next, *q; l->head.next = NULL; while (p) { q = p->next; p->next = l->head.next; l->head.next = p; p = q; } return; }
8、打印输出链表
void output(List *l); l为链表的结构体指针
void output(List *l) { if (l == NULL) return; printf("List(%d) = [", l->length); ListNode *p; for (p = l->head.next;p;p = p->next) { printf("%d->", p->data); } printf("NULL]\n"); return; }
9、测试及整体代码
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct ListNode { int data; struct ListNode* next; }ListNode; typedef struct List { ListNode head; int length; }List; ListNode *getNewNode(int); List *getLinkList(); int insert(List*, int, int); int erase(List*, int); void clear_node(ListNode*); void clear(List*); void reverse(List*); void output(List*); int main() { srand(time(0)); #define max_op 20 List *l = getLinkList(); int i; for (i=0;i<max_op;i++) { int val = rand() % 100; int ind = rand() % (l->length + 3) - 1; int op = rand() % 4; switch(op) { case 0: case 1: { printf("insert %d at %d to List = %d\n", val, ind, insert(l, ind, val)); break; } case 2: { printf("erase item at %d from List = %d\n", ind, erase(l, ind)); break; } case 3: { printf("reverse the List!\n"); reverse(l); break; } } output(l); printf("\n"); } #undef max_op clear(l); return 0; } ListNode *getNewNode(int val) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->data = val; node->next = NULL; return node; } List *getLinkList() { List *l = (List*)malloc(sizeof(List)); l->head.next = NULL; l->length = 0; return l; } int insert(List *l, int ind, int val) { if (l == NULL) return 0; if (ind < 0 || ind > l->length) return 0; ListNode *p = &(l->head), *node = getNewNode(val); while (ind--) p = p->next; node->next = p->next; p->next = node; l->length += 1; return 1; } int erase(List *l, int ind) { if (l == NULL) return 0; if (ind < 0 || ind >= l->length) return 0; ListNode *p = &(l->head), *q; while (ind--) p = p->next; q = p->next; p->next = q->next; free(q); l->length -= 1; return 1; } void clear_node(ListNode *node) { if (node == NULL) return; free(node); return; } void clear(List *l) { if (l == NULL) return; ListNode *p = l->head.next, *q; while (p) { q = p->next; clear_node(p); p = q; } free(l); return; } void reverse(List *l) { ListNode *p = l->head.next, *q; l->head.next = NULL; while (p) { q = p->next; p->next = l->head.next; l->head.next = p; p = q; } return; } void output(List *l) { if (l == NULL) return; printf("List(%d) = [", l->length); ListNode *p; for (p = l->head.next;p;p = p->next) { printf("%d->", p->data); } printf("NULL]\n"); return; }
测试输出:
知行合一,
翻万卷书,游千里路,会百家才