線性表的鍊錶實現 遍歷 查詢 插入 刪除 翻轉

2021-10-01 07:06:02 字數 1975 閱讀 8941

(1)用隨機函式生成10個3位整數(100~999),把這些整數存於鍊錶中;

(2)輸出鍊錶的內容;

(3)讀入乙個整數,檢視該整數是否在表中,若在,輸出其位置(首位置為1);

(4)讀入乙個整數,以及要插入的位置,把該整數插入到鍊錶中,輸出鍊錶的內容(要求判斷輸入的位置是否合理);

(5)讀入乙個整數,若該整數在煉表裡,刪除該整數,輸出鍊錶的內容;

(6)把鍊錶的內容翻轉,輸出鍊錶的內容。

生成函式生成100~999

#include

cout <<

"隨機生成三位整數,並存入鍊錶中"

<< endl;

srand((

int)

time(0

));p-

>date =

rand()

%899

+100

;

#include

#include

#define ok 1

#define error 0

using

namespace std;

typedef

int status;

typedef

int elemtype;

typedef

struct lnode lnode,

*linklist;

status initlist

(linklist &l)

status putlist

(linklist &l)

cout << endl;

return ok;

}status showlist

(linklist &l)

cout << endl;

return ok;

}int

locateelem

(linklist l, elemtype e)

return j;

}status listinsert

(linklist &l,

int i, elemtype &e)if(

!p || j > i -1)

return error;

s->date = e;

s->next = p-

>next;

p->next = s;

return ok;

}status listdelete

(linklist &l,

int a)

q = p-

>next;

p->next = q-

>next;

delete q;

return ok;

}status returnlist

(linklist &l, linklist &l1)

return ok;

}int

main()

else

cout <<

"插入失敗"

<< endl;

break

;case3:

cout <<

"請輸入要刪除的數字:"

; cin >> a;if(

listdelete

(l, a)

)else

cout <<

"刪除失敗"

<< endl;

break

;case4:

if(returnlist

(l, l1)

)else

cout <<

"資料翻轉失敗!"

<< endl;

break

;case5:

cout <<

showlist

(l)<< endl;}}

}

鍊錶插入 線性表

題目描述 線性表 已知一單鏈表,從第二個結點至表尾遞增有序,設a1輸入 輸入長度n 7 輸入資料 4 1 2 3 6 8 9 輸出1 2 3 4 6 8 9 樣例輸入 511 7 8 9 10 樣例輸出 7 8 9 10 11 分析 此題,考察鍊錶的插入。基本思想只要是判斷,在插入。include ...

實現線性表的遍歷查詢插入刪除合併

資料結構 書上給的 僅僅是 在某個特定的位置 插入 某乙個元素。那樣不是 線性表的要求。申請動態記憶體空間神馬的 真費勁,不熟練。include include include include include include include include include include inclu...

線性表的鍊錶實現

include include define elemtype int typedef struct lnode list struct lnode struct lnode l list ptrl list makeempty list ptrl 建立空表 int length list ptrl...