code:

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h> #define INITIA 10 typedef int ElemType; typedef int Status; typedef struct Node
{
ElemType data;
struct Node * next;
}node; node * head = NULL, *p, *q; Status GetElem();
Status LinkListInsert();
Status LinkListDelete(); //获取某个地址的元素
Status GetElem(int i, ElemType e)
{
int j;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p -> next;
++j;
}
if(!p || j > i)
return 0;
e = p -> data;
printf("%d\n",p -> data);
return 1;
}
//向链表某位置插入节点
Status LinkListInsert(int i)
{
int j;
node * s;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p ->next;
++j;
}
if(!p || j > i)
return 0;
s = ( node * ) malloc ( sizeof ( node ) );
s -> data = rand()%100 + 1;
s ->next = p -> next, p -> next = s ;
return 1;
}
//删除链表某节点
Status LinkListDelete(int i, ElemType e)
{
int j;
node * s;
p = head;
j = 1;
while(p && j < i - 1)
{
p = p -> next;
++j;
}
if(!p || j > i)
return 0;
s = p -> next;
p -> next = p -> next -> next;
e = s -> data;
free(s);
s = NULL; return 1;
} int main()
{
char str;
int i;
ElemType e = 0;
srand ( time( 0 ) );
for(i = 0; i < INITIA; i ++)
{
p = ( node * ) malloc ( sizeof ( node ) );
if(head == NULL)
head = p;
else
q ->next = p;
p -> next = NULL;
p -> data = rand()%100 + 1;
q = p;
}
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
printf("\n查找 请按 1 插入数据 请按 2 删除数据 请按 3");
str = getch();
if(str == '1')
{
printf("\n请输入要查找的数的位置:");
scanf("%d\n",&i);
GetElem(i, e);
}
if(str == '2')
{
printf("\n请输入要插入的数的位置:"); //插在原本该位置上数据的前面
scanf("%d",&i);
LinkListInsert(i);
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
}
if(str == '3')
{
printf("\n请输入要删除的数的位置:");
scanf("%d",&i);
LinkListDelete(i, e);
p = head;
while(p)
{
printf("%d ",p -> data);
p = p -> next;
}
}
while(head)
{
p = head;
head = head -> next;
free(p);
}
p = NULL; return 0;
}

# 2017.8.8

  前面的测试并不完整,它有一个BUG,比如插入第一位和删除第一位时都不对。

  解决:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<conio.h> #define N 5 typedef char Element;
typedef int Status; typedef struct Node
{
Element data [20];
struct Node*next;
}Node; Node*head,*tail,*current; void Swap();
void Sort();
int length();
Status GetData();
Status InsertNode();
Status DeleteNode(); void Swap(Element*p, Element*q)
{
Element swap[20];
strcpy(swap, p),strcpy(p, q),strcpy(q, swap);
} void Sort(Node*p)
{
Node*q;
for(p = head->next; p != NULL; p = p->next)
for(q = p->next; q != NULL; q = q->next)
if(*(p->data) > *(q->data))
Swap(p->data, q->data);
} int length(Node*p) //链表长度
{
int i = 1;
p = head->next;
while(p)
{
p = p ->next;
i++;
}
return i;
} Status GetData(Node*p, int i) //获取某节点数据
{
int j = 0;
Element Elem [20]; p = head->next; if(i < 1 || i > length(p))
return 0; while(p && j < i - 1)
{
p = p->next;
j++;
} if(!p || j > i)
return 0;
printf("%s\n",strcpy(Elem, p->data));
return 1;
} Status InsertNode(Node*p, int i, Element Elem [])
{
Node*New;
int j = 1; p = head->next; if(i == 1)
{
New = (Node *)malloc(sizeof(Node));
strcpy(New->data, Elem);
New->next = head->next;
head->next = New;
} if(i < 1 || i > length(p))
return 0; if(i > 1)
{
while(p && j < i - 1)
{
p = p->next;
j++;
} if(!p || j > i)
return 0; New = (Node *)malloc(sizeof(Node));
strcpy(New->data, Elem);
New->next = p->next;
p->next = New;
}
return 1;
} Status DeleteNode(Node*p, int i)
{
int j = 1;
Node*h; p = head->next; if(i == 1)
{
h = head->next;
head->next = p->next;
free(h);
} if(i < 1 || i > length(p))
return 0; if(i > 1)
{
while(p && j < i - 1)
{
p = p->next;
j++;
} if(!p || j > i)
return 0; h = p->next;
p->next = p->next ->next;
free(h);
}
return 1;
} int main()
{
int i, Pos;
char key;
Element Elem[20];
Node*p; head = (Node *)malloc(sizeof(Node));
tail = head;
for(i = 0; i < N; i++)
{
current = (Node *)malloc(sizeof(Node));
gets(current->data);
tail-> next = current;
tail = current;
}
tail->next = NULL; puts("显示数据:");
p = head->next;
while(p)
{
printf("%s ",p->data);
p = p->next;
}
putchar('\n'); puts("按 1 由大到小排序 按 2 插入数据");
puts("按 3 删除数据 按 4 查看某位置的数据");
puts("按 q 退出程序");
while((key = getch()) != 'q')
{
if(key == '1')
{
p = head->next;
Sort(p); p = head->next;
while(p)
{
printf("%s ", p->data);
p = p->next;
}
putchar('\n');
}
if(key == '2')
{
p = head->next;
puts("\n");
printf("请输入要插入的位置/数据:");
scanf("%d %s",&Pos, Elem); InsertNode(p, Pos, Elem); puts("插入完毕!\n显示数据:");
p = head->next;
while(p)
{
printf("%s ", p->data);
p = p->next;
}
}
if(key == '3')
{
p = head->next;
puts("\n");
printf("请输入要删除的数据的位置:");
scanf("%d",&Pos);
DeleteNode(p, Pos); puts("删除完毕!\n显示数据:");
p = head->next;
while(p)
{
printf("%s ",p->data);
p = p->next;
}
}
if(key == '4')
{
p = head->next;
printf("请输入要查看的数据的位置:");
scanf("%d", &Pos); puts("查看数据:");
GetData(p, Pos);
}
} return 0;
}

  

