C單鏈表操作

2021-06-18 17:11:13 字數 1854 閱讀 7863

今天面試給「宇龍酷派」鄙視了。我想說,其實鍊錶反轉我會!

單鏈表:初始化、建立、顯示、刪除、插入特定位置、刪除特定位置、反轉操作。

#include #include #include #include typedef struct student

node;

//初始化

node* initnode()

head->next = null;

}//輸入資料

node* creatnode(node* head)

node* pre = head;

printf("creatnode: enter a data:\n");

int itmp;

while (scanf("%d", &itmp)) //ctrl+z 結束

tmp->data = itmp;

tmp->next = null;

pre->next = tmp;

pre = tmp;

} return head;

}//顯示

node* printnode(node* head)

node* pre = head->next;

printf("the list is:\n");

while (pre)

printf("\n");

return head;

}//計算

int calcnode(node* head)

node* pre = head->next;

int num = 0;

while (pre)

return num;

}//刪除

void delnode(node* head)

node* delp = head->next;

while (delp)

printf("the head is cleared.\n");

}//刪除指定某個

node* delposnode(node* head, int pos) //pos = 0,1,2..

if (pos > calcnode(head))

node* p1 = head;

node* p2 = p1->next;

node* p3 = p2->next;

int itmp = pos;

while (itmp != 0)

if (p2 != null)

return head;

}//反**1、儲存原先鍊錶,並while遍歷

// 2、取出head節點做新煉表頭

// 3、利用頭插入法將取出的原先元素存入新錶中

node* reversenode(node* head)

node* oldnow = head->next; //遍歷原先鍊錶 節點

head->next = null; //原先鍊錶清空,取出head節點

while (oldnow)

return head;

}//插入

node* insernode(node* head, int pos, int data) //pos = 1,2,..

if (pos > calcnode(head))

node* pre = head;

node* now = pre->next;

int num = pos;

while (pos--) //尋找到位置

if (now != null) //now為當前插入的位置

return head;

}int main(void)

C單鏈表操作

函式順序依次為單鏈表的建立 頭插和尾插法 初始化,判空,遍歷,求鍊錶長度,按值查詢,按位查詢,插入,刪除,銷毀操作 並且在主函式中舉例說了鍊錶的建立,遍歷,求長,刪除,插入操作 高階操作 c迴圈鍊錶 include include typedef int datatype 用datatype 替代 ...

C單鏈表操作

頭指標存放鍊錶元素數量,頭節點開始存放資料,尾節點指向null list.h ifndef list h define list h include include define debug 0typedef struct nodenode node l malloc 分配記憶體 node init...

C單鏈表的操作

單鏈表的建立 刪除結點 插入新結點 遍歷但鍊錶。很多不足之處,希望多多提意見改正 1 鍊錶 1 單鏈表的建立 首先建立乙個節點,將頭結點 q臨時節點 建立節點p。都指向這個節點。接下來迴圈建立鍊錶的每個節點,為了實現人機互動,最好在建立每個節點的時候都詢問是否建立節點,建立下乙個單獨節點結束之後。然...