1. 线性表:n个数据元素的有序集合。
线性表是一种常用的数据结构。在实际应用中,线性表都是以栈、队列、字符串、数组等特殊线性表的形式来使用的。由于这些特殊线性表都具有各自的特性,因此,掌握这些特殊线性表的特性,对于数据运算的可靠性和提高操作效率都是至关重要的。 线性表是一个线性结构,它是一个含有n≥0个结点的有限序列,对于其中的结点,有且仅有一个开始结点没有前驱但有一个后继结点,有且仅有一个终端结点没有后继但有一个前驱结点,其它的结点都有且仅有一个前驱和一个后继结点。
特征:
1.集合中必存在唯一的一个“第一元素”;
2.集合中必存在唯一的一个 “最后元素” ;
3.除最后一个元素之外,均有 唯一的后继(后件);
4.除第一个元素之外,均有 唯一的前驱(前件)。
Java中的List接口,就是线性表。ArrayList就是顺序线性表,LinkedList就是链表线性表。
2. 线性表的顺序表示:ArrayList
一般使用数组(C语言中的数组采用顺序存储方式。即连续地址存储)来描述。
优点:在于随机访问元素,
缺点:插入和和删除的时候,需要移动大量的元素。
c语言实现代码:
-
- #include "stdafx.h"
- #include <stdio.h>
- #include "stdlib.h"
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define INFEASIBLE -1
- #define OVERFLOW -2
-
- #define LT(a,b) ((a)<(b))
- #define N = 100
-
- #define LIST_INIT_SIZE 100 //线性表初始空间分配量
- #define LISTINCREMENT 10 //线性表空间分配的增量
-
- typedef int Status;
- typedef int ElemType;
-
- typedef struct LNode{
- ElemType *elem;
- int lenght;
- int listsize;
- }SqList;
-
-
- Status initList(SqList &L, int lenght){
- if (lenght == 0) lenght = LIST_INIT_SIZE;
- L.elem = (ElemType *)malloc(lenght * sizeof(ElemType));
- if(!L.elem) exit(OVERFLOW);
- L.lenght = 0;
- L.listsize = lenght ;
- return OK;
- }
- Status insertList(SqList &L, ElemType e, int i){
- ElemType *p, *q;
- if(i<0 ||i > L.lenght) return ERROR;
- if (L.lenght >= L.listsize) {
- ElemType *newbase = (ElemType *)realloc(L.elem ,(L.listsize +LISTINCREMENT)*sizeof(ElemType));
- if(!newbase) return OVERFLOW;
- L.elem = newbase;
- L.listsize += LISTINCREMENT;
- }
- q = &L.elem[i];
- for (p = &L.elem[L.lenght]; p>=q; --p) {
- *p = *(p-1);
- }
- *q = e;
- L.lenght +=1;
- return OK;
-
- }
- void sortList(SqList &L){
-
-
- }
- Status deleteListElem(SqList &L, int i, ElemType &e){
- int *p, *q;
- if(i<0 ||i > L.lenght) return ERROR;
- q = &L.elem[i];
- e = *q;
- for (p = q; p< (L.elem + L.lenght); p++){
- *p = *(p+1);
- }
- --L.lenght;
- return OK;
- }
-
-
- int partition(SqList &L, ElemType low, ElemType high){
- ElemType pivotkey = L.elem[low];
- while (low < high) {
- while (low < high && L.elem[high] >= pivotkey ) --high;
- L.elem[low] = L.elem[high];
- L.elem[high] = pivotkey;
-
- while (low < high && L.elem[low] <= pivotkey ) ++low;
- L.elem[high] = L.elem[low];
- L.elem[low] = pivotkey;
- }
- return low;
- }
-
- void quickSort(SqList &L, ElemType low, ElemType high){
- int pivot;
- if(low < high) {
- pivot = partition(L, low, high);
- quickSort(L, low, pivot -1);
- quickSort(L, pivot +1, high);
- }
-
- }
-
-
-
- void mergeList(SqList La, SqList Lb, SqList &Lc){
- ElemType *pa, *pb, *pc;
- Lc.listsize = La.lenght + Lb.lenght;
- initList(Lc, Lc.listsize);
- Lc.lenght = Lc.listsize;
- pc = Lc.elem;
- pa = La.elem;
- pb = Lb.elem;
- while (pa <= &La.elem[La.lenght -1] && pb <= &Lb.elem[Lb.lenght -1]){
- if (*pa <= *pb) *pc++ = *pa++;
- else *pc++ = *pb++;
- }
- while(pa <= &La.elem[La.lenght -1]) *pc++ = *pa++;
- while(pb <= &Lb.elem[Lb.lenght -1]) *pc++ = *pb++;
-
- }
-
- void printList(SqList L){
- printf("当前值:");
- for (int i =0; i<L.lenght;i++) {
- printf("%d ", *(L.elem+i));
- }
- printf("\r\n");
- }
-
- void main()
- {
- SqList La,Lb,Lc;
- ElemType e;
- int init,i;
- init = initList(La, LIST_INIT_SIZE);
- int data[6] = {5,3,6,2,7,4};
- for (i=0; i<6;i++) {
- insertList(La, data[i], i);
- }
- printf("LA:\r\n");
- printList(La);
- deleteListElem(La, 3, e );
- printList(La);
- insertList(La, e, 3);
- printList(La);
-
-
- quickSort(La,0, La.lenght-1);
- printList(La);
-
- printf("LB:\r\n");
- initList(Lb, LIST_INIT_SIZE);
- int Bdata[5] = {1,3,2,4,6};
- for (i=0; i<5;i++) {
- insertList(Lb, Bdata[i], i);
- }
-
- quickSort(Lb,0, Lb.lenght-1);
- printList(Lb);
-
- mergeList(La, Lb, Lc);
- printList(Lc);
-
- }
3. 线性表的链表表示LinkedList
一般使用链表来描述。
优点:对于新增和删除操作add和remove和方便。不需要移动元素。
缺点:不方便随机访问元素,LinkedList要移动指针
代码实现:
- #include "stdafx.h"
- #include <stdio.h>
- #include "stdlib.h"
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define INFEASIBLE -1
- #define OVERFLOW -2
-
- #define LT(a,b) ((a)<(b))
- #define N = 100
-
- typedef int Status;
- typedef int ElemType;
-
- typedef struct LNode{
- ElemType data;
- struct LNode *next;
- }LNode, *LinkList;
-
- Status initList(LinkList &L){
-
- L = (LinkList)malloc(sizeof(LNode));
- if(!L) exit(OVERFLOW);
- L->next=NULL;
- return OK;
-
-
-
- }
-
- void createList(LinkList L, int n){
-
- if (!L) {
- initList(L);
- }
- ElemType data;
- LinkList p,q = L;
- printf("输入节点数据的个数%d:\r\n", n);
- for(int i = 0; i<n; i++) {
- p = (LinkList) malloc( sizeof(LNode));
- scanf("%d",&data);
- p->data = data;
- p->next = q->next;
- q->next = p;
- q = p;
- }
- }
- Status insertList(LinkList L, ElemType e, int i){
- LinkList s, p = L;
- int j = 0;
- while (p && j<i){
- p = p->next;
- j++;
- }
- if (!p ||j >i) return ERROR;
- s = (LinkList) malloc(sizeof(LNode));
- s->data = e; s->next = p->next;
- p->next = s;
- return OK;
-
- }
-
- Status deleteListElem(LinkList L, int i, ElemType &e){
- LinkList p, q;
- int j = 0;
- p = L;
- while (p && j<i){
- p = p->next;
- ++j;
- }
- if (!p->next || j>i) return ERROR;
- q = p->next; p->next = q->next;
- e = q->data; free(q);
- return OK;
- }
-
-
-
- void InsertSort(LinkList L)
- {
- LinkList list;
- LinkList node;
- LinkList p;
- LinkList q;
-
- list = L->next;
- L->next = NULL;
- while (list != NULL) {
- node = list, q = L;
- while (q && node->data > q->data ) {
- p = q;
- q = q->next;
- }
-
- if (q == L) {
- L = node;
- } else {
- p->next = node;
- }
- list = list->next;
- node->next = q;
-
- }
- }
-
-
-
-
- void mergeList(LinkList &La, LinkList &Lb, LinkList &Lc){
- LinkList pa, pb, pc;
- pa = La->next;
- pb = Lb->next;
- Lc = pc = La;
- while (pa && pa) {
- if (pa->data > pb->data) {
- pc->next = pb;
- pc = pb;
- pb =pb->next;
- }else{
- pc->next = pa;
- pc = pa;
- pa =pa->next;
- }
- }
- pc->next = pa? pa :pb;
- free(Lb);
- }
-
- void printList(LinkList L){
- printf("当前值:");
- LinkList p;
- p = L->next;
- while(p){
- printf("%d ", p->data);
- p = p->next;
- }
- printf("\r\n");
- }
-
- void main()
- {
- LinkList La,Lb,Lc;
- ElemType e;
- int init,i;
- printf("LA:\r\n");
- initList(La);
- createList(La, 5);
- insertList(La, 7, 3);
- printList(La);
- deleteListElem(La, 3, e);
- printList(La);
- InsertSort(La);
- printList(La);
-
- printf("Lb:\r\n");
- initList(Lb);
- createList(Lb, 4);
- InsertSort(Lb);
- printList(Lb);
-
- printf("Lc:\r\n");
- initList(Lc);
- mergeList(La, Lb, Lc);
- printList(Lc);
-
- }