#include
#include
/*---------------------- 1、單鏈表的 建立 ----------------------------*/
//鍊錶節點的定義
typedef struct node
node;
typedef node *ptrnode;
//建立單鏈表(輸入每個節點的資料,輸入0表示停止建立)
node *createsinglelist(
)else
} tail->next = null; //list的最後乙個節點的next指標置為null
return
head;}
/*---------------------- 2、單鏈表的 測長 ----------------------------*/
//返回單鏈表長度
int lengthoflist(ptrnode head)
return len;
}/*---------------------- 3、單鏈表的 列印 ----------------------------*/
//列印單鏈表
void printlist(ptrnode head)
while
(p != null)
}/*---------------------- 4、單鏈表節點的查詢 ----------------------------*/
//查詢單鏈表pos位置的節點,返回節點指標
//pos從0開始,0返回head
ptrnode search_node(ptrnode head, int pos)
if(pos == 0)
//取得第乙個有效節點的指標
ptrnode p = head->next;
if(p == null)
while
(--pos)
}return p;
}/*---------------------- 5、單鏈表節點的 插入 ----------------------------*/
//在單鏈表pos位置插入節點,返回煉表頭指標
// 即插在第pos個節點之後
//pos從0開始計算,0表示插入到head節點後面
ptrnode insert_node(ptrnode head, int pos, int data)
ptrnode tmp =
(ptrnode)malloc(sizeof(struct node))
; tmp->data = data;
ptrnode p = null;
if(pos == 0)
else
if((p = search_node(head, pos))
!= null)
return
head;}
/*---------------------- 6、單鏈表節點的 刪除 ----------------------------*/
// 刪除單鏈表的pos位置的節點,返回煉表頭指標
// pos從1開始計算,1表示刪除head後的第乙個節點(即:第乙個有效節點)
ptrnode delete_node(ptrnode head, int pos) // pos >= 1
// else
p = search_node(head, pos - 1);if
(p != null && p->next != null)
return
head;}
}/*---------------------- 7、單鏈表的逆置 ----------------------------*/
ptrnode reverselist(ptrnode head)
else
head->next = newli;
return
head;}
}/*---------------------- 8、尋找單鏈表的中間元素 ----------------------------*/
ptrnode search_middle_node(ptrnode head)
else
}return middle;
}/*---------------------- 9、單鏈表的正向排序(建立鍊錶時) ----------------------------*/
ptrnode insertsort(void)
else
else
if(curptr->data >= tmp->data)
else}}
}else
}return
head;}
/*---------------------- 10、判斷鍊錶是否存在環形鍊錶問題 ----------------------------*/
// 可參考:
// 判斷是否存在回環
// 如果儲存在,start存放回環開始的節點
// 由於c沒有bool,此處設定:返回1代表true 0代表false
int isloop(ptrnode head, ptrnode *start)
while
(fastptr && fastptr->next && slowprt != fastptr);if
(slowprt == fastptr)
else
}/*---------------------- 11、合併兩個有序的單鏈表 ----------------------------*/
//預設從小到大
ptrnode mergelist(ptrnode head1, ptrnode head2)
else
if(p1->data == p2->data)
else}if
(p1 != null)
curptr->next = p1;
if(p2 != null)
curptr->next = p2;
return headmerge;
}int main(
)// gcc test.c -o out -std=c99
// ./out
C和C 程式設計師面試秘笈
本系列部落格基於董山海的zywang shu.edu.cn 第一章 c c 程式基礎 賦值語句 位運算與邏輯運算以及三元操作符的區別 c 域操作符,注意全域性變數和區域性變數的區別 i i i 先 i ii 自增 1 11,然後再列印 iii i i i 先列印 i ii,再 i ii 自增 111...
《C和C 程式設計師面試秘笈》中存在的錯誤
最近在複習c 面試的考點,所以購買了 c和c 程式設計師面試秘笈 這本書 人民郵電出版社2014.3 isbn 978 7 115 34113 6 因為我只選擇性的看了第1 2 3 5 6 7章 全文共12章 所以本文只列出了這幾個章節中存在的錯誤。頁碼面試題編號 存在的錯誤 p15面試題11 只會...
C 程式設計師面試指南第9章
面試題1 指出下段程式的錯誤,並解釋它為什麼是錯誤的。includeclass base class derive base int main int,char char a class b virtual public a b class c virtual public b c void mai...