-
2021-05-19 09:27:54
#includetypedef struct Node{
int d;
struct Node *next;
}TNode, *PNode;
//创建具有n个结点的链表(结点数据为1-100之间的随机数)
PNode createList(int n){
PNode head, p;
if(n==0)
return 0;
head=(PNode)malloc(sizeof(TNode));
head->d=rand()%100+1;
head->next=0;
p=head;
while(--n){
p=(PNode)malloc(sizeof(TNode));
p->d=rand()%100+1;
p->next=head;
head=p;
}
return head;
}
//显示链表
void displayList(PNode head){
PNode p=head;
while(p){
printf("%4d",p->d);
p=p->next;
}
printf("\n");
}
//释放链表
void freeList(PNode head){
PNode p;
while(head){
p=head;
head=p->next;
free(p);
}
}
//查找链表中数据值为k的结点
PNode findNode(PNode head, int k){
PNode p=head;
while(p && p->d!=k)
p=p->next;
return p;
}
void main(){
PNode head,p;
int n,k;
printf("n=?");
scanf("%d",&n);
srand(time(0));
head=createList(n);
displayList(head);
printf("k=?");
scanf("%d",&k);
p=findNode(head,k);
if(p==0)
printf("%d 结点不存在。\n", k);
else if(p->next==0)
printf("%d 结点为尾结点(没有直接后继)\n", k);
else
printf("%d 结点的直接后继结点为 %d\n", k, p->next->d);
freeList(head);
}
更多相关内容 -
C语言创建一个线性表,然后输出线性表,如何编写程序?
2021-05-23 11:23:45匿名用户 1级 2017-12-26 回答 #include #include #include #defineOVERFLOW -2 #define OK 1 #define ERROR 0 #defineLIST_INIT_... } } 这个是我最近编写的 顺序表也是线性表的 这里还有链表的程序 用的话再传给你匿名用户
1级
2017-12-26 回答
#include
#include
#include
#defineOVERFLOW -2
#define OK 1
#define ERROR 0
#defineLIST_INIT_SIZE 100
#defineLISTINCREMENT 10
typedef intElemType;
typedef intStatus;
//定义顺序存储结构
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//初始化顺序表
StatusInitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem ) exit(ERROR);
L.length =0;
L.listsize =LIST_INIT_SIZE;
return OK;
}
//自定义创建顺序表
voidCreate_SqList(SqList &L)
{
int c,i=0;
int *newBase;
printf("请输入顺序表元素:\n");
while((scanf("%d",&c))!=EOF)
{
if(i>=L.listsize) //自定义顺序表大小超过初始化大小
{
newBase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
//为初始顺序表以LISTINCREMENT大小重新增加存储空间
if(!newBase)exit(OVERFLOW);
L.elem=newBase;
L.listsize+=LISTINCREMENT;
}
L.elem[i++]=c;
}
L.length=i;
printf("输入的顺序表元素:\n");
for(i=0;i
printf("%d ",L.elem[i]);
printf("\n");
}
//在指定位置插入元素
StatusListInsert(SqList &L,int i,ElemType e)
{
ElemType *p,*q,*newbase;
if(i<1||i>L.length+1)
{
printf("插入位置错误\n");
return(ERROR);
}
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
if(i==L.length) L.elem[i+1]=e;
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
//在指定位置删除元素
StatusListDelete_Sq(SqList &L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>L.length+1)
return ERROR;
p=&(L.elem[i-1]);
*e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length ;
return OK;
}
void main()
{
SqList L;
int m,n;
int location,element;
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n");
exit(ERROR);
}
Create_SqList(L);
for(m=0;m<3;m++)
{
printf("输入插入位置:");
scanf("%d",&location);
while(location>L.length+1||location<1)
{
printf("输入位置错误,请重新输入!\n");
scanf("%d",&location);
}
printf("插入元素:");
scanf("%d",&element);
if(!ListInsert(L,location,element))
{
printf("顺序表插入失败!\n");
exit(ERROR);
}
printf("插入顺序表为:\n");
for(int i=0;i<=L.length -1;i++)
{
printf("%d ",L.elem[i]);
}
printf("\n新顺序表一共有%d个元素。\n",L.length);
}
for(n=0;n<3;n++)
{
printf("输入删除位置:");
scanf("%d",&location);
while(location>L.length||location<1)
{
printf("输入位置错误,请重新输入!\n");
scanf("%d",&location);
}
if(!ListDelete_Sq(L,location,&element))
{
printf("删除错误!\n");
exit(ERROR);
}
printf("被删除的元素为:%d \n",element);
printf("被删除后的顺序表为:\n");
for(int j=0;j<=L.length-1;j++)
{
printf("%d ",L.elem[j]);
}
printf("\n新顺序表一共有%d个元素。\n",L.length);
}
}
这个是我最近编写的 顺序表也是线性表的
这里还有链表的程序 用的话再传给你
-
初识Java语言——顺序表和单向链表(基础知识1——创建一个线性表)
2021-01-01 15:22:24初识Java语言——线性表(基础知识1——创建一个线性表) 在数据结构中,按逻辑将数据的结构分为线性表和非线性表,而线性表按照存储单元是否连续分为顺序表和链表。 一、顺序表 顺序表你可以看成一个数组,因为它的...初识Java语言——线性表(基础知识1——创建一个线性表)
在数据结构中,按逻辑将数据的结构分为线性表和非线性表,而线性表按照存储单元是否连续分为顺序表和链表。
一、顺序表
顺序表你可以看成一个数组,因为它的存储单元是连续的,要访问它的元素,就需要知道首地址和偏移量,进而可以随机访问顺序表当中的值。下面来看一下如何创建一个顺序表吧。(后序补充顺表当中的算法)class ArraysList{ private int usedSize; private int[]num; public ArraysList(){ this.num=new int[5]; } } public class Main{ ArraysList arr=new ArraysList();//此时就算创建了一个存储长度为5的一个顺序表了 }
但是顺序表有个缺点就是当我执行删除或者插入操作时会带动大量的元素进行移动,还有就是如果顺序表扩容时,如果我们仅仅只需再放入一个元素时,那么多出来的存储空间就会 被浪费掉,所以就引申出了链表这个东西。
二、链表
链表和顺序表的性质完全相反(存储结构不连续,永远不会放满,不浪费空间,但是必能做到随机访问数据元素,一切都是在遍历整条链表的前提下进行操作的。下面来看一下如何创建一个单项链表吧。
class Node{ public int data; public Node next; public Node(){ } public Node(int data){ this.data=data; } } //暂时以头插法创建一个单链表,后序会补充尾插和其他功能 public class LinkList{ Node head; public creatLink(int data){ Node node=new Node(data); node.next=this.head; this.head=node; } }
public class Main{ public static void main(String[]args){ LinkList link=new LinkList(); link.creatLink(8); link.creatLink(7); link.creatLink(6); }//这样就创建了一个单向链表,6 7 8 }
以上就是顺序表和单向链表的创建,后序会补充顺序表和链表的一些算法,一起加油!!!!!
-
创建一个简单的线性表
2021-02-27 15:58:10创建一个SeqList.h头文件 #pragma once #include<iostream> using namespace std; //#include"stdlib.h" #include <assert.h> #define DEFAULT_SIZE 30 template<class ElemType> class SeqList {...创建一个SeqList.h头文件
#pragma once #include<iostream> using namespace std; #include <assert.h> #define DEFAULT_SIZE 50 template<class ElemType> class SeqList { protected: //顺序的数据成员 int length;//顺序表当前的最大长度 int maxLength; //顺序表最大的容量 ElemType *elems;//元素存储空间的首地址 public: //顺序表的成员函数 SeqList(int size = DEFAULT_SIZE);//构造函数构造一个空表 SeqList(const ElemType v[], int n, int size = DEFAULT_SIZE);//构造函数,根据数组v构造顺序表 virtual ~SeqList();//析构函数 int Getlength();//取顺序表的长度; void clear();//清空顺序表; void Traverse();//遍历顺序表; int locateelem(const ElemType& e);//指定元素所在位置; void Getlength(int i, ElemType& e)const;//取线性表中第i个元素的值 void setelem(int i, const ElemType& e);//修改线性表中第i个元素的值 void deleteelem(int i, ElemType& e);//删除线性表中第i个元素 void insertelem(int i, ElemType& e);//在顺序表弟i个元素后边插入元素 }; //构造空顺序表 template<class ElemType> SeqList<ElemType>::SeqList(int size) { elems = new ElemType[size];//申请存储空间 assert(elems);//申请空间失败,程序终止 maxLength = size;//设置顺序表的最大容量 length = 0;//当前的线性表长度为0; } //根据数组构造线性表 template<class ElemType> SeqList<ElemType>::SeqList(const ElemType v[], int n, int size) { length = n; elems = new ElemType[size];//申请存储空间’ assert(elems);//申请失败则程序终止; maxLength = size; for (int i = 0; i < length; i++) { elems[i] = v[i];//将数组v中的元素依次放入elems中 } } //析构函数 template<class ElemType> SeqList<ElemType>::~SeqList() { delete[]elems;//释放动态数组的存储空间 cout << "析构函数已调用" << endl; } //清空线性表 template<class ElemType> void SeqList<ElemType>::clear() { length = 0;//直接将顺序表的当前长度改为0即可; } //遍历顺序表 template<class ElemType> void SeqList<ElemType>::Traverse() { cout << "当前顺序表的内容有:" << endl; for (int i = 0; i < length; i++) { cout <<elems[i] <<" "; } cout << " " << endl; } //指定元素在顺序表中的位置 template<class ElemType> int SeqList<ElemType>::locateelem(const ElemType& e) { int i = 0; while (i < length && elems[i] != e) { i++; } return i < length ? i + 1 : 0; } //线性表的长度 template<class ElemType> int SeqList<ElemType>::Getlength() { return length; } //取指定元素的值 template<class ElemType> void SeqList<ElemType>::Getlength(int i, ElemType& e)const { if (i<1 || i>length) { cout << "指定位置不合理" << endl; } else { e = elems[i - 1]; cout << "第i个元素为: " << e << endl; } } //修改指定位置的元素 template<class ElemType> void SeqList<ElemType>::setelem(int i, const ElemType& e) { if (i<1 || i>length) { cout << "指定位置不合理" << endl; } else { elems[i - 1]=e; cout << "第i个元素修改为: " << elems[i - 1] << endl; } } //删除指定位置的元素 template<class ElemType> void SeqList<ElemType>::deleteelem(int i, ElemType& e) { if (i<1 || i>length) { cout << "指定位置不合理" << endl; } else { e = elems[i - 1];//用e返回被删除的元素 for (int j = i; j < length; j++) { elems[j - 1] = elems[j]; } length--; cout << "删除成功" << endl; } } template<class ElemType> void SeqList<ElemType>::insertelem(int i, ElemType& e) { if (length == maxLength) { cout << "顺序表已满,无法插入" << endl; } else if(i<1 || i>length) { cout << "指定位置不合理" << endl; } else { for (int j = length; j >=i; j--) { elems[j] = elems[j-1]; } length++; cout << "插入成功" << endl; } }
线性表.cpp文件
#include<iostream> #include"SeqList.h" #include"stdlib.h" using namespace std; int main() { int v[6] = { 1,2,3,4,5,6 }; SeqList<int>list(v, 6);//初始化一个线性表 cout << "初始线性表为: " << endl; list.Traverse(); cout << "线性表的长度为:" << list.Getlength() << endl; int e; list.deleteelem(1, e); cout << "删除i位置后线性表为: " << endl; list.Traverse(); list.insertelem(1, e); cout << "i位置后插入元素e后线性表为: " << endl; list.Traverse(); list.setelem(1, e); cout << "i位置修改为元素e后线性表为: " << endl; list.Traverse(); list.clear();//清空线性表 system("pause"); return 0; }
-
C++怎样建立一个线性表?
2021-05-25 00:47:15} /***********计算线性表的长度*******************/ int SLLenght(SLType *SL) { return(SL->ListLen);//返回顺序表的元素数量 } /*********插入结点*******************************/ int SLInsert(SLType *... -
建立一个线性表,求平均成绩 (越快分越多),该如何处理
2021-05-25 00:47:40C/C++ code#include #include #include typedef struct Node{char name[20];float score;struct Node* next;}*LinkList;void Reverse(LinkList head){LinkList p1 = head,p2 = head->next,p3;... -
C语言创建线性表
2019-09-20 22:30:55在VS2019中创建的,结构体里定义了默认的构造函数(不清楚这算不算C++的语法),线性表的长度是动态分配的。 Incream_List 线性表扩容函数 ListInsert 插入数据函数 ListInit 初始化时输入一批数据 ListDelete 删除... -
线性表创建(最基础)
2021-03-26 21:27:34线性表:是最基本、最简单、也是最常用的一种数据结构。 前提:在学习数据结构的时候命名不要使用拼音!这是初学者的经常犯得错误。 顺序表的创建: #include<stdio.h> #define N 10 typedef int SLDataType; ... -
线性表的创建(C)
2021-05-24 02:43:28main.c#define _CRT_SECURE_NO_WARNINGS#include "header.h"#... //建立线性表InitList_Sq(&LA);//初始化线性表。。。。。。。。。。。。。。。。。//添加自己的程序代码while (1);}header.h#include #inc... -
java——线性表接口实现
2021-03-16 22:20:39线性表是存储顺序牌类的数据时最常用的数据结构。实现线性表有两种方式。第一种是使用数组存储线性表的元素。...对于线性表的主要操作有:1、提取线性表的元素2、在线性表中插入一个新元素3、从线性... -
实现一种算法,将这两个线性表/数组相加得到一个的新的线性表,并将该线性表/数组输出。
2021-03-24 19:17:47将这两个线性表/数组相加得到一个的新的线性表 题目 输入数据创建两个非空线性表/数组来代表两个正整数,线性表/数组中的每个结点只存储一位数字。实现一种算法,将这两个线性表/数组相加得到一个的新的线性表,并将... -
线性表的创建和基本操作
2021-09-14 21:58:40&&逻辑与 ||逻辑或 线性表是最基本、最简单、也是最常用的一种数据结构。 线性表结构中,数据元素之间通过一对一首位相接的方式连接起来。...为了讨论简化,我们假设每个数据元素是一个整数: . -
使用c语言数组创建线性表-链表
2021-05-21 03:45:14链表操作在c语言里我们通过结构体和数组这两种数据结构构造线性表创建顺表表-数组静态分配内存typedef struct {char data[10];int length;}SqList;bool ListInsert(SqList &L,int i,char e){for (int j = L.... -
创建线性表
2018-09-07 15:15:11#include &lt;stdio.h&gt; #include &...函数功能:创建线性表 参数:无 函数返回值:成功时,返回指向线性表首地址的指针;失败时,返回NULL。 */ LIST * createList(voi -
c语言建立线性表(顺序储存,链式储存,循环,双向)全
2021-09-01 19:27:00二、删除值为val的第一个元素,没有返回-1 三、在非递减有序的有序表中删除多余的相同元素 其余操作 一、将线性表中的所有元素转置 二、两个有序的顺序表合并后任然有序 完整代码 链式储存 存储结构 建立链表 一、... -
实例详解C语言线性表的顺序与实现步骤
2021-05-20 12:20:07数据结构中,线性表是一种入门级的数据结构,线性表分为序列表和链表,在下文中爱站技术频道小编将实例详解C语言线性表的顺序与实现步骤,下面一起跟着爱站技术频道的步伐来学习吧!1.概述通常来说顺序表是在计算机... -
数据结构(Java版)模块2线性表.pptx
2021-03-14 01:01:41上课啦THE POWERPOINT TEMPALTE模块2线性表54123顺序存储结构逻辑结构实例引入应用举例链式存储结构学习目的与要求重点:掌握顺序表和链表的逻辑描述和存储实现难点:顺序表...且具有如下特点:(1)存在唯一的一个被... -
c语言实现线性表
2022-04-01 18:13:26线性表类型: 线性表的初始化: 线性表的尾插法: 线性表的打印 线性表检查增容 线性表尾删法 线性表的头插法 线性表的头删法 线性表的删除 指定位置插入 查找数据 ... -
创建头文件,线性表的建立,(C)2020/9/19
2020-09-20 00:35:21源代码: # include<stdio.h> # include "SqList.h" int main() ... printf("\n请输入您要建立的线性表1的长度n:"); scanf("%d",&n); l->length = n; printf("输入表的长度是%d\n",l-> -
用C++编写线性表
2019-10-27 22:32:54数据结构:用C++编写顺序表 数据结构实验心得 用c++语言编写一段程序,以实现...元素的追加只需要在尾部处增加一个地址,顺序表的长度增大一,把值赋给该地址。 元素的插入稍微复杂一点,要先判断插入的位置... -
使用数组实现线性表
2021-02-23 17:21:26数组是内存中一段连续的存储空间,所以属于线性表的顺序存储结构,初始化一个数组、给定一个变量cll(current list length)代表线性表的长度 数组的特点 数组的性质决定了数组的添加和删除比较慢(给指定下标添加... -
创建线性表,以及表中的基本操作
2016-04-02 19:01:32在这个地方我们通过C语言来演示创建线性表以及对线性表的一些操作。 怎样创建一个线性表: typedef char ElemType 我们创建顺序表都是用来盛放元素 typedef struct { ElemType *elem; int length; int listsize; }... -
线性表的C语言实现
2021-05-22 11:38:22函数声明头文件:function.h#define true 1#define false 0/* 定义.../*线性表的单链表存储结构*/typedef struct l_node{/*声明数据域*/datatype data;/*声明指针域*/struct l_node *next;}l_node, *link_list;/* ... -
Java实现线性表
2021-05-06 21:33:17线性表是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串… 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上... -
数据结构线性表的构建
2020-03-17 08:15:53创建名为线性表的空项目,在创建的线性表中创建线性表cpp文件,之际按c文件的格式写代码即可。 首先要在开头添加两个头文件: #include <conio.h> (不然,getch()没有定义) #include <windows.h> 然后... -
线性表的建立和使用
2018-09-05 22:02:08void CreatList(List * & L, int a[], int n) //初始化线性表,构造一个空的线性表L { int i = 0; L = ( List *) malloc( sizeof( List ) ); while( i ) { L->data[i] = a[i]; i++; } L->... -
如何用c程序建立两个线性表并把他们合并成一个线性表?
2021-05-23 11:24:02满意答案ryan_gan推荐于 2017.11.23采纳率:52%等级:12已帮助:14275人从键盘输入两个链表,通过程序对他们排序,之后按递增顺序合并链表#include "stdio.h"#include "stdlib.h"#include "malloc.h"#define NULL 0... -
C语言实现线性表
2020-10-23 20:01:01#define MAXSIZE 100 //定义线性表最大长度 /*定义顺序表*/ typedef struct { int data[MAXSIZE]; //线性表占用的数组空间 int length; //存储线性表的长度 }SeqList; /*初始化顺序表*/ void InitList(SeqList... -
数据结构C语言版——初始化一个线性表
2017-03-09 20:31:43数据结构C语言版——初始化一个线性表