高分在线等~!数据结构:求把指定的链表转成顺序表(线性表)的C语言代码!!

2025-04-28 14:43:06
推荐回答(1个)
回答1:

//xieai999
#include

typedef struct stduy
{

char r[1000];

int len;
}Sqlist;//顺序表

typedef struct edu
{

char e;

struct edu *next;
}LT;//链表

void CreatLT(LT **s,char a[])//初始化链表
{

LT *p,*q;

(*s)=(LT *)malloc(sizeof(LT));

(*s)->e=a[0];

(*s)->next=NULL;

q=(*s);

int i=1;

while(a[i]!='\\0')

{

p=(LT *)malloc(sizeof(LT));

p->e=a[i];

p->next=NULL;

q->next=p;

q=q->next;

i++;

}
}

void linkTOlist(LT *s,Sqlist **r)//转换
{

(*r)=(Sqlist *)malloc(sizeof(Sqlist));

int i=0;

while(s!=NULL)

{

(*r)->r[i]=s->e;//链表转换成顺序表

s=s->next;

i++;

}
(*r)->len=i;
}

main()
{

char s[1000];

printf("请输入初始化的字符串\
");

scanf("%s",&s);

LT *A;Sqlist *B;

CreatLT(&A,s);

printf("转换进行中......\
");

linkTOlist(A,&B);

printf("转换后输出为\
");

int k=0;

for(k;klen;k++)

printf("%c ",B->r[k]);

return 0;
}

不知是否满意???