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. bugku 求getshell

    要修改三个地方 根据大佬们的writeup,要修改三个地方: 1.扩展名filename 2.filename下面一行的Content-Type:image/jpeg 3.最最最重要的是请求头里的Co ...

  2. 吴裕雄 python 机器学习——人工神经网络与原始感知机模型

    import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ...

  3. properties文件读写工具类PropertiesUtil.java

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  4. idea2019.2激活至2089年!

    上图! 激活到2089年8月,绝对够用! ​ 注意:在激活之前,无需改动 host 文件. 资料自取:链接:https://pan.baidu.com/s/1MzX5ewt6lbzHYuggP5sGE ...

  5. Could not set property of class with value There is no setter for property named

    检查entity中类的属性与MAPPER中的resultMap属性是否一致

  6. sping中AOP

    委托代理的概念: 委托类对象就是我们后面说到的"目标对象", 也就是需要[被]代理的对象 target代理类对象就是我们后面说到的"代理对象",目标对象就是需要 ...

  7. 一个Log-Tan积分

    \[\Large\int_{0}^{\pi }\theta \ln\tan\frac{\theta }{2}\mathrm{d}\theta \] \(\Large\mathbf{Solution:} ...

  8. ztree-拖拽(排序树)

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - beforeDrag / onDrag / bef ...

  9. linux 系统 vi编辑器下的删除

    vi filename 进入vi模式 首先 最常用的   dd:删除 光标所在的整行:      d1G: 删除光标所在到第一行的所有数据: dG: 删除光标到最后一行的所有数据 : d$:删除光标到 ...

  10. SpringCloud全家桶学习之概览(一)

    一.概览 根据百度百科的描述,微服务架构是一项在云中部署应用和服务的新技术.而SpringCloud是微服务架构思想的一个具体实现,它为开发人员提供了构建分布式系统中一些常见模式的工具(服务注册与发现 ...