C语言链表创建和输入
发布网友
发布时间:2022-04-26 07:15
我来回答
共2个回答
热心网友
时间:2023-10-30 22:19
敢问楼主,为何要两个链表?你的需求一个链表完全搞定了啊,第二个链表是用来做什么的?
#include<stdio.h>
#include<stdlib.h>
struct student{
char name[10];
char id[10];
struct student *next;
};
int main(void)
{
int n;
printf("请输入要学生个数,以回车结束!\n");
scanf("%d",&n);
struct student *head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
struct student *end=head;
for(int i=0;i!=n;i++)
{
struct student*p=(struct student *)malloc(sizeof(struct student));
p->next=NULL;
printf("第%d个学生信息:\n",i+1);
printf(" 姓名:");
scanf("%s",&p->name);
printf(" 学号:");
scanf("%s",&p->id);
end->next=p;
end=p;
}
for(struct student *p=head->next;p!=NULL;p=p->next)
{
printf("姓名:%s 学号:%s \n",p->name,p->id);
}
}
截图如下:
追问题目就是这样要求的,因为下一问是要让链表合并
追答
下面代码包含:两个链表输入、输出、合并(《知道》恶心的*字数,无法继续回答,只能附件了)
附件也上传不成功...
只能截图给你了:
热心网友
时间:2023-10-30 22:19
#include "link.h"
//实现类似于strlen
struct string_linkinfo BL_Stringlen(BLString *link)
{
struct string_linkinfo st_string = {0, 0};
char *p;
Block *pnode = NULL;
if(NULL == link){
printf("Invalid arg...\n");
return st_string;
}
//链表为空
if(NULL == link->head){
return st_string;
}
else{
pnode = link->head;
while(pnode != link->tail)
{
st_string.length += N;
st_string.count_node++;
pnode = pnode->next;
}//当while循环执行完后,pnode一定是最后一个节点
p = pnode->buffer;
st_string.count_node++;
while('\0' != *p)
{
st_string.length++;
p++;
}
}
return st_string;
}
//实现类似于strcmp
int BL_Stringcmp(BLString *link1, BLString *link2)
{
assert(NULL != link1 && NULL != link2);//断言
int i;
Block *pnode1 = link1->head;
Block *pnode2 = link2->head;
while(pnode1 != link1->tail && pnode2 != link2->tail)
{
for(i = 0; i < N; i++)
{
if((pnode1->buffer)[i] != (pnode2->buffer)[i])
return (pnode1->buffer)[i] - (pnode2->buffer)[i];
}
pnode1 = pnode1->next;
pnode2 = pnode2->next;
}
return strcmp(pnode1->buffer, pnode2->buffer);
}
//实现类似于strcpy
/*BLString *BL_Stringcpy(BLString *link1, BLString *link2)
{
}*/
//类似于strcat
BLString *BL_Stringcat(BLString *link1, BLString *link2)
{
//函数入口检测
if(NULL == link1 || NULL == link2)
{
printf("Invalid arg...\n");
return NULL;
}
if(NULL == link1->head){
link1->head = link2->head;
}
else//都不为空
{
int i, tmp = 0;
Block *pnode1 = link1->head;
Block *pnode2 = link2->head;
while(pnode1 != link1->tail)//将pnode1定位到末尾
pnode1 = pnode1->next;
//先看看缺多少个字符
tmp = strlen(pnode1->buffer);
//尾首链接
pnode1->next = pnode2;
while(pnode2 != link2->tail)
{
for(i = 0; i < N - tmp; i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];
for(i = 0 ; i < tmp; i++)
(pnode2->buffer)[i] = (pnode2->buffer)[N-tmp+i];
pnode1 = pnode1->next;
pnode2 = pnode2->next;
}
if(strlen(pnode2->buffer) < N-tmp)
{
for(i = 0; i < strlen(pnode2->buffer); i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];
(pnode1->buffer)[tmp+i] = '\0';
free(pnode2);
link1->tail = pnode1;
}
else{
for(i = 0; i < N - tmp; i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];
strcpy(pnode2->buffer, (pnode2->buffer)+(N-tmp));
link1->tail = pnode2;
}
link1->tail->next = NULL;
}
return link1;
}
void BL_Printstring(BLString *plink)
{
int i;
if(NULL == plink){
printf("Invalid arg...\n");
return ;
}
if(NULL == plink->head){
printf("The string is empty...\n");
return ;
}
Block *pnode = plink->head;
while(pnode != plink->tail)
{
for(i = 0; i < N; i++)
printf("%c", (pnode->buffer)[i]);
pnode = pnode->next;
}
printf("%s\n", pnode->buffer);
return;
}