链表典型数据结构:

#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的更多相关文章

  1. 终端mysql Operation not permitted错误解决方案

    前言 前段时间装mysql,就遇到了ln: /usr/bin/mysql: Operation not permitted的错误,网上好多方法都过时了,下边是我的解决方法 原因 这是因为苹果在OS X ...

  2. 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 ...

  3. 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 ...

  4. TNS-12535: TNS:operation timed out案例解析

    一数据库突然连接不上,在自己电脑上使用SQL Developer也连接不上.立即使用SecureCRT连接上了这台服务器,从下面几个方面检查. 1:检查了数据库的状态是否正常 $ sqlplus / ...

  5. 【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.原因,工作队列被占用,只需 ...

  6. the user operation is waiting

    eclipse在编辑完代码保存的时候,弹出一个进度框,等N长时间,标题是"user operation is waiting",里面显示的是building workspace的进 ...

  7. Python安装pywinauto时遇到error: The read operation timed out解决方法

    Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...

  8. svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法

    今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...

  9. windows下安装kibana出 "EPERM: operation not permitted

    D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...

随机推荐

  1. 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动”

    在VC++2008的项目中,如何显示地指定要使用的C++库的版本? 开发环境:VS2008 SP1 + Win2003 SP2         因为我的VS2008安装了SP1补丁,所以有了9.0.3 ...

  2. hdu 1394 zoj 1484 求旋转序列的逆序数(并归排序)

    题意:给出一序列,你可以循环移动它(就是把后面的一段移动到前面),问可以移动的并产生的最小逆序数. 求逆序可以用并归排序,复杂度为O(nlogn),但是如果每移动一次就求一次的话肯定会超时,网上题解都 ...

  3. java的访问控制(包、访问修饰符、修饰符)

    一.     java的类包 编写好一个源程序后,首先要执行javac命令进行编译源文件,并生成与源文件同名后缀为“.class”的字节码文件(类文件),该类文件与源文件默认在同一个目录中.该类文件是 ...

  4. Device Mapper Multipath(DM-Multipath)

    Device Mapper Multipath(DM-Multipath)能够将server节点和存储阵列之间的多条I/O链路配置为一个单独的设备.这些I/O链路是由不同的线缆.交换机.控制器组成的S ...

  5. BNUOJ29065鸣人的查克拉

    鸣人的查克拉 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: ...

  6. MVC过滤器基本使用

        Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...

  7. Hibernate与iBATIS的比较

    1.出身 hibernate 是当前最流行的o/r mapping框架,它出身于sf.net,现在已经成为jboss的一部分了. ibatis 是另外一种优秀的o/r mapping框架,目前属于ap ...

  8. gsoap 超时(timeout)设置

    参考:http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc19.19 gsoap就不用介绍了,是一个c/c++编写的可用于服务端与客户端的连接工具. ...

  9. YII框架下实现密码修改

    YII2 实现修改密码功能 主要难点: 1.密码加密 YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数 ...

  10. (转)C++中extern “C”含义深层探索

    (转)C++中extern “C”含义深层探索 转自: http://www.cppblog.com/Macaulish/archive/2008/06/17/53689.html 1.引言 C++语 ...