C语言创建一个线性表,然后输出线性表,如何编写程序?

C语言创建一个线性表,然后输出线性表,如何编写程序?,第1张

#include<stdio.h>

#include<iostream.h>

#include<stdlib.h>

#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=0i<L.lengthi++)

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(++pp<=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=0m<3m++)

{

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=0i<=L.length -1i++)

{

printf("%d ",L.elem[i])

}

printf("\n新顺序表一共有%d个元素。\n",L.length)

}

for(n=0n<3n++)

{

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=0j<=L.length-1j++)

{

printf("%d ",L.elem[j])

}

printf("\n新顺序表一共有%d个元素。\n",L.length)

}

}

这个是我最近编写的 顺序表也是线性表

这里还有链表的程序 用的话再传给你

代码如下:

头文件:

2_1.h

#ifndef  _2_1_H

#define  _2_1_H

typedef void SeqList

typedef void SeqListNode

//创建线性表

SeqList * SeqList_Create(int capacity)

//销毁线性表

void SeqList_DesTroy(SeqList * list)

void SeqList_Clear(SeqList* list)

int SeqList_Length(SeqList* list)

int SeqList_Capacity(SeqList* list)

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)

SeqListNode* SeqList_Get(SeqList* list, int pos)

SeqListNode* SeqList_Delete(SeqList* list, int pos)

#endif

源文件:

// 顺序线性表.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <malloc.h>

#include <stdlib.h>

#include "2_1.h"

typedef unsigned int TSeqListNode

typedef struct {

int len     //长度

int capacity//总长度

TSeqListNode * node//每个节点的指针

} TSeqList

int main()

{

SeqList* list = SeqList_Create(5)//创建线性表

int i = 6//赋值6个变量,已超过线性表最大值 5

int j = 1

int k = 2

int x = 3

int y = 4

int z = 5

int index = 0

SeqList_Insert(list, &i, 7) //将这6个变量插入线性表中

SeqList_Insert(list, &j, 0)

SeqList_Insert(list, &k, 0)

SeqList_Insert(list, &x, 0)

SeqList_Insert(list, &y, 0)

SeqList_Insert(list, &z, 0)

//遍历

for(index=0index<SeqList_Length(list)index++)

{

int* p = (int*)SeqList_Get(list, index)

printf("%d  ", *p)

}

printf("\n")

//删除 *** 作

while( SeqList_Length(list) >0 )

{

int* p = (int*)SeqList_Delete(list, 0)

printf("删除了: %d\n", *p)

}

SeqList_Clear(list)

SeqList_DesTroy(list)

system("pause")

return 0

}

//创建线性表

SeqList * SeqList_Create(int capacity)

{

TSeqList* ret = NULL

if(capacity >= 0)

{

ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode)*capacity)  //为线性表分配空间,包含结 //构体和节点的总大小

}

if(NULL != ret)

{

ret->len = 0

ret->capacity = capacity

ret->node = (TSeqListNode*)(ret + 1)//将节点指向上述分配到的空间的后部分

}

return ret

}

//销毁

void SeqList_DesTroy(SeqList * list)

{

free(list)

}

//清空

void SeqList_Clear(SeqList* list)

{

TSeqList * ret = (TSeqList*)list

if(NULL != ret)

{

ret->len = 0

}

}

//获得线性表的长度

int SeqList_Length(SeqList* list)

{

TSeqList * ret = (TSeqList*)list

int len = -1

if(NULL != ret)

{

len = ret->len

}

return len

}

//线性表的总长度

int SeqList_Capacity(SeqList* list)

{

TSeqList * ret = (TSeqList*)list

int capacity = -1

if(NULL != ret)

{

ret->capacity = capacity

}

return capacity

}

//插入

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)

{

TSeqList * sList = (TSeqList*)list

int i,ret = -1

if((sList != NULL) &&(pos >= 0) &&sList->capacity >= sList->len+1)

{

if(pos >= sList->len)

{

pos = sList->len

}

for(i = sList->leni >posi--)

{

sList->node[i] = sList->node[i-1]

}

sList->node[i] = (TSeqListNode)node

++sList->len

ret = 1

}

return ret

}

//获得指定位置的节点

SeqListNode* SeqList_Get(SeqList* list, int pos)

{

TSeqList * sList = (TSeqList*)list

TSeqListNode* node = NULL

if(NULL != sList &&pos>=0 &&pos <sList->len)

{

node = (TSeqListNode*)sList->node[pos]

}

return node

}

//删除

SeqListNode* SeqList_Delete(SeqList* list, int pos)

{

TSeqList * sList = (TSeqList*)list

SeqListNode * node = SeqList_Get( list, pos)

int i

if(sList != NULL &&pos >= 0 &&pos<sList->len)

{

for( i=pos+1i<sList->leni++)

{

sList->node[i-1] = sList->node[i]

}

sList->len--

}

return node

}

演示:

资料拓展:

线性表是最基本、最简单、也是最常用的一种数据结构。

线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储),但是把最后一个数据元素的尾指针指向了首位结点)。

我们说“线性”和“非线性”,只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表。

在数据结构逻辑层次上细分,线性表可分为一般线性表和受限线性表。一般线性表也就是我们通常所说的“线性表”,可以自由的删除或添加结点。受限线性表主要包括栈和队列,受限表示对结点的 *** 作受限制。

线性表的逻辑结构简单,便于实现和 *** 作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11367388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存