单链表面试经典问题
/**************************************************
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;
}
總結
- 上一篇: 【转载】RocketMQ优秀文章
- 下一篇: 亲测!FileUtils不是线程安全的使