单链表面试经典问题

/**************************************************
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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508703.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux SO_KEEPALIVE属性,心跳

对于面向连接的TCP socket,在实际应用中通常都要检测对端是否处于连接中,连接端口分两种情况: 1、连接正常关闭,调用close() shutdown()连接优雅关闭,send与recv立马返回错误,select返回SOCK_ERR; 2、连接的对端异常关闭,比如网络断掉,突然断电. 对于第二种情况,判断连接是否断…

Linux下ARM开发环境搭建

本人的系统环境&#xff1a;Linux ubuntu 3.8.0-35-generic #50-Ubuntu SMP Tue Dec 3 01:25:33 UTC 2013 i686 i686 i686 GNU/Linux 1、安装skyeye sudo apt-get install skyeye s kyeye -h可以看到skyeye的版本号为1.2.5也可以到http://sourceforge.jp/projects/sfnet_skyeye…

cygwin开发环境搭建与apt-cyg的应用

1、Cygwin安装 http://www.cygwin.com/下载安装工具 具体安装过程参照http://jingyan.baidu.com/article/6b97984d83dfe51ca2b0bf0e.html 2、Cygwin一些设置 打开Cygwin终端,右击打开 Options...选项 Text可以设置字体的一些属性,如大小、编码,Locale 选择C, Character set 选择…

CentOS下升级python版本

源码安装python 安装python源码所依赖的工具及依赖的库 yum install -y make gcc gcc-c yum install -y bzip2 bzip2-devel yum install zlib-devel openssl openssl-devel -y yum install -y make xz下载安装python源码 从官方网站或者华为镜像源下载所有需的源码包&#xff0…

linux下confstr与uname函数_获取C库与内核信息

#include <stdio.h> #include <sys/utsname.h> //unameint main(int argc, char **argv[]) {struct utsname u;if (uname(&u) ! -1) {printf("获取当前内核的名称和信息如下\n""sysname:%s\n""nodename:%s\n""release:%s\…

linux下getrlimit与sysconf函数

#include <stdio.h> #include <sys/time.h> #include <sys/resource.h>int main(int argc, char *argv[]) {struct rlimit nofile_rlmt;if (getrlimit(RLIMIT_NOFILE, &nofile_rlmt) ! -1) {printf("获取进程最大能打开的文件描述符个数信息:\n&quo…

Linux下environ环境变量操作函数

#include <stdio.h>int main(int argc,char *argv[],char **envptr) {int i0;for(i0; envptr[i]!NULL; i)printf("%s\n",envptr[i]);return 0; } main函数是程序的入口函数,int main(int argc,char *argv[]); argc是程序参数的个数,argv保存参数 与下边的程…

Linux网络编程--聊天室客户端程序

聊天室客户端程序 #define _GNU_SOURCE 1 #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <string.h&…

二叉树学习之二叉查找树

写在前面的话 最近接到几个大学同学研究生毕业不是签华为就是签百度,本人取经得到:操作系统、数据结构与算法、网络编程与数据库是面试中利器。想想自己工作2.5年月薪还不到10K,过着苦逼的码农生活,而他们一出校门就是大放光芒(最起码进入的公司就让人觉得牛逼哄哄的).本人痛定…

二叉树学习之非递归遍历

二叉树递归遍历可谓是学过数据结构的同仁都能想一下就能写出来,但在应聘过程我们常常遇到的是写出一个二叉树非递归遍历函数,接着上篇文章写二叉树的非递归遍历,先难后易,一步一步的来. 先上代码: #include "binarytree.h" #include <stack> #include <queu…

二叉树学习之堆排序

认识堆是从堆排序开始的 二叉堆是完全二叉树或者是近似完全二叉树,堆存储在数组中: 根结点下标为0时,下标为n的元素的子结点下标分别为2*n1,2*n2,其父结点下标为(n-1)/2 二叉堆的特性: 1、父结点的键值总是>(<)任何一个子结点的键值 2、每个结点的左右子树都是二叉堆…

步入github世界

在源码的世界里&#xff0c;越来越多的优秀源码涌现&#xff0c;开源的世界不但代表他的优秀&#xff0c;也代表了他优秀的传播途径。 https://github.com/ github自从2008年现世&#xff0c;可谓是后来者居上。开源代码的公开库&#xff0c;优秀程序员的博客园&#xff0c;热心…

libevent学习__学习历程总结

The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.环境搭建 下载: http:…

redis☞ python客户端

安装 https://pypi.python.org/pypi/redis/ https://github.com/andymccurdy/redis-py 参照官网,安装命令 sudo pip install redis 或者 sudo easy_install redis 亦或 源码包执行sudo python setup.py install实例 >>> import redis >>> r redis.Redis(ho…

Catalan数应用

Catalan数应用 Catalan数应用原理卡特兰数经典应用括号化买票找零组合数与阶乘计算 卡特兰数又称卡塔兰数&#xff0c;是组合数学中一个常出现在各种计数问题中的数列。由以比利时的数学家欧仁查理卡塔兰 (1814–1894)命名。 其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430,…

统计整数n的二进制表示中1的个数

(1)逐位判断&#xff08;位运算&#xff09;int counter_ones(unsigned n){int counter 0;While (n) {counter n&1;n >>1;}return counter;}(2)一个整型不为0&#xff0c;那么二进制表示时&#xff0c;至少包含一位1。如果整数减去1&#xff0c;那么最右边的1变成0…

百度2015校园招聘软件开发笔试题及答案

简单题&#xff08;本题共30分&#xff09; 请简述Tcp-ip的3次握手以及4次挥手过程&#xff1f;并解释为何关闭连接需要4次挥手(10分) 详细答案参见TCP/IP协议三次握手与四次握手流程解析 TCP三次握手、四次挥手过程如下: 通常情况下&#xff0c;一个正常的TCP连接&#xf…

python常见数据结构

Python中常见的数据结构可以统称为容器&#xff08;container&#xff09;。序列&#xff08;如列表和元组&#xff09;、映射&#xff08;如字典&#xff09;以及集合&#xff08;set&#xff09;是三类主要的容器。 一、序列&#xff08;列表、元组和字符串&#xff09; 序列…

理解‘*‘,‘*args‘,‘**‘,‘**kwargs‘

原文Understanding ‘*’, ‘*args’,’**‘and’**kwargs’ 刚开始学习python的时候&#xff0c;对有关args,kwargs,*和**的使用感到很困惑。相信对此感到疑惑的人也有很多。我打算通过这个帖子来排解这个疑惑(希望能减少疑惑)。 让我们通过以下5步来理解&#xff1a; 通过…

MongoDb随笔,PyMongo简单使用

安装MongoDb 【更新2021-07-06】 https://www.mongodb.com/try/download/community 下载对应系统的软件版本&#xff08;CentOS7.9 mongod 4.4.6&#xff09;rpm -ivh mongodb-org-server-4.4.6-1.el7.x86_64.rpm安装服务systemctl start mongod启动服务rpm -ivh mongodb-org…