/**************************************************
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;
}
单链表面试经典问题
最新推荐文章于 2024-04-04 17:50:07 发布