數據對象集:線性表是N(>=0)個元素構成的有序序列,a1,a2,a3.....a(N-1),aN,a(N+1)node
線性表上的基本操做有:數組
⑴ 線性表初始化:Init_List(L)
初始條件:表L不存在操做結果:構造一個空的線性表
⑵ 求線性表的長度:Length_List(L)
初始條件:表L存在
操做結果:返回線性表中的所含元素的個數
⑶ 取表元:Get_List(L,i)
初始條件:表L存在且1<=i<=Length_List(L)
操做結果:返回線性表L中的第i個元素的值或地址
⑷ 按值查找:Locate_List(L,x),x是給定的一個數據元素。
初始條件:線性表L存在
操做結果:在表L中查找值爲x的數據元素,其結果返回在L中首次出現的值爲x的那個元素的序號或地址,稱爲查找成功; 不然,在L中未找到值爲x的數據元素,返回一特殊值表示查找失敗。
⑸ 插入操做:Insert_List(L,i,x)
初始條件:線性表L存在,插入位置正確(1<=i<=n+1,n爲插入前的表長)。
操做結果:在線性表L的第i 個位置上插入一個值爲x 的新元素,這樣使原序號爲i , i+1, ... , n 的數據元素的序號變爲i+1,i+2, ... , n+1,插入後表長=原表長+1。
⑹ 刪除操做:Delete_List(L,i)
初始條件:線性表L存在,1<=i<=n。
操做結果:在線性表L中刪除序號爲i的數據元素,刪除後使序號爲i+1, i+2,..., n的元素變爲序號爲i, i+1,...,n-1,新表長=原表長-1。數據結構
線性表的存儲:順序存儲spa
1 #include<stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define max 10 6 typedef struct{ 7 int data[max]; //數組 8 int last; //最後一個數據在數組中的位置 9 }LIST; 10 11 //初始化 12 LIST * makeEmpty(); 13 //查找,返回數據在數組中的位置,不存在返回-1 14 int find(int,LIST *); 15 //插入,成功1,失敗-1 16 int insert(int site,int num,LIST * Ptrl); 17 //打印數據結構 18 void dump(LIST *); 19 //刪除 20 int del(int site,LIST * Ptrl); 21 22 void main(void) 23 { 24 int num; 25 LIST * demo; 26 demo = makeEmpty(); 27 28 29 } 30 31 //初始化 32 LIST * makeEmpty() 33 { 34 LIST * Ptrl; 35 Ptrl = (LIST*)malloc(sizeof(LIST)); 36 Ptrl->last = -1; //爲空時last爲1 37 return Ptrl; 38 } 39 //查找,返回數據在數組中的位置,不存在返回-1 40 int find(int num,LIST * Ptrl) 41 { 42 int i=0; 43 while(i<=Ptrl->last && Ptrl->data[i]!=num) 44 { 45 i++; 46 } 47 //若是i大於數據的長度,則就是沒有找到 48 if(i > Ptrl->last) 49 return -1; 50 //返回數據的位置 51 return i; 52 } 53 //插入,成功1,失敗-1 54 int insert(int site,int num,LIST * Ptrl) 55 { 56 //一、判斷是否已經滿了,最後的位置=長度-1 57 if(Ptrl->last == max-1){ 58 return -1; 59 } 60 //二、判斷要插入的位置是否大於等於長度 site >= max 61 if(site<0 || Ptrl->last >= max){ 62 return -1; 63 } 64 //三、從最後一個數據開始移動,下一位填充上一位的數據 ,data[n+1] = data[n],data[n]=data[n-1],直到n>=site 65 int i; 66 for(i=Ptrl->last;i>=site;i--) 67 { 68 Ptrl->data[i+1] = Ptrl->data[i]; 69 } 70 Ptrl->data[site] = num; 71 Ptrl->last += 1; 72 return 1; 73 } 74 75 //打印數據結構 76 void dump(LIST * Ptrl){ 77 int i=0; 78 while(i<=Ptrl->last) 79 { 80 printf("%d=>%d---%d\n",i,Ptrl->data[i],Ptrl->last); 81 i++; 82 } 83 } 84 //刪除 85 int del(int site,LIST * Ptrl) 86 { 87 //檢測刪除的位置是否存在 88 if(site > Ptrl->last || site<0){ 89 return -1; 90 } 91 92 int i; 93 for(i=site;i<=Ptrl->last;i++) 94 { 95 Ptrl->data[i] = Ptrl->data[i+1]; 96 } 97 Ptrl->last--; 98 return 1; 99 }
線性表的存儲:鏈式存儲指針
存放數據元素信息的稱爲數據域,存放其後繼地址的稱爲指針域。所以n個元素的線性表經過每一個結點的指針域拉成了一個「鏈子」,稱之爲鏈表。由於每一個結點中只有一個指向後繼的指針,因此稱其爲單鏈表。
鏈表是由一個個結點構成的,結點定義以下:
typedef struct node{code
datatype data;
struct node *next;
} LNode,*LinkList;
定義頭指針變量:
LinkList H;對象
1 #include<stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define max 10 6 typedef struct list{ 7 int data; 8 struct list * next; 9 }LIST; 10 11 //初始化 12 LIST * initialize(); 13 //按key查找 14 LIST * findByKey(int key,LIST * Ptrl); 15 //按value查找 16 LIST * findByVal(int value,LIST * Ptrl); 17 //插入 18 int insert(int key,int val,LIST * Ptrl); 19 //刪除 20 int delete(int key,LIST * Ptrl); 21 //表長 22 int length(LIST * Ptrl); 23 //打印 24 void dump(LIST * Ptrl); 25 26 void main(void) 27 { 28 LIST * demo; 29 demo = initialize(); 30 insert(1,22,demo); 31 insert(2,222,demo); 32 LIST * find = findByVal(22,demo); 33 //demo = initialize(); 34 dump(demo); 35 //printf("%d",length(demo)); 36 } 37 38 //初始化 39 LIST * initialize() 40 { 41 LIST * Ptrl; 42 Ptrl = (LIST *)malloc(sizeof(LIST)); 43 if(Ptrl==NULL){ 44 puts("molloc fail in line 38\n"); 45 exit; 46 } 47 Ptrl->next = NULL; 48 Ptrl->data = 0; 49 return Ptrl; 50 } 51 //按key查找 52 LIST * findByKey(int key,LIST * Ptrl) 53 { 54 if(key==0) 55 { 56 return Ptrl; 57 } 58 59 LIST * p; 60 p = Ptrl; 61 int i=0; 62 63 while(p!=NULL && i<key) 64 { 65 p = p->next; 66 i++; 67 } 68 if(i==key){ 69 return p; 70 } 71 return NULL; 72 } 73 //按value查找 74 LIST * findByVal(int value,LIST * Ptrl) 75 { 76 LIST * p; 77 p = Ptrl; 78 while(p!=NULL && p->data!=value) 79 { 80 p=p->next; 81 } 82 if(p != NULL){ 83 return p; 84 }else{ 85 return NULL; 86 } 87 } 88 89 //插入 90 int insert(int key,int val,LIST * Ptrl) 91 { 92 LIST * p,* find; 93 94 p = (LIST *)malloc(sizeof(LIST)); 95 if(Ptrl==NULL){ 96 puts("molloc fail in line 86\n"); 97 exit; 98 } 99 100 101 find = findByKey(key-1,Ptrl); 102 103 p->data = val; 104 p->next = find->next; 105 find->next = p; 106 107 return 1; 108 } 109 //刪除 110 int delete(int key,LIST * Ptrl) 111 { 112 //檢查長度的合法性 113 114 //刪除 115 LIST * p,* find; 116 p = findByKey(key,Ptrl); 117 find = findByKey(key-1,Ptrl); 118 find->next = p->next; 119 free(p); 120 } 121 //表長 122 int length(LIST * Ptrl) 123 { 124 int i=0; 125 LIST * p = Ptrl; 126 while(p!=NULL) 127 { 128 p = p->next; 129 i++; 130 } 131 return i; 132 } 133 //打印 134 void dump(LIST * Ptrl) 135 { 136 LIST * p = Ptrl; 137 while(p!=NULL) 138 { 139 printf("%d\n",p->data); 140 p = p->next; 141 } 142 }
棧的存儲blog
順序存儲string
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #define max 10 5 6 typedef struct list{ 7 int data[max]; 8 int top; 9 }LIST; 10 11 //初始化 12 LIST * initialize(); 13 //判斷是否爲空 14 int isEmpty(LIST *); 15 //判斷是否滿了 16 int isFull(LIST *); 17 //出棧 18 int pop(LIST *); 19 //入棧 20 int push(int value,LIST * Ptrl); 21 //打印 22 void dump(LIST *); 23 24 void main(void) 25 { 26 LIST * demo = initialize(); 27 push(3,demo); 28 push(6,demo); 29 push(9,demo); 30 pop(demo); 31 dump(demo); 32 } 33 34 //初始化 35 LIST * initialize() 36 { 37 LIST * Ptrl; 38 Ptrl = (LIST *)malloc(sizeof(LIST)); 39 if(Ptrl == NULL) 40 { 41 puts("malloc error!!!\n"); 42 exit; 43 } 44 Ptrl->top=-1; 45 return Ptrl; 46 } 47 //判斷是否爲空 48 int isEmpty(LIST * Ptrl) 49 { 50 if(Ptrl->top == -1) 51 { 52 return 0; 53 } 54 return 1; 55 } 56 57 //判斷是否滿了 58 int isFull(LIST * Ptrl) 59 { 60 if(Ptrl->top >= max-1) 61 { 62 return 0; 63 } 64 return 1; 65 } 66 //出棧 67 int pop(LIST * Ptrl) 68 { 69 if(isEmpty(Ptrl) == 0) 70 { 71 puts("is empty"); 72 exit; 73 } 74 75 int temp = Ptrl->data[Ptrl->top]; 76 Ptrl->top--; 77 return temp; 78 } 79 //入棧 80 int push(int value,LIST * Ptrl) 81 { 82 if(isFull(Ptrl) == 0) 83 { 84 puts("is full"); 85 exit; 86 } 87 Ptrl->top++; 88 Ptrl->data[Ptrl->top] = value; 89 return 1; 90 } 91 //打印 92 void dump(LIST * Ptrl) 93 { 94 if(isEmpty(Ptrl) == 0) 95 { 96 puts("is empty"); 97 exit; 98 } 99 100 int i; 101 for(i=Ptrl->top;i>=0;i--) 102 { 103 printf("%d\n",Ptrl->data[i]); 104 } 105 }
鏈式村粗it
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #define max 10 5 6 typedef struct list{ 7 int data; 8 struct list * next; 9 }LIST; 10 11 //初始化 12 LIST * initialize(); 13 //判斷是否爲空 14 int isEmpty(LIST *); 15 //判斷是否滿了 16 int isFull(LIST *); 17 //出棧 18 int pop(LIST *); 19 //入棧 20 int push(int value,LIST * Ptrl); 21 //打印 22 void dump(LIST *); 23 24 void main(void) 25 { 26 LIST * demo = initialize(); 27 push(2,demo); 28 push(5,demo); 29 push(6,demo); 30 push(8,demo); 31 pop(demo); 32 dump(demo); 33 } 34 35 36 //初始化 37 LIST * initialize() 38 { 39 LIST * Ptrl; 40 Ptrl = (LIST *)malloc(sizeof(LIST)); 41 Ptrl->next = NULL; 42 //Ptrl->data = -1; 43 return Ptrl; 44 } 45 //判斷是否爲空 46 int isEmpty(LIST * Ptrl) 47 { 48 if(Ptrl->next == NULL) 49 { 50 return 0; 51 } 52 return 1; 53 } 54 55 //判斷是否滿了 56 int isFull(LIST * Ptrl) 57 { 58 59 } 60 //出棧 61 int pop(LIST * Ptrl) 62 { 63 if(isEmpty(Ptrl)==0){ 64 puts("is empty"); 65 exit; 66 } 67 LIST * temp = Ptrl->next; 68 int data = temp->data; 69 Ptrl->next = temp->next; 70 free(temp); 71 return data; 72 } 73 //入棧 74 int push(int value,LIST * Ptrl) 75 { 76 LIST * temp = (LIST *)malloc(sizeof(LIST)); 77 temp->next = Ptrl->next; 78 temp->data = value; 79 Ptrl->next = temp; 80 return 1; 81 } 82 //打印 83 void dump(LIST * Ptrl) 84 { 85 if(isEmpty(Ptrl)==0){ 86 puts("is empty"); 87 exit; 88 } 89 LIST * p = Ptrl->next; 90 while(p != NULL) 91 { 92 printf("%d\n",p->data); 93 p = p->next; 94 } 95 96 }