LinkList(链表)
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(链表)的更多相关文章
- C_数据结构_链表的链式实现
传统的链表不能实现数据和链表的分离,一旦数据改变则链表就不能用了,就要重新开发. 如上说示:外层是Teacher,里面小的是node. #ifndef _MYLINKLIST_H_ #define _ ...
- 学习笔记 C++ 链表
今天查了不少关于链表的资料大概理解了链表,为记录只用留笔于此. 链表概述:动态的数据存储单元,可以比数组更加灵活. 链表的组成:存储的数据,下一个节点. 首先让我们用代码完成一个节点. class N ...
- PHP数据结构之实现单链表
学习PHP中,学习完语法,开始尝试实现数据结构,今天实现单链表 <?php class node //节点的数据结构 { public $id; public $name; public $ne ...
- C++模拟链表
C++模拟链表 简易模拟链表,工厂设计模式.. 注意:请不要在操作时产生环状链表,会造成输出链表时陷入无限循环. #include <iostream> #include <stri ...
- JAVA基础——链表结构之单链表
链表:一种数据存储结构.学链表首先要搞懂数组,按朋友的话说,数组和链表的关系就相当于QQ2008和QQ2009. 除非要通过索引频繁访问各个数据,不然大多数情况下都可以用链表代替数组. 链表部分主要要 ...
- python经典面试算法题1.4:如何对链表进行重新排序
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.4 对链表按照如下要求重新排序 [微软笔试题] 难度系数: ...
- 使用OC实现单链表:创建、删除、插入、查询、遍历、反转、合并、判断相交、求成环入口
一.概念 链表和数组都是一种线性结构,数组有序存储的,链表是无序存储的. 数组中的每一个元素地址是递增或者递减的关系,链表的每一个节点的地址没有此规律,它们是通过指针的指向连接起来. 链表种类:单链表 ...
- 【Weiss】【第03章】链表例程
这种基础例程,如之前所提,会有一个实现和一个简单的测试代码. 链表其实没什么可说的,其实包括后面的栈和队列也没什么可说的,直接放代码吧. 下面这个是测试代码 #include <iostream ...
- C++实现链表的相关基础操作
链表的相关基础操作 # include <iostream> using namespace std; typedef struct LNode { int data; //结点的数据域 ...
随机推荐
- 神经网络的基础-Graph,Session
张量:基于 Tensorflow 的 NN:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型. 张量:张量就是多维数组(列表),用“阶”表示张量的维度. 0 阶 ...
- 6_13古代象形符号(UVa1103)<图的连通块的应用>
给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 输出说明:For each test case, display its ...
- vue中用 async/await 来处理异步
原文作者:https://www.cnblogs.com/SamWeb/p/8417940.html 昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简 ...
- 主席树板子 p2104
#include<cstdio> #include<algorithm> #include<vector> using namespace std; ; int n ...
- Homebrew安装和Mac使用
软件安装 1.Homebrew安装 ruby环境: curl -sSL https://get.rvm.io | bash -s stable 官网方式: /usr/bin/ruby -e & ...
- Java - Test - TestNG: testng.xml 简介
1. 概述 简介 testng.xml 中的格式, 元素 2. 背景 testng.xml 概述 测试套件 的配置文件 问题 一下生成了那么多内容 我有点看不懂 一上来就看不懂, 其实很正常, 慢慢说 ...
- 设备驱动基础学习--/proc下增加节点
在需要创建一个由一系列数据顺序组合而成的/proc虚拟文件或一个较大的/proc虚拟文件时,推荐使用seq_file接口. 数据结构struct seq_fille定义在include/linux/s ...
- oss创建文件夹
在进行putObject时,第二个参数写成path/your-object即可
- php 基础 字符型转换整形
示例: 可以得出规律:以有效数字开头的,取有效数字.以非有效数字开头的都转换为0:
- if语句的汇编表示
转自:https://blog.csdn.net/u011608357/article/details/22586137 demo: C语言: int max(int x,int y) { if (x ...