所有節點都是在程式中建立的,不是臨時開闢的,也不能用完後釋放,這種鍊錶稱之為靜態鍊錶。
例下:
#include
struct student
;void
main()
}
指在程式執行過程中,從無到有地建立起乙個鍊錶,即乙個乙個地開闢節點和輸入各個節點的資料,並建立起前後相鏈的關係。
(1). 建立鍊錶
帶頭鍊錶
(建立帶頭鍊錶時,也需要給頭節點分配記憶體單元,否則會出現錯誤)
struct student *
creat()
//建立帶頭鍊錶
q =null
;return head;
}
不帶頭鍊錶
struct student *
creat1()
//建立不帶頭
q->next =
null
;return head;
}
2. 輸出鍊錶
帶頭鍊錶
void
print
(struct student *head)
//輸出帶頭鍊錶所有節點
}}
不帶頭結點
void
print1
(struct student *head)
//輸出不帶頭鍊錶所有節點
}}
3.刪除節點
(1)帶頭鍊錶
根據學生學號,刪除帶頭節點的函式,刪除後返回頭指標
struct student *
delete
(struct student *head,
int studentnum)
//刪除函式,帶頭鍊錶,根據學號刪除
p=p->next;
q=q->next;
}return head;
}
(2)不帶頭鍊錶
不帶頭結點的刪除,需要多考慮,要刪除的結點是否為第乙個結點。
struct student *
delete1
(struct student *head,
int studentnum)
//不帶頭鍊錶,根據學號刪除刪除
else
p=p->next;
q=q->next;}}
return head;
}
3.增加結點
帶頭鍊錶增加節點
struct student *
add(
struct student * head,
int studentnum,
int studentscore)
q->next= temp;
q= temp;
q->next =
null
;return head;
}
4.更改結點
struct student *
updata
(struct student *head,
int studentnum,
int studentscore)
p = p->next;
}return head;
}
所有原始碼:
#include
#include
struct student *
creat()
;//建立帶頭鍊錶
struct student *
creat1()
;//建立不帶頭鍊錶
void
print
(struct student *head)
;//輸出帶頭鍊錶所有節點
void
print1
(struct student *head)
;//輸出不帶頭鍊錶所有節點
struct student *
delete
(struct student *head,
int studentnum)
;//帶頭鍊錶,根據學號刪除
struct student *
delete1
(struct student *head,
int studentnum)
;//不帶頭鍊錶,根據學號刪除
struct student *
add(
struct student * head,
int studentnum,
int studentscore)
;struct student *
updata
(struct student *head,
int studentnum,
int studentscore)
;struct student
;void
main()
struct student *
creat()
//建立帶頭鍊錶
q->next=
null
;return head;
}struct student *
creat1()
//建立不帶頭
q->next =
null
;return head;
}void
print
(struct student *head)
//輸出帶頭鍊錶所有節點}}
void
print1
(struct student *head)
//輸出不帶頭鍊錶所有節點}}
struct student *
delete
(struct student *head,
int studentnum)
//帶頭鍊錶,根據學號刪除刪除
p=p->next;
q=q->next;
}return head;
}struct student *
delete1
(struct student *head,
int studentnum)
//不帶頭鍊錶,根據學號刪除刪除
else
p=p->next;
q=q->next;}}
return head;
}struct student *
add(
struct student * head,
int studentnum,
int studentscore)
q->next= temp;
q= temp;
q->next =
null
;return head;
}struct student *
updata
(struct student *head,
int studentnum,
int studentscore)
p = p->next;
}return head;
}
執行結果:
請輸入學號,分數(用空格隔開,0結束):
10000 100
10001 99
10002 98
10003 97
10004 96
10005 95
0 0學生的資料為:
學號 :10000 分數 :100.00
學號 :10001 分數 :99.00
學號 :10002 分數 :98.00
學號 :10003 分數 :97.00
學號 :10004 分數 :96.00
學號 :10005 分數 :95.00
刪除後學生的資料為:
學號 :10000 分數 :100.00
學號 :10002 分數 :98.00
學號 :10003 分數 :97.00
學號 :10004 分數 :96.00
學號 :10005 分數 :95.00
增加後學生的資料為:
學號 :10000 分數 :100.00
學號 :10002 分數 :98.00
學號 :10003 分數 :97.00
學號 :10004 分數 :96.00
學號 :10005 分數 :95.00
學號 :20000 分數 :100.00
修改後學生的資料為:
學號 :10000 分數 :100.00
學號 :10002 分數 :98.00
學號 :10003 分數 :97.00
學號 :10004 分數 :96.00
學號 :10005 分數 :95.00
學號 :20000 分數 :50.00
C語言鍊錶 應用總結
每次面試的時候,面試官都會問你,會鍊錶不 顯得很高逼格 掌握鍊錶的建立 插入 刪除 修改 清空。你會發現,其實鍊錶就那麼回事。1.建立鍊錶結構體 typedef struct linklist linklist 2.生成乙個鍊錶的頭指標,僅僅 just!是乙個位址 linklist list hea...
c語言 鍊錶 C語言鍊錶例項 玩轉鍊錶
下圖為最一簡單鍊錶的示意圖 第 0 個結點稱為頭結點,它存放有第乙個結點的首位址,它沒有資料,只是乙個指標變數。以下的每個結點都分為兩個域,乙個是資料域,存放各種實際的資料,如學號 num,姓名 name,性別 和成績 score 等。另乙個域為指標域,存放下一結點的首位址。鍊錶中的每乙個結點都是同...
c語言鍊錶 鍊錶
在儲存一大波數的時候,我們通常使用陣列,但有時候陣列顯得不夠靈活,比如有一串已經從小到大排序好的數 2 3 5 8 9 10 18 26 32 現在需要往這串數中插入6使其得到的新序列仍符合從小到大排列。如果我們使用陣列來實現這一操作,則需要將8和8後面的數字都依次往後挪一位,如果你覺得這幾個數不算...