LinkList Operation
链表典型数据结构:
#define ElemType int
typedef struct LinkNode{
ElemType value;
struct LinkNode* next;
};
相比于顺序结构,链式存储结构好处在于在任意位置插入或者删除元素,操作时间比较短。 1 节点回收以及申请
注意指针和节点的关系,防止产生游离节点。记住不管是节点内容以及指针一定要初始化
LinkList Create_Linkist(int elem){
LinkList head;
head = (LinkList)malloc(sizeof(LinkNode));
if(!head) exit(EXIT_FAILURE);
head->value = elem;
head->next = NULL;
return head;
}
假设head指针指向第一个元素,那么插入元素时
无序插入表头插入
void Insert_Head(LinkList head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = Create_Linkist(elem);
temp->next = head->next;
head->next = temp;
}
}
无序插入表尾插入 (q 为表尾指针)
void Insert_Tail(LinkList &head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = head;
while(temp->next) temp = temp->next;
temp->next = Create_Linkist(elem);;
}
}
有序插入操作递归实现
void InsertSorted(LinkList &head, int elem){
if(head == NULL || head->value >= elem){
LinkList temp = Create_Linkist(elem);
temp->next = head;
head = temp;
}
else InsertSorted(head->next, elem);
}
打印函数
void print(LinkList head){
while(head){
cout<<head->value<<" ";
head = head->next;
}
}
源代码:
#include <iostream>
#include <stdlib.h>
using namespace std; #define ElemType int
typedef struct LinkNode{
ElemType value;
struct LinkNode* next;
}*LinkList; LinkList Create_Linkist(int elem){
LinkList head;
head = (LinkList)malloc(sizeof(LinkNode));
if(!head) exit(EXIT_FAILURE);
head->value = elem;
head->next = NULL;
return head;
} void Insert_Head(LinkList head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = Create_Linkist(elem);
temp->next = head->next;
head->next = temp;
}
} void Insert_Tail(LinkList &head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = head; while(temp->next) temp = temp->next;
temp->next = Create_Linkist(elem);;
}
} void InsertSorted(LinkList &head, int elem){
if(head == NULL || head->value >= elem){
LinkList temp = Create_Linkist(elem);
temp->next = head;
head = temp;
}
else InsertSorted(head->next, elem);
} void print(LinkList head){
while(head){
cout<<head->value<<" ";
head = head->next;
}
} int main()
{
LinkList linkTest;
int testNum;
cout<<"enter test number: ";
cin>>testNum;
linkTest = Create_Linkist(testNum);
print(linkTest);
cout<<linkTest<<endl;
InsertSorted(linkTest, --testNum);
//Insert_Tail(linkTest, ++testNum);
//Insert_Head(linkTest, ++testNum);
cout<<"link after insert: "<<endl;
print(linkTest); system("pause");
return 0;}
版权声明:本文为博主原创文章,未经博主允许不得转载。
LinkList Operation的更多相关文章
- 终端mysql Operation not permitted错误解决方案
前言 前段时间装mysql,就遇到了ln: /usr/bin/mysql: Operation not permitted的错误,网上好多方法都过时了,下边是我的解决方法 原因 这是因为苹果在OS X ...
- SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...
- Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b error: Illegal ...
- TNS-12535: TNS:operation timed out案例解析
一数据库突然连接不上,在自己电脑上使用SQL Developer也连接不上.立即使用SecureCRT连接上了这台服务器,从下面几个方面检查. 1:检查了数据库的状态是否正常 $ sqlplus / ...
- 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted
1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...
- the user operation is waiting
eclipse在编辑完代码保存的时候,弹出一个进度框,等N长时间,标题是"user operation is waiting",里面显示的是building workspace的进 ...
- Python安装pywinauto时遇到error: The read operation timed out解决方法
Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- windows下安装kibana出 "EPERM: operation not permitted
D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...
随机推荐
- Springmvc+Spring+Hibernate搭建方法及实例
Springmvc+Spring+Hibernate搭建方法及实例
- 转:C++中多态是怎样实现的?
多态是一种不同的对象以单独的方式作用于相同消息的能力,这个概念是从自然语言中引进的.例如,动词“关闭”应用到不同的事务上其意思是不同的.关门,关闭银行账号或关闭一个程序的窗口都是不同的行为:其实际的意 ...
- cocos2dx CCControlSwitch
CCControlSwitch也是extension中的控件,本身比较简单,直接上例子 // on "init" you need to initialize your insta ...
- 时尚B2B方兴未艾-Maker’s Row 获100万美元种子投资 |华丽志
时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 |华丽志 华丽志 » 网internet, 时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 由 luxeco 发 ...
- PHPRPC for PHP
14的路 PHPRPC for PHP PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出重定向的.支持分级错误处 ...
- ssh的学习
快毕业了.临走前帮导师搭建了gerrit,git服务器,其中涉及ssh的知识,就总结了下.希望对大家有帮助 一.前言(ssh出世的原因) 万物有因就有果,既然ssh存在,就必然有它存在的理由! 许多网 ...
- 【方法2】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
依据guigui111111的建议:先把Map按Key从大到小排序,然后再把Key和Value互换.这也是一种非常好的思路,我写了一下代码,顺便贴上来,供大家參考与分享. package shuai. ...
- Android内存泄漏分析及调试
尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/13017999 此文承接我的另一篇文章:Android进程的内存管理分析 首先 ...
- Java 网络编程(六) 使用无连接的数据报(UDP)进行通信
连接地址:http://www.cnblogs.com/mengdd/archive/2013/03/10/2952673.html 使用无连接的数据报(UDP)进行通信 什么是Datagram? 数 ...
- 关于LZO和LZOP
LZO 是一个适合实时解压.压缩的压缩库 LZOP 基于LZO库的压缩解压工具 PS:有了压缩解压库LZO,还不能直接操作文件压缩解压,需要LZOP 下载的话直接google吧~~~