1.從鍵盤依次輸入10個整數(彼此以若干空格隔開),在記憶體中建立乙個順序表。然後完成以下操作:
① 查詢:輸入乙個欲查詢的整數,找到則顯示第乙個相匹配的整數在順序表中所處的位置,若不存在,則顯示"not found"。
② 刪除:輸入乙個表示欲刪除整數的位置的整數i(注意i的合法性),在順序表中刪除該數,並保證刪除後依然是順序表。
③ 插入:輸入乙個欲插入位置i和欲插入元素e,將e插入到第i個整數之前(注意i的合法性)。
1 #include2 #include3#define maxsize 30
4 typedef int
datatype;
56 typedef struct
node
7 seqlist,*pseqlist;
11//
typedef seqlist* pseqlist;
1213
14pseqlist init_seqlist( );
15pseqlist create_seqlist(pseqlist pl);
16int
location_seqlist (pseqlist pl, datatype x);
17int delete_seqlist(pseqlist pl,int
i );
18int insert_seqlist(pseqlist pl,int
i,datatype x);
19print_seqlist(pseqlist pl);
2021
main()
229394}
959697}
9899
100101 pseqlist init_seqlist(void
)102
109110
pseqlist create_seqlist(pseqlist pl)
111121
return
(pl);
122123
124}
125126
intlocation_seqlist (pseqlist pl,datatype x)
127
134 i=0
;135
while (ilength && pl->data[i]!=x)
136 i++;
137if (i>=pl->length)
138return0;
139else
140return (i+1
);
141142
}143
144int delete_seqlist(pseqlist pl,int
i)145
152if(i<1 || i> pl -> length) /*
檢查刪除位置的合法性
*/153
157//*x= pl ->data[i-1];
158for(j=i;j< pl -> length; j++)
159 pl ->data[j-1]=pl ->data[j]; //
向上移動
160 pl-> length --;
161return (1); /*
刪除成功
*/162
} 163
164int insert_seqlist(pseqlist pl,int
i,datatype x)
165
172if (pl-> length >=maxsize)
173
177178
if(i<1||i>pl->length+1
)179
183184for(j= pl->length-1;j>=i-1;j--)
185 pl->data[j+1]= pl ->data[j]; //
移動元素
186 pl->data[i-1]=x;//
pl->data[i-1]=x;
187 pl->length++;
188return (1
);189
} 190
191print_seqlist(pseqlist pl)
192199
if(pl->length==0
)200
204205 printf("
順序表的長度為%d
",pl->length);
206 printf("
順序表的各元素值為:");
207for(i=0;i<=pl->length-1;i++)
208 printf("
%4d",pl->data[i]);
209return1;
210211 }
動態順序表的增刪改查
順序表還是相對於比較簡單的資料結構,所謂動態,不過是在初始化時 賦予動態空間,所以說,就直接看 了。標頭檔案 pragma once sequence list define crt secure no warnings 1 include include include include typed...
動態順序表的增刪改查及相關操作
順序表的概念及結構 靜態順序表 使用定長陣列儲存。動態順序表 使用動態開闢的陣列儲存 順序表的靜態儲存 define n 100 typedef int datatype typedef struct seqlist seqlist 靜態順序表只適用於知道了需要存多少資料的場景,比較死板,其中的定長...
順序表的基本操作(增刪改查) C語言
一 定義 1 儲存空間一定連續 2 可以進行隨機訪問 二 1 定義結構體 typedef struct sqlist,lpsqlist 2 順序表初始化 lpsqlist createsqlist 3 插入操作 void insertsqlist lpsqlist sql,datatype data...