标签:大量 lin 循环链表 locate code data lib 尾插法 null
链式与顺序结构的最大区别在于,插入或删除操作需要移动大量元素。
链表类型:单链表,循环链表,双向链表。
单链表的组成:每个数据元素内包括两个域:数据域和指针域。
单链表的创建方式有两种:一种是头插法和尾插法。
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct Node{ ElemType data; struct Node *next; }ListNode; typedef ListNode *LinkList; /** ***插入 ***/ void InsertList(LinkList head, int i, ElemType x) { ListNode *p, *s; int j = 0; p = head; while(p != NULL && j < i - 1) { p = p->next; ++j; } if (p == NULL) { printf("ERROR\n"); return ; } else { s = (ListNode *)malloc(sizeof(ListNode)); s->data = x; s->next = p->next; p->next = s; } } /** *** 删除 **/ ElemType DeleteList(LinkList head, int i) { ListNode *p, *s; ElemType x; int j = 0; p = head; while (p != NULL && j < i - 1) { p = p->next; ++j; } if (p == NULL) { printf("position error\n"); return -1; } else { s = p->next; p->next = s->next; x = s->data; free(s); return x; } } /** **头插法 **/ LinkList CreateListHead() { LinkList head = NULL; ListNode *p; int ch; while( (ch = getchar() )!= ‘\n‘) { p = (ListNode *)malloc(sizeof(ListNode)); p->data = ch; p->next = head; head = p; } return head; } /** ** 尾插法 **/ LinkList CreateListTail() { LinkList head = (ListNode *)malloc(sizeof(ListNode)); ListNode *p, *r; int ch; r = head; while( (ch = getchar()) != ‘\n‘) { p = (ListNode *)malloc(sizeof(ListNode)); p->data = ch; r->next = p; r = p; } r->next = NULL; return head; } ListNode *LocateNodek(LinkList head, ElemType k) { ListNode *p = head->next; while(p && p->data != k) { p = p->next; } return p; } ListNode *GetNodei(LinkList head, int i) { ListNode *p; int j = 1; p = head->next; while( p!= NULL && j < i) { p = p->next; ++j; } if (j == i) return p; else return NULL; }
标签:大量 lin 循环链表 locate code data lib 尾插法 null
原文地址:https://www.cnblogs.com/Python-233/p/14947863.html