C 編寫鍊錶實現插入和刪除等操作

2021-10-05 09:07:59 字數 2711 閱讀 4757

【問題描述】

編寫鍊錶實現插入和刪除等操作;

根據所給主函式以及輸入輸出格式完整**

【樣例輸入輸出格式】

請輸入學生姓名:

zhang

wang

lizhao

sunqian

學生資訊為:

2018001 li

2018002 wang

2018003 zhang

2018004 zhao

2018005 sun

2018006 qian

請輸入要刪除的學生學號:

2018004

學生資訊為:

2018001 li

2018002 wang

2018003 zhang

2018005 sun

2018006 qian

請輸入要插入的學生學號和姓名:

2018004 han

學生資訊為:

2018001 li

2018002 wang

2018003 zhang

2018004 han

2018005 sun

2018006 qian

題目給出主函式為:

int  main()

for (int i = 2018004; i <= 2018006; i++) //依次輸入名字

cout <

list1.print();

cout << "請輸入要刪除的學生學號:"<> id;

cout << "學生資訊為:" << endl;

list1.deletenode(id);

list1.print();

cout << "請輸入要插入的學生學號和姓名:"<> id >>name;

cout << "學生資訊為:" << endl;

list1.insertnode(id, name);

list1.print();

return 0;

}

最終總程式為:

#include

#include

using

namespace std;

class

node

;node::

node

(int id,string name)

//接受編號和姓名

void node::

showmessage()

//輸出資料

class

linklist

;linklist::

linklist()

void linklist::

addnodeathead

(int id,string name)

//頭插入

}void linklist::

addnodeattail

(int id,string name)

//尾插入

}void linklist::

insertnode

(int id,string name)

//按id順序插入

if(q!=

null

)else}}

void linklist::

deletenode

(int id)

//刪除某個id的資訊

else

if(head-

>mid==id)

else

if(p)

//在鍊錶中找到該元素

q->next=p-

>next;

else

flag=0;

}}}void linklist::

print()

//列印該鍊錶的基本資訊

else}}

intmain()

for(

int i =

2018004

; i <=

2018006

; i++

)//依次輸入名字

cout <<

"學生資訊為:"

<< endl;

list1.

print()

; cout <<

"請輸入要刪除的學生學號:"

<

cin >> id;

cout <<

"學生資訊為:"

<< endl;

list1.

deletenode

(id)

; list1.

print()

; cout <<

"請輸入要插入的學生學號和姓名:"

<

cin >> id >>name;

cout <<

"學生資訊為:"

<< endl;

list1.

insertnode

(id, name)

; list1.

print()

;return0;

}

輸出結果如下圖:

鍊錶實現建立 插入 刪除 求鏈長等操作

include include using namespace std define error 0 define ok 1 typedef char elemtype 根據實際情況定義資料元素型別 typedef int status typedef struct lnode lnode,link...

順序鍊錶的插入刪除等操作 C語言

順序表的操作 include stdio.h include string.h include stdlib.h 定義表的最大長度 define maxlen 100 定義結點的關鍵字 typedef struct data 定義順序表的結構 typedef struct sltype 初始化順序表...

鍊錶插入刪除操作

include using namespace std 定義單向鍊錶節點 struct listnode end of listnode 將新節點插入煉表頭 void insertlist listnode head,int insertdata listnode pnode new listnod...