一.線性表的順序存盤
typedef int ElemType;
typedef struct List { ElemType *data;//動態分配 ,需要申請空間 int length; }List;
0.完整代碼
#include <stdio.h> #include <stdlib.h> #define MaxSize 50 #define TRUE 1 #define FALSE 0 typedef int ElemType ; struct List { ElemType *data;//動態分配 ,需要申請空間 int length; }; void InitList(List *p);//初始化表 int ListInsert(List *p,int i,ElemType e);//插入操作 (前插),在第i個位置插入資料e int ListDelete(List *p,int i);//洗掉操作,洗掉第i個位置資料 int ListFindValue(List L,ElemType e);//按值查找元素e ,回傳e在順序表表的位置 int ListFindLocate(List L,int i);//按位查找第i位的值 int Empty(List L); //判空,如果表為慷訓傳TRUE void PrintList(List L);//輸出操作 int main() { List L; InitList(&L); ListInsert(&L,1,1); ListInsert(&L,2,3); ListInsert(&L,3,5); ListInsert(&L,4,7); ListInsert(&L,5,9); PrintList(L); return 0; } void InitList(List *p) { p->data=https://www.cnblogs.com/hky8220/p/(ElemType*)malloc(sizeof(ElemType)*MaxSize); p->length=0; } int ListInsert(List *p,int i,ElemType e) { if(i<1 || i>p->length+1) { return FALSE;//插入位置不合法 } if(p->length>=MaxSize) { return FALSE;//順序表已滿 } for(int j=p->length;j>=i;j--) { p->data[j]=p->data[j-1]; } p->data[i-1]=e; p->length++; return TRUE; } int ListDelete(List *p,int i) { if(i<1 || i>p->length) { return FALSE; } for(int j=i;j<p->length;j++) { p->data[j-1]=p->data[j]; } p->length--; return TRUE; } int ListFindValue(List L,ElemType e) { for(int i=0;i<L.length;i++) { if(L.data[i]==e) { return i+1; } } return FALSE; } int ListFindLocate(List L,int i) { return L.data[i-1]; } void PrintList(List L) { for(int i=0;i<L.length;i++) { printf("%d ",L.data[i]); } printf("\n"); } int Empty(List L) { if(L.length==0) { return TRUE; } else { return FALSE; } }View Code
1.初始化順序表
void InitList(List *p) { p->data=https://www.cnblogs.com/hky8220/p/(ElemType*)malloc(sizeof(ElemType)*MaxSize); p->length=0; }View Code
2.插入操作 ,在第i個位置插入資料e
int ListInsert(List *p,int i,ElemType e) { if(i<1 || i>p->length+1) { return FALSE;//插入位置不合法 } if(p->length>=MaxSize) { return FALSE;//順序表已滿 } for(int j=p->length;j>=i;j--) { p->data[j]=p->data[j-1]; } p->data[i-1]=e; p->length++; return TRUE; }View Code
3.洗掉操作,洗掉第i個位置資料
int ListDelete(List *p,int i) { if(i<1 || i>p->length) { return FALSE; } for(int j=i;j<p->length;j++) { p->data[j-1]=p->data[j]; } p->length--; return TRUE; }View Code
4.按值查找元素 ,回傳元素在順序表的位置
int ListFindValue(List L,ElemType e) { for(int i=0;i<L.length;i++) { if(L.data[i]==e) { return i+1; } } return FALSE; }View Code
5.按位置查找元素
int ListFindLocate(List L,int i) { return L.data[i-1]; }View Code
6.判斷順序表是否為空,為慷訓傳TRUE
int Empty(List L) { if(L.length==0) { return TRUE; } else { return FALSE; } }View Code
7.顯示順序表
void PrintList(List L) { for(int i=0;i<L.length;i++) { printf("%d ",L.data[i]); } printf("\n"); }View Code
二.線性表的鏈式存盤

typedef int ElemType; typedef struct Node{ ElemType data; struct Node *next; }Node;
0.完整代碼
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define TRUE 1 4 #define FALSE 0 5 typedef int ElemType; 6 typedef struct Node{ 7 ElemType data; 8 struct Node *next; 9 }Node; 10 11 Node* InitNode();//初始化創建頭結點 12 Node* Node_HeadInsert(Node *L);//頭插法建立鏈表 13 Node* Node_TailInsert(Node *L);//尾插法建立鏈表 14 Node* NodeInsert(Node *L,int i);//在第i個位置插入結點 15 Node* NodeDelete(Node *L,int i);//洗掉第i個結點 16 Node* NodeSearchNum(Node *L,int i);//按序號查找 17 Node* NodeSearchValue(Node *L,ElemType x);//按值查找 18 void PrintNode(Node *L);//顯示單鏈表 19 Node* NodeMerge(Node *p,Node *q);//合并兩個遞增鏈表 20 21 22 int main() 23 { 24 Node *L; 25 L=InitNode(); 26 L=Node_TailInsert(L); 27 L=NodeInsert(L,2); 28 PrintNode(L); 29 L=NodeDelete(L,1); 30 PrintNode(L); 31 return 0; 32 } 33 Node* InitNode() 34 { 35 Node *L; 36 L=(Node*)malloc(sizeof(Node)); 37 L->next=NULL; 38 return L; 39 } 40 Node* Node_HeadInsert(Node *L) 41 { 42 Node *s; 43 ElemType x; 44 scanf("%d",&x);//插入結點的值 45 while(x!=9999) 46 { 47 s=(Node*)malloc(sizeof(Node)); 48 s->data=https://www.cnblogs.com/hky8220/p/x; 49 s->next=L->next; 50 L->next=s; 51 scanf("%d",&x); 52 } 53 return L; 54 } 55 56 Node* Node_TailInsert(Node *L) 57 { 58 ElemType x; 59 Node *s,*r=L; 60 scanf("%d",&x); 61 while(x!=9999) 62 { 63 s=(Node*)malloc(sizeof(Node)); 64 s->data=https://www.cnblogs.com/hky8220/p/x; 65 r->next=s; 66 r=s; 67 scanf("%d",&x); 68 } 69 r->next=NULL; 70 return L; 71 } 72 73 Node* NodeInsert(Node *L,int i) 74 { 75 ElemType x; 76 Node *s,*p=NodeSearchNum(L,i-1); 77 printf("輸入插入節點的值:") ; 78 scanf("%d",&x); 79 s=(Node*)malloc(sizeof(Node)); 80 s->data=https://www.cnblogs.com/hky8220/p/x; 81 s->next=p->next; 82 p->next=s; 83 printf("插入完成!\n"); 84 return L; 85 } 86 87 Node* NodeDelete(Node *L,int i) 88 { 89 Node *p,*q; 90 p=NodeSearchNum(L,i-1); 91 q=p->next; 92 p->next=q->next; 93 free(q); 94 printf("洗掉完成!\n"); 95 return L; 96 } 97 98 Node *NodeSearchNum(Node *L,int i) 99 { 100 int count=1;//計數 101 Node *p=L->next; 102 if(i==0) 103 return L; 104 if(i<1) 105 return NULL; 106 while(p&&count<i) 107 { 108 p=p->next; 109 count++; 110 } 111 return p; 112 } 113 114 Node *NodeSearchValue(Node *L,ElemType x) 115 { 116 Node *p=L->next; 117 while(p&&p->data!=x) 118 { 119 p=p->next; 120 } 121 return p; 122 } 123 void PrintNode(Node *L) 124 { 125 Node *p=L->next; 126 printf("單鏈表:"); 127 while(p) 128 { 129 printf("%d ",p->data); 130 p=p->next; 131 } 132 printf("\n"); 133 134 } 135 136 Node* NodeMerge(Node *p,Node *q) 137 { 138 Node *r,*t; 139 r=InitNode(); 140 t=r; 141 while(p->next&&q->next) 142 { 143 if(p->next->data<q->next->data) 144 { 145 t->next=p->next; 146 p->next=p->next->next; 147 t=t->next; 148 } 149 else 150 { 151 t->next=q->next; 152 q->next=q->next->next; 153 t=t->next; 154 } 155 } 156 157 while(p->next) 158 { 159 t->next=p->next; 160 p->next=p->next->next; 161 t=t->next; 162 } 163 164 while(q->next) 165 { 166 t->next=q->next; 167 q->next=q->next->next; 168 t=t->next; 169 } 170 171 free(p); 172 free(q); 173 174 return r; 175 }View Code
1.初始化創建頭結點
Node* InitNode() { Node *L; L=(Node*)malloc(sizeof(Node)); L->next=NULL; return L; }View Code
2.頭插法建立鏈表
Node* Node_HeadInsert(Node *L) { Node *s; ElemType x; scanf("%d",&x);//插入結點的值 while(x!=9999) { s=(Node*)malloc(sizeof(Node)); s->data=https://www.cnblogs.com/hky8220/p/x; s->next=L->next; L->next=s; scanf("%d",&x); } return L; }View Code
3.尾插法建立鏈表
Node* Node_TailInsert(Node *L) { ElemType x; Node *s,*r=L; scanf("%d",&x); while(x!=9999) { s=(Node*)malloc(sizeof(Node)); s->data=https://www.cnblogs.com/hky8220/p/x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; }View Code
4.在第i個位置插入結點
Node* NodeInsert(Node *L,int i) { ElemType x; Node *s,*p=NodeSearchNum(L,i-1); printf("輸入插入節點的值:") ; scanf("%d",&x); s=(Node*)malloc(sizeof(Node)); s->data=https://www.cnblogs.com/hky8220/p/x; s->next=p->next; p->next=s; printf("插入完成!\n"); return L; }View Code
5.洗掉第i個結點
Node* NodeDelete(Node *L,int i) { Node *p,*q; p=NodeSearchNum(L,i-1); q=p->next; p->next=q->next; free(q); printf("洗掉完成!\n"); return L; }View Code
6.按序號查找
Node *NodeSearchNum(Node *L,int i) { int count=1;//計數 Node *p=L->next; if(i==0) return L; if(i<1) return NULL; while(p&&count<i) { p=p->next; count++; } return p; }View Code
7.按值查找
Node *NodeSearchValue(Node *L,ElemType x) { Node *p=L->next; while(p&&p->data!=x) { p=p->next; } return p; }View Code
8.顯示單鏈表
void PrintNode(Node *L) { Node *p=L->next; printf("單鏈表:"); while(p) { printf("%d ",p->data); p=p->next; } printf("\n"); }View Code
9.合并兩個遞增鏈表
Node* NodeMerge(Node *p,Node *q) { Node *r,*t; r=InitNode(); t=r; while(p->next&&q->next) { if(p->next->data<q->next->data) { t->next=p->next; p->next=p->next->next; t=t->next; } else { t->next=q->next; q->next=q->next->next; t=t->next; } } while(p->next) { t->next=p->next; p->next=p->next->next; t=t->next; } while(q->next) { t->next=q->next; q->next=q->next->next; t=t->next; } free(p); free(q); return r; }View Code
輸出示例:

2020-06-27
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/36572.html
標籤:其他
