数据结构c语言顺序表(数据结构中,顺序表和C语言数组的区别是什么)
本文目录
- 数据结构中,顺序表和C语言数组的区别是什么
- 数据结构实验(C语言): 顺序表实验
- 数据结构:顺序表的合并(C语言)
- C语言(数据结构)顺序表的初始化需要申请结点空间吗
- C语言(数据结构)顺序表的初始化
- 数据结构C语言版,顺序线性表的合并程序最好有注释
- (C语言数据结构) 设计两个有序顺序表的合并排序算法
数据结构中,顺序表和C语言数组的区别是什么
他们答得我个人觉得是不正确的,因为刚好在学,顺序表是指在计算机内存中连续的线性表,既有逻辑结构,也有存储结构;而数组属于有序表,单指逻辑结构。也就是说数组的实现最终是顺序表。
数据结构实验(C语言): 顺序表实验
//线性表函数操作
#include 《*****》
#include 《*****》
#define MaxSize 30
#define Error 0
#define True 1
typedef char ElemType;
typedef struct
{
ElemType elem;
int length;
}SqList; /*顺序表类型定义*/
void InitList(SqList * &L) /*初始化顺序表L*/
{
L = (SqList *)malloc(sizeof(SqList));
L -》 length = 0;
}
void DestroyList( SqList *L ) /*释放顺序表L*/
{
****(L);
}
int ListEmpty( SqList *L ) /*判断顺序表L是否为空表*/
{
return( L -》 length == 0);
}
int ListLength( SqList *L ) /*返回顺序表L的元素个数*/
{
return( L -》 length);
}
void DispList( SqList *L ) /*输出顺序表L*/
{
int i;
if( ListEmpty(L))
return;
for( i = 0; i 《 L -》 length; i++ )
printf("%c", L -》 elem);
printf("\n");
}
int GetElem( SqList *L, int i, ElemType &e) /*获取顺序表中的第i个元素*/
{
if( i 《 1 || i 》 L -》 elem)
return Error;
e = L -》 elem;
return True;
}
int LocateElem( SqList *L, ElemType e) /*在顺序表中查找元素e*/
{
int i = 0;
while( i 《 L -》 length && L -》 elem != e)
i++;
if(i 》= L -》 length)
return Error;
else
return i+1;
}
int ListInsert( SqList * &L, int i, ElemType e) /*在顺序表L中第i个位置插入元素e*/
{
int j;
if( i 《 1 || i 》 L -》 length + 1)
return 0;
i--; /*将顺序表位序转化为elem下标*/
for( j = L -》 length; j 》 i; j--) /*将elem及后面元素后移一个位置*/
L -》 elem;
L -》 elem = e;
L -》 length++; /*顺序表长度增1*/
return True;
}
int ListDelete( SqList * &L, int i, ElemType &e) /*顺序表L中删除第i个元素*/
{
int j;
if( i 《 1 || i 》 L -》 length)
return Error;
i--; /*将顺序表位序转化为elem下标*/
e = L -》 elem;
for(j = i; j 《 L -》 length - i; j++)
L -》 elem;
L -》 length--; /*顺序表长度减1*/
return True;
}
void main()
{
SqList *L;
ElemType e;
printf("(1)初始化顺序表L\n");
InitList(L);
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(L, 1, ’a’);
ListInsert(L, 2, ’b’);
ListInsert(L, 3, ’c’);
ListInsert(L, 4, ’d’);
ListInsert(L, 5, ’e’);
printf("(3)输出顺序表L:");
DispList(L);
printf("(4)顺序表L长度 = %d\n", ListLength(L));
printf("(5)顺序表L为%s\n", (ListEmpty(L) ?"空" :"非空"));
GetElem(L, 3, e);
printf("(6)顺序表L的第3个元素 = %c\n", e);
printf("(7)元素a的位置 = %d\n", LocateElem(L,’a’));
printf("(8)在第4个元素位置上插入f元素\n");
ListInsert(L, 4, ’f’);
printf("(9)输出新的顺序表L:");
DispList(L);
printf("(10)删除L的第3个元素\n");
ListDelete(L, 3, e);
printf("(11)输出新的顺序表L:");
DispList(L);
printf("(12)释放顺序表L\n");
DestroyList(L);
}
数据结构:顺序表的合并(C语言)
#include 《*****》
#include 《*****》
#include 《*****》
#define LIST_INIT_SIZE 10 // 线性表存储空间的初始分配量
#define LISTINCREMENT 2 // 线性表存储空间的分配增量
struct SqList
{
int *elem; // 存储空间基址
int length; // 当前长度
int listsize; // 当前分配的存储容量(以sizeof(int)为单位)
};
void InitList(SqList &L)
{ // 操作结果:构造一个空的顺序线性表
***** = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if (!*****)
exit(0); // 存储分配失败
***** = 0; // 空表长度为0
***** = LIST_INIT_SIZE; // 初始存储容量
}
int ListInsert(SqList &L, int i, int e)
{ // 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)+1
// 操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
int *newbase, *q, *p;
if (i《1 || i》***** + 1) // i值不合法
return 0;
if (***** 》= *****) // 当前存储空间已满,增加分配
{
if (!(newbase = (int *)realloc(*****, (***** + LISTINCREMENT)*sizeof(int))))
exit(0); // 存储分配失败
***** = newbase; // 新基址
***** += LISTINCREMENT; // 增加存储容量
}
q = ***** + i - 1; // q为插入位置
for (p = ***** + ***** - 1; p 》= q; --p) // 插入位置及之后的元素右移
*(p + 1) = *p;
*q = e; // 插入e
++*****; // 表长增1
return 1;
}
void Print(SqList &L)
{
int i;
for (i = 0; i《*****; i++)
printf("%d ", *(***** + i));
printf("\n");
}
// ————————————————————————————————————
// 函数①
void MergeList1(SqList La, SqList Lb, SqList &Lc)
{
int *pa, *pa_last, *pb, *pb_last, *pc;
pa = *****;
pb = *****;
***** = ***** = ***** + *****;//不用InitList()创建空表Lc
pc = ***** = (int *)malloc(******sizeof(int));
if (!*****) // 存储分配失败
exit(0);
pa_last = ***** + ***** - 1;
pb_last = ***** + ***** - 1;
while (pa 《= pa_last&&pb 《= pb_last) // 表La和表Lb均非空
{ // 归并
if (*pa 《 *pb)
*pc++ = *pa++;
else if (*pa 》 *pb)
*pc++ = *pb++;
else{
*pc++ = *pa++;
pb++;
Lc.length--;
}
}
while (pa 《= pa_last) // 表La非空且表Lb空
*pc++ = *pa++; // 插入La的剩余元素
while (pb 《= pb_last) // 表Lb非空且表La空
*pc++ = *pb++; // 插入Lb的剩余元素
}
// ————————————————————————————————————
// 函数②
void MergeList2(SqList La, SqList &Lb, SqList &Lc)
{
int *pa, *pa_last, *pb, *pb_last, *pc;
pa = *****;
pb = *****;
***** = ***** = ***** + *****;//不用InitList()创建空表Lc
pc = ***** = (int *)malloc(******sizeof(int));
if (!*****) // 存储分配失败
exit(0);
pa_last = ***** + ***** - 1;
pb_last = ***** + ***** - 1;
while (pa 《= pa_last&&pb 《= pb_last) // 表La和表Lb均非空
{ // 归并
if (*pa 《= *pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while (pa 《= pa_last) // 表La非空且表Lb空
*pc++ = *pa++; // 插入La的剩余元素
while (pb 《= pb_last) // 表Lb非空且表La空
*pc++ = *pb++; // 插入Lb的剩余元素
***** = *****;
Lb = Lc;
}
// ————————————————————————————————————
// 函数③
void Inverse(SqList &L){
int i, t;
for (i = 0; i 《 ***** / 2; i++)
{
t = *(***** + i);
*(***** + i) = *(***** + ***** - i - 1);
*(***** + ***** - i - 1) = t;
}
}
void main(){
SqList LA, LB, LC;
int a = { 2, 6, 8, 9, 11, 15, 20 };
int i;
InitList(LA);
InitList(LB);
InitList(LC);
for (i = 0; i 《 4; i++)
ListInsert(LA, i + 1, a);
for (i = 0; i 《 7; i++)
ListInsert(LB, i + 1, b);
printf("LA=");
Print(LA);
printf("LB=");
Print(LB);
printf("\n");
MergeList1(LA, LB, LC);
printf("①LC=");
Print(LC);
printf("\n");
MergeList2(LA, LB, LC);
printf("②LB=");
Print(LB);
printf("\n");
printf("③LC=");
Inverse(LC);
Print(LC);
}
C语言(数据结构)顺序表的初始化需要申请结点空间吗
C语言(数据结构)顺序表的初始化需要申请结点空间。
初始化顺序表
1、/**
2、
*
初始化顺序表
3、
*
返回1
表示初始化成功
4、*
返回0
表示初始化失败
5、*/
6、int
initList_sq(SqList
&L)
{
//只有在C++中才会有引用的存在
7、
*****
=
(SeqType
*)
malloc(sizeof(SeqType)
*
LIST_INIT_SIZE);
8、
if
(!*****)
9、
return
0;
//内存分配失败,存储空间不够
10、
*****
=
0;
//表示顺序表为空
11、
*****
=
LIST_INIT_SIZE;
//表示顺序表里,最大存储单元个数
分配顺序表的存储单元,初始化顺序表属性的值。
定义结构
typedef
int
SeqType;
//存储单元类型
typedef
struct{
SeqType
*elem;
//存储空间基地址
int
length;
//当前长度
int
listsize;
//当前分配的存储容量(以sizeof(ElemType)为单位)
}
SqList;
结构体内,有三个元素:存储空间基地址,类似于数组首地址;当前长度,记录顺序表中有效存储单元个数;当前分配的存储容量,顺序表中,最多容纳的存储单元个数。当顺序表中所有存储单元已经被使用,在下次插入元素之前,需要新增存储单元。这点是数组所不具有的特性。
*注:定义一个存储单元类型SeqType是为了使顺序表适和更多数据类型,使用的时候修改SeqType类型即可。
C语言(数据结构)顺序表的初始化
肯定是要分配空间的,你第一个程序的initsqlist函数中声明的结构体指针(Sqlist *a;),而第二个程序的initsqlist函数中是声明的结构体(Sqlist a;),前者只是在计算机内存中声明一个指针,而不会给这个指针分配内存空间,所以你初始化的时候要给这个指针用malloc函数分配空间,而后者是实实在在的定义了一个结构体,在内存在不仅仅是一个地址,还分配了空间,就像你定义了一个数组一样,已经在内存存在空间了,不需要再分配了。
总体来说就是你定义的是指针的话就要分配空间。
你的2个程序都是对的,但是一般用第一种定义链表!!!
数据结构C语言版,顺序线性表的合并程序最好有注释
//希望我的回答对你的学习有帮助
#include 《*****》
/*顺序表存储空间长度的最小值*/
#define LISTMINSIZE 10
/*顺序表存储结构类型定义*/
typedef struct
{
ListDT*base; /*顺序表空间基地址*/
intlistsize; /*顺序表空间尺寸*/
intlen; /*顺序表长度*/
}SeqList;
/*顺序表初始化*/
void ListInitialize(SeqList *pL, int size)
{
if(size《LISTMINSIZE)
size=LISTMINSIZE; /*限定不能小于最小尺寸*/
pL-》listsize=size;
pL-》base=(ListDT*)malloc(pL-》listsize*sizeof(ListDT));
if(!pL-》base)
exit(EXIT_FAILURE);
pL-》len=0; /*初始化空表*/
}
/*按给定的下标取顺序表元素值*/
BOOL ListElem(SeqList L, int index, ListDT *pelem)
{
BOOL***=TRUE;
if(index《0|| index》L.len-1 )
***=FALSE; /*参数越界*/
else
*pelem=*****;
return***;
}
/*求顺序表长度*/
int ListLen(SeqList L)
{
*****;
}
/*在顺序表中指定序号位置插入元素*/
BOOL ListInsert(SeqList *pL, int pos, ListDT d)
{
BOOL***=TRUE;
inti;
if(pos《0|| pL-》len》=pL-》listsize || pos》pL-》len)
***=FALSE;
else
{
for(i=pL-》len-1;i》=pos; i--) /*移动数据*/
pL-》base;
pL-》base=d; /*写入数据*/
pL-》len++; /*表长增1*/
}
return***;
}
/*把顺序表中指定序号的元素删除*/
BOOL ListDel(SeqList *pL, int pos)
{
BOOL***=TRUE;
inti;
if(pos《0|| pos》=pL-》len)
***=FALSE;
else
{
for(i=pos+1;i《pL-》len; i++) /*移动数据*/
pL-》base;
pL-》len--; /*表长增1*/
}
return***;
}
/*在顺序表中查找元素*/
int ListLoc(SeqList L, ListDT d,BOOL (*equal)(ListDT,ListDT))
{
intpos=L.len-1;
while(pos》=0&& !(*equal)(*****,d))
pos--;
returnpos;
}
/*取前导元素序号位置*/
BOOL ListPrior(SeqList L, int pos, int *ppriorpos)
{
BOOL***=TRUE;
if(pos》0&& pos《*****)
*ppriorpos=pos-1;
else
***=FALSE;
return***;
}
/*取后继元素序号位置*/
BOOL ListNext(SeqList L, int pos, int *pnextpos)
{
BOOL***=TRUE;
if(pos》=0&& pos《L.len-1)
*pnextpos=pos+1;
else
***=FALSE;
return***;
}
/*销毁顺序表*/
void ListDestroy(SeqList L)
{
****(*****);
}
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
建议性测试用程序
*/
typedef enum {TRUE=1,FALSE=0} BOOL;
typedef int ListDT;
#include "*****"
void printSeqList(SeqList L)
{
inti;
ListDTx;
printf("\nList:\n");
for(i=0;i《ListLen(L); i++)
{
ListElem(L,i,&x);
printf("%3d",x);
}
}
BOOL dataequal(int x, int y)
{
return(x==y)? TRUE:FALSE;
}
#define N 5
void main()
{
inti,prior,next;
ListDTx,test={10,20,30,40,50};
SeqListL;
/*初始化顺序表*/
ListInitialize(&L,N);
/*在表头插入N个元素*/
for(i=0;i《N; i++)
ListInsert(&L,0,test);
printSeqList(L);
/*删除元素*/
ListDel(&L,N/2);
printSeqList(L);
printf("\ninputa key:\n");
scanf("%d",&x);
/*查找x在表中位置*/
i=ListLoc(L,x,dataequal);
/*求x的前导元素*/
if(ListPrior(L,i,&prior))
{
ListElem(L,prior,&x);
printf("Prior:%d\n",x);
}
else
printf("noPrior.\n");
/*求x的后继*/
if(ListNext(L,i,&next))
{
ListElem(L,next,&x);
printf("Next:%d\n",x);
}
else
printf("noNext.\n");
/*求表长*/
printf("Listlength=%d",ListLen(L));
/*销毁顺序表*/
ListDestroy(L);
}
(C语言数据结构) 设计两个有序顺序表的合并排序算法
#include
#include
typedef
struct
node//定义结构体
{
int
score;
struct
node
*next;
}node;
node
*create(int
n)//创建链表
{
node
*head,*tail,*p;
head=tail=null;
int
i;
for(i=1;i《=n;i++)
{
p=(node
*)malloc(sizeof(node));
p-》next=null;
printf("please
input
%d
score:",i);
scanf("%d",&p-》score);
if(head==null)
head=tail=p;
else
{
tail-》next=p;
tail=p;
}
}
return
head;
}
node
*range(node
*head)//排序
{
node
*p,*q;
int
score,i,n=0;
p=head;
while(p)
{
n++;
p=p-》next;
}
for
(i=0;i
next!=null)//内循环
{
q=p-》next;
if
(p-》score》q-》score)//值交换
{
score=p-》score;
p-》score=q-》score;
q-》score=score;
}
p=q;
}
}
return
head;
}
node
*connect(node
*head1,node
*head2)//合并
{
node
*p;
p=head1;
while(p-》next!=null)
p=p-》next;
p-》next=head2;
return
head1;
}
void
output(node
*head)//输出
{
node
*p;
p=head;
while(p)
{
printf("%d
",p-》score);
p=p-》next;
}
printf("\n");
}
int
main()
{
node
*head,*hea1,*head2;
int
n1,n2;
printf("please
input
a
n1:");//第一个链表的成绩个数
scanf("%d",&n1);
hea1=create(n1);
printf("第一个链表的成绩:");
output(hea1);
printf("please
input
a
n2:");//第二个链表的成绩个数
scanf("%d",&n2);
head2=create(n2);
printf("第二个链表的成绩:");
output(head2);
head=connect(hea1,head2);
head=range(head);
printf("合并后,并排好序:");
output(head);
return
0;
}
更多文章:
html中的iframe(html标签 iframe 问题)
2026年5月3日 10:00
cocos2dx 教程(急问,安卓2d游戏开发用哪个引擎,本人想学AndEngine,可是没教程)
2026年5月3日 09:40
fortran goto语句(关于fortran goto 语句)
2026年5月3日 09:00
数据结构c语言顺序表(数据结构中,顺序表和C语言数组的区别是什么)
2026年5月3日 08:20
C++中,如何实例化一个类实例.?什么样的抽象类不能产生类的实例(怎么定义抽象类)
2026年5月3日 07:20
connection with(have a connection with和have to do with的区别)
2026年5月3日 07:00



