順序表的建立

2022-06-24 15:24:11 字數 4455 閱讀 5334

下標法

1 #include 2 #include 3 #include 

4 #include 5

#define error 0

6#define ok 1

7#define maxsize 1000000

8using

namespace

std;

9 typedef int

status;

10 typedef int

elemtype;

11 typedef struct

12sqlist;

16sqlist l;

17sqlist list2;

1819 status initlist(sqlist &l)//

順序表初始化

2027

28void menu()//

選單選項

2941

42 status readfromarray(sqlist &l,int a,int len)//

1.從陣列中讀入資料到順序表

4349 l.length+=len;

50return

ok;51}52

53 status listprint(sqlist &l)//

2.遍歷順序表輸出

5461

62int locateelem(sqlist &l,elemtype s)//

3.查詢,返回元素位置序號

6367

68 status listinsert(sqlist &l,int i,elemtype e)//

4.插入元素

6979

80 status listdelete(sqlist &l,int i)//

5.刪除元素 81

8990 status listchange(sqlist &l,elemtype s,elemtype e)//

6.修改元素

9199

100 status readfromfile(sqlist &l,char *filename)//

7.從檔案中讀入資料到順序表

101109

infile.close();

110return

ok;111

}112

113 status writetofile(sqlist &l,char *filename)//

8.將順序表中資料寫入到檔案

114123 outfile<124outfile.close();

125return

ok;126

}127

128int

main()

129136

else cout<<"

順序表list初始化成功!

"<137 f=initlist(list2);

138if(!f)

139143

else cout<<"

順序表list2初始化成功!

"<144int a[6]=;

145146

menu();

147int

choose;

148while(cin>>choose)

149156

if(choose==2

)157

161else

if(choose==3

)162

169else

if(choose==4

)170

181else

if(choose==5

)182

192else

if(choose==6

)193

204else

if(choose==7

)205

214}

215else

if(choose==8

)216

221222

menu();

223}

224225

return0;

226 }

指標法

1 #include 2 #include 3 #include 

4 #include 5

#define error 0

6#define ok 1

7#define maxsize 1000000

8using

namespace

std;

9 typedef int

status;

10 typedef int

elemtype;

11 typedef struct

12sqlist;

16sqlist l;

17sqlist list2;

1819 status initlist(sqlist &l)//

順序表初始化

2027

28void menu()//

選單選項

2941

42 status readfromarray(sqlist &l,int a,int len)//

1.從陣列中讀入資料到順序表

4353 l.length+=len;

54return

ok;55}56

57 status listprint(sqlist &l)//

2.遍歷順序表輸出

5867 cout<68return

ok;69}70

71int locateelem(sqlist &l,elemtype s)//

3.查詢,返回元素位置序號

7279

return0;

80}8182 status listinsert(sqlist &l,int i,elemtype e)//

4.插入元素

8392

93 ple=l.elem+l.length-1;94

for(int j=l.length-1;j>=i-1;j--)

9599 *(ple+1)=e;

100 l.length++;

101return

ok;102

}103

104 status listdelete(sqlist &l,int i)//

5.刪除元素

105115 l.length--;

116return

ok;117

} 118

119 status listchange(sqlist &l,elemtype s,elemtype e)//

6.修改元素

120129

130 status readfromfile(sqlist &l,char *filename)//

7.從檔案中讀入資料到順序表

131142

infile.close();

143return

ok;144

}145

146 status writetofile(sqlist &l,char *filename)//

8.將順序表中資料寫入到檔案

147158 outfile<159outfile.close();

160return

ok;161

}162

163int

main()

164171

else cout<<"

順序表list初始化成功!

"<172 f=initlist(list2);

173if(!f)

174178

else cout<<"

順序表list2初始化成功!

"<179int a[6]=;

180181

menu();

182int

choose;

183while(cin>>choose)

184191

if(choose==2

)192

196else

if(choose==3

)197

204else

if(choose==4

)205

216else

if(choose==5

)217

227else

if(choose==6

)228

239else

if(choose==7

)240

249}

250else

if(choose==8

)251

256257

menu();

258}

259260

return0;

261 }

完。

順序表的建立

定義 資料結構是相互之間存在一種或多種特定關係的資料元素的集合。根據資料元素之間關係的不同特性,通常有如下4類基本結構 順序表的特徵是,在記憶體中占用連續的儲存單元,可以簡單的理解為順序表就是陣列。只是根據需要,在實際應用中動態分配順序表占用的記憶體單元。而陣列是在編譯的時候,預分配了指定大小的記憶...

順序建立鍊錶

資料結構實驗之鍊表一 順序建立鍊錶 timelimit 1000ms memory limit 65536k 題目描述輸入n 個整數,按照輸入的順序建立單鏈表儲存,並遍歷所建立的單鏈表,輸出這些資料。輸入第一行輸入整數的個數n 第二行依次輸入每個整數。輸出輸出這組整數。示例輸入 81256 4 6 ...

順序建立鍊錶

對鍊錶的操作今天上課聽的有點懵,沒反應過來,主要還是當時沒學太好,所以忘得更徹底,剛剛重新學了一波,趕緊總結一下 題目 輸入n個整數,按照輸入的順序建立單鏈表儲存,並遍歷所建立的單鏈表,輸出這些資料。input 第一行輸入整數的個數n 第二行依次輸入每個整數。輸出這組整數。sample input ...