日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单链表面试经典问题

發布時間:2024/8/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单链表面试经典问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/************************************************** http://www.cnblogs.com/lifuqing/archive/2011/08/20/List.html http://www.cnblogs.com/wenjiang/p/3310233.html 鏈表經典問題匯總:http://blog.csdn.net/vividonly/article/details/6673758 鏈表有關的常見面試題:http://www.cnblogs.com/newth/archive/2012/05/03/2479903.html ***************************************************/ #include <stdio.h> #include <stdlib.h>typedef int elemType;typedef struct Node { /*定義單鏈表節點類型*/elemType data;struct Node *next; } Node;// 初始化線性鏈表,即置單鏈表表頭指針為空 void initList(Node **pNode) {*pNode = NULL;printf("initList函數執行,初始化成功\n"); }//遍歷鏈表 void printList(Node *pHead) {if (NULL == pHead) {printf("printList函數執行,鏈表為空");} else {while (NULL != pHead) {printf("%d\t", pHead->data);pHead = pHead->next;}printf("\n");} }//向表頭插入元素 int insertListHead(Node **pHead, elemType newElem) {Node *pInsert;pInsert = (Node *)calloc(1, sizeof(Node));if (NULL == pInsert) {return 0;}pInsert->data = newElem;pInsert->next = *pHead;*pHead = pInsert;printf("insertListHead函數運行\n");return 1; }//向表尾插入元素 int insertListTail(Node **pHead, elemType newElem) {Node *pInsert, *nextNode;pInsert = (Node *)calloc(1, sizeof(Node));if (NULL == pInsert) {return 0;}pInsert->data = newElem;if (*pHead == NULL) {*pHead = pInsert;} else {nextNode = *pHead;while (nextNode->next != NULL) {nextNode = nextNode->next;}nextNode->next = pInsert;}return 1; }//鏈表逆序 struct Node* reverseList(struct Node* pHead) {struct Node *curNode, *curNext;curNode = pHead;pHead = NULL;while (curNode){curNext = curNode;curNode = curNode->next;curNext->next = pHead;pHead = curNext;} return pHead; }//判斷鏈表是否為空 int isListEmpty(Node *pHead) {return (pHead == NULL); }//獲取鏈表長度 int getListSize(Node *pHead) {int size = 0;while (pHead != NULL) {size++;pHead = pHead->next;}return size; }//獲取第pos個節點的元素值 int getElement(Node *pHead, int pos, elemType *value) {int i = 1;if (pos < 1) {return 0;}while (pHead != NULL) {if (i == pos) {break;} ++i;pHead = pHead->next;//pHead為第i個節點}if (pHead == NULL) {return 0;} else {*value = pHead->data;return 1;} }//修改鏈表中第pos個節點的值為newValue,并保存,并將原來的值保存到oldValue,修改成功返回1,否則返回0 int modifyElem(Node *pHead, int pos, elemType newValue, elemType *oldValue) {int i = 1;if (pos < 1)return 0;while (pHead != NULL) {if (pos == i)break;++i;pHead = pHead->next;}if (pHead == NULL) {return 0;} else {if (oldValue) {*oldValue = pHead->data;}pHead->data = newValue;return 0;} }//刪除表中的值 int removeElem(Node **pHead, elemType value) {Node *pre, *toBeDeleted;pre = NULL;toBeDeleted = *pHead;while (toBeDeleted != NULL) {if (toBeDeleted->data == value)break;pre = toBeDeleted;toBeDeleted = toBeDeleted->next;}if (toBeDeleted == NULL)return 0;if (pre == NULL) {*pHead = (*pHead)->next;} else {pre->next = toBeDeleted->next;}free(toBeDeleted);return 1; }//清空鏈表,使之成為空鏈表 Node *clearList(Node *pHead) {Node *curNode;while (NULL != pHead) {curNode = pHead;pHead = pHead->next;free(curNode);}return pHead; }int main(int argc, char *argv[]) {elemType newElem;struct Node *pHead;initList(&pHead);scanf("%d", &newElem);while (newElem > 0) {//insertListHead(&pHead, newElem);insertListTail(&pHead, newElem);scanf("%d", &newElem);}printList(pHead);printf("------- list length is %d\n", getListSize(pHead));getElement(pHead, 1, &newElem);printf("the first value is %d\n", newElem);printList(pHead);getElement(pHead, 6, &newElem);printf("the sixth value is %d\n", newElem);printList(pHead);modifyElem(pHead, 6, 8, NULL);printList(pHead);getElement(pHead, 6, &newElem);printf("the sixth value is %d\n", newElem);removeElem(&pHead, 88);printList(pHead);pHead = reverseList(pHead);printList(pHead);pHead = clearList(pHead);return 0; }

總結

以上是生活随笔為你收集整理的单链表面试经典问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。