LinkList(链表)的更多相关文章

  1. C_数据结构_链表的链式实现

    传统的链表不能实现数据和链表的分离,一旦数据改变则链表就不能用了,就要重新开发. 如上说示:外层是Teacher,里面小的是node. #ifndef _MYLINKLIST_H_ #define _ ...

  2. 学习笔记 C++ 链表

    今天查了不少关于链表的资料大概理解了链表,为记录只用留笔于此. 链表概述:动态的数据存储单元,可以比数组更加灵活. 链表的组成:存储的数据,下一个节点. 首先让我们用代码完成一个节点. class N ...

  3. PHP数据结构之实现单链表

    学习PHP中,学习完语法,开始尝试实现数据结构,今天实现单链表 <?php class node //节点的数据结构 { public $id; public $name; public $ne ...

  4. C++模拟链表

    C++模拟链表 简易模拟链表,工厂设计模式.. 注意:请不要在操作时产生环状链表,会造成输出链表时陷入无限循环. #include <iostream> #include <stri ...

  5. JAVA基础——链表结构之单链表

    链表:一种数据存储结构.学链表首先要搞懂数组,按朋友的话说,数组和链表的关系就相当于QQ2008和QQ2009. 除非要通过索引频繁访问各个数据,不然大多数情况下都可以用链表代替数组. 链表部分主要要 ...

  6. python经典面试算法题1.4:如何对链表进行重新排序

    本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.4 对链表按照如下要求重新排序 [微软笔试题] 难度系数: ...

  7. 使用OC实现单链表:创建、删除、插入、查询、遍历、反转、合并、判断相交、求成环入口

    一.概念 链表和数组都是一种线性结构,数组有序存储的,链表是无序存储的. 数组中的每一个元素地址是递增或者递减的关系,链表的每一个节点的地址没有此规律,它们是通过指针的指向连接起来. 链表种类:单链表 ...

  8. 【Weiss】【第03章】链表例程

    这种基础例程,如之前所提,会有一个实现和一个简单的测试代码. 链表其实没什么可说的,其实包括后面的栈和队列也没什么可说的,直接放代码吧. 下面这个是测试代码 #include <iostream ...

  9. C++实现链表的相关基础操作

    链表的相关基础操作 # include <iostream> using namespace std; typedef struct LNode { int data; //结点的数据域 ...

随机推荐

  1. docker容器 - 宿主机和容器之间复制文件(cp)

    实验环境 CentOS 7.5 容器 容器是镜像的运行实例.不同的是,镜像是静态的只读文件,而容器带有运行时需要的可写文件层:同时,容器中的应用进程处于运行状态. 主机和容器之间复制文件 要想实现在主 ...

  2. luckyframe的一些坑

    建议使用idea运行 1.第一次运行访问http://localhost:8088/LuckyFrameServer 2.luckyframe提示“javax.net.ssl.SSLKeyExcept ...

  3. IntelliJ IDEA 2017.3尚硅谷-----显示行号和方法分隔符

  4. JavaWeb02-JSP数据交互

    01.页面编码格式 001.jsp页面本身的编码 page指令中的 pageEncoding属性! 002.浏览器渲染页面采用的编码 contentType属性 003.服务器保存数据采用的编码(re ...

  5. Windows下MD5校验

    参考博客:https://www.cnblogs.com/liubinghong/p/9299276.html 参考博客:https://www.jianshu.com/p/1e1d56552e03 ...

  6. 在Docker中使用Microsoft SQL Server数据库

    下图中对SQL Server容器创建及数据库创建等操作进行了记录,方便自己日后查看.(文中的 * 仅表示隐藏自己的个人信息,手动马赛克,哈哈-) Docker下载可看上一篇博文mac系统,docker ...

  7. session跨域共享问题解决方案

    在讨论 session 跨域共享问题之前,我们首先要了解 session 做了什么,没做什么 1.HTTP是无状态的,也就是说服务器不知道谁访问过他,但是有时间,又需要我们去保留这个状态比如说用户的登 ...

  8. hadoop学习笔记(四):HDFS文件权限,安全模式,以及整体注意点总结

    本文原创,转载注明作者和原文链接! 一:总结注意点: 到现在为止学习到的角色:三个NameNode.SecondaryNameNode.DataNode 1.存储的是每一个文件分割存储之后的元数据信息 ...

  9. 封装一个漂亮的ant design form标签组件

    在ant design 的form组件中 能用于提交的组件比较少,所以我在这写了一个可以单选.多选标签提交的组件,调用非常简单. 代码: import React,{Fragment} from 'r ...

  10. Travel in desert

    传送门 不算难吧 应该有思路的 还是太水了吧 (而且和货车运输很像的啊 ---------------------------------------------------------------- ...