线性表的建立c语言
‘壹’ c语言中怎么定义个线性表
线性表是数据结构
具体到c中实现
可以有多种方式
比较常见的是数组方式以及链表方式。
‘贰’ 怎样创建线性表(C语言)
线性表是个抽象的概念,没办法直接创建抽象类型,需要具体的类型(比如数组,链表)。
比如创建数组:int array[100];
创建一个GList链表: GList * list = NULL; (空的)
‘叁’ c语言中创建线性表问题
因为你的p没有初始化,printf 里面d与,后面的&。。不匹配
#include<stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配量
typedef int ElemType[LIST_INIT_SIZE];
typedef struct {
ElemType *elem;
int length;
int listsize;
}sqlist;
typedef int Status;
int InitList_Sq(SqList L)
{
//构造一个空的线性表L。
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem){return 0;}
L.length=0;
printf("%d\n",L.length);
L.listsize=LIST_INIT_SIZE;
return 1;
}
int main()
{
int t=0;
SqList p={};
InitList_Sq(p);
printf("%d\n",p.length);
return 0;
‘肆’ C语言创建一个线性表,然后输出线性表,如何编写程序
#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=0;i<L.length;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);
}
}
这个是我最近编写的 顺序表也是线性表的
这里还有链表的程序 用的话再传给你
‘伍’ 数据结构中,怎么用C语言构造线性表!
#define
OK
1
#define
ERROR
-1
#define
MAX_SIZE
100
typedef
int
Status
;
typedef
int
ElemType
;
typedef
struct
sqlist
{
ElemType
Elem_array[MAX_SIZE]
;
int
length
;
}
SqList
;
以上为线性表建立的相关定义
Status
Init_SqList(
SqList
*L
)
{
L->elem_array=(
ElemType
*
)malloc(MAX_SIZE*sizeof(
ElemType
)
)
;
if
(
!L
->
elem_array
)
return
ERROR
;
else
{
L->length=
0
;
return
OK
;
}
}
以上为线性表初始化函数
有以上两部分可构造一个线性表
‘陆’ 怎样创建一个线性链表(C语言)
可以用头插法或尾插法
(下面用尾插法)
思想为:让你输入一串字符串,
为每个字符创建一个节点,添加到链表的后面.直到输入的字符为@为止.
#include
<stdio.h>
#include
<malloc.h>
typedef
char
datatype;
typedef
struct
node
{
datatype
data;
struct
node
*next;
}linklist;
linklist
*p,*q,*head;
main()
{
char
c;
head
=
(linklist
*)malloc(sizeof(linklist));
head->next
=
NULL;
p
=
head;
c
=
getchar();
while(c
!=
'@')
{
q
=
(linklist
*)malloc(sizeof(linklist));
q->data
=
c;
q->next
=
NULL;
p->next
=
q;
p
=
p->next;
c
=
getchar();
}
}
可以在
main()
最后加上
for(p=head->next;
p!=NULL;
p=p->next)
{
printf("%5c",
p->data);
}
来测试结果,本人已经TC
2.0下面测试通过.
‘柒’ 数据结构 线性表 用c语言
#define MAXSIZE 100 //表中元素的最大个数
typedef int ElemType;//元素类型
typedef struct list{
ElemType elem[MAXSIZE];//静态线性表
int length; //表的实际长度
}SqList;//顺序表的类型名
‘捌’ 线性表的基本操作c语言实现
代码如下:
头文件:
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=0; index<SeqList_Length(list); index++)
{
int* p = (int*)SeqList_Get(list, index);
printf("%d ", *p);
}
printf(" ");
//删除操作
while( SeqList_Length(list) > 0 )
{
int* p = (int*)SeqList_Delete(list, 0);
printf("删除了: %d ", *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->len; i > pos; i--)
{
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+1; i<sList->len; i++)
{
sList->node[i-1] = sList->node[i];
}
sList->len--;
}
return node;
}
演示:
资料拓展:
线性表是最基本、最简单、也是最常用的一种数据结构。
线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储),但是把最后一个数据元素的尾指针指向了首位结点)。
我们说“线性”和“非线性”,只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表。
在数据结构逻辑层次上细分,线性表可分为一般线性表和受限线性表。一般线性表也就是我们通常所说的“线性表”,可以自由的删除或添加结点。受限线性表主要包括栈和队列,受限表示对结点的操作受限制。
线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。
‘玖’ 用C语言怎么建立线性链表
线性链表是一种数据结构。
使用指针在线性表的各个节点进行连接。
struct
NODE
{
int
value;
NODE*
next;
}
这种结构就是一种线性表的数据结构咯,你使用next指针指向下一个节点。