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; //结点的数据域 ...
随机推荐
- Linux07——安装MySQL
①检查工作 CentOS6 rpm -qa|grep mysql 或者 rpm -qa | grep mysql 如果存在mysql-libs的旧版本包如下: 一定要执行卸载呀!!! 卸载命令:rp ...
- sql server2008用ip远程连接
sql server2008用ip远程连接 转载 weixin_34167819 发布于2017-09-14 15:23:00 阅读数 84 收藏 展开 1,2005的外围应用配置器在2008中换了地 ...
- SQL Server 函数大全
本文链接:https://blog.csdn.net/qq_15028299/article/details/81330854SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合.htt ...
- js实现汉字转拼音
汉字转拼音,每个字首字母大写:pinyin.getFullChars(name); 提取首字母并大写:pinyin.getCamelChars(name); /* --- description: P ...
- 无显示器安装raspberry zero w树莓派 zero w
笔者的环境 1. Macbook 电脑用于烧录树莓派系统到SD卡 2. 树莓派 zero w 把SD卡放进读卡器,插到Mac电脑上, 打开命令行工具Terminal diskutil list列出di ...
- vue去掉地址栏# 方法
超简单 export default new Router({ //将mode 设置为‘history‘ 即可.默认情况是’hash’ 所以会有丑陋的# mode: 'history', routes ...
- 03hive_DDL数据定义
一. DDL数据定义 创建数据库 1)create database db_hive; 2)避免要创建的数据库已经存在错误,增加 if not exists 判断. create database i ...
- python web django 2nd level -- 待更新
练习代码位置 实例代码位置 --> app: myblog Form 利用Form表单验证,自己写的html 思路: 新建一个类 LoginForm(forms.Form) 新建对象 obj = ...
- sqlmap 扫描注入漏洞
.检测是否存在漏洞: sqlmap -u .获取数据库信息: sqlmap -u --dbs .数据库表信息: sqlmap -u -D security --tables .表中字段信息 sqlma ...
- Python3 post 嵌套json
目录 python3 post json burpsuite 抓取 python requests 数据包 小结 python3 post json 前些天python3 post出现的小问题做下记录 ...