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; //结点的数据域 ...
随机推荐
- git 多人开发解决步骤
1.pull -- 开发 -- pull -- 解决冲突(如果有) -- commit -- push (PS 老子今天很烦躁)
- C++继承、多态与虚表
继承 继承的一般形式 子类继承父类,是全盘继承,将父类所有的东西都继承给子类,除了父类的生死,就是父类的构造和析构是不能继承的. 继承的访问权限从两方面看: 1.对象:对象只能直接访问类中公有方法和成 ...
- AS报错:Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 'com.and
build->Rebuid-project 寻找错误根源: 报错里可以发现: Resolved versions for app (26.1.0) and test app (27.1.1) d ...
- if (flag)
var flag = false;//暂停标记if (flag){ alert("aa")}else { alert("bb")}bb if (flag) = ...
- 吴裕雄 python 机器学习——多项式贝叶斯分类器MultinomialNB模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes from skl ...
- Android学习15
Date&Time DatePicker(日期选择器),TimePicker(时间选择器),CalendarView(日期视图): 1.TextClock TextClock可以以字符串格式显 ...
- 20200227英语上课笔记 about advantage and disadvantage
Hello and welcome to class! Remember to keep your microphone off when you are not speaking Pronuncia ...
- 存储引擎:engine
1.表类型: 默认的服务器表类型,通过my.ini文件可以手动修改配置:default-storage- engine=INNODB 在创建表,或者编辑表时,可以指定表的存储引擎: 语法:engine ...
- 在一个C程序中,main()函数可以放在哪?
C语言规定,在一个C程序中,main()函数的位置(). A.必须在系统调用的库函数之后 B.必须在程序的开始 C.必须在程序的最后 D.可以在任意位置 答案:D [解析] 每个C程序有且只有一个主函 ...
- 使用jenkins 实现 .net core项目自动发布到 docker
在Docker内运行Jenkins pull镜像 docker pull jenkins/jenkins:lts Dockerfile FROM jenkins/jenkins:lts USER r ...