用c语言连接两个链表,怎么将两个链表用C语言链接起来

2016-11-25 回答

#include

#include

#include

#include

#include

typedef struct node

{int score;

char name[10];

struct node *next;

}stu,*pstu;

#define size sizeof(stu)//这里要放到结构体之后,因为stu定义结构体后才//声明的

pstu head,last;

pstu creatlinks(int);

void deletenode(pstu,int);

void freelink(pstu);

void insertnode(pstu,int);

pstu findnode(pstu,int);

void printflink(pstu);

void sortlink(pstu);

int main()

{

head=creatlinks(7);

last = head;

last=last->next;

printf("%d\n",last->score);

last=last->next;

printf("%d\n",last->score);

last=last->next;

printf("%d\n",last->score);

last=last->next;

printf("%d\n",last->score);

last=last->next;

printf("%d\n",last->score);

printflink(head);

deletenode(head,4) ;

// sortlink(head);

//这个函数在下面定义的时候看不明白

//你要做什么,所以没 改,我把它注释出来,自己根据功能改吧

printflink(head);

insertnode(head,6);

printflink(head);

getch();

return 0;

}

pstu creatlinks(int n)//这里创建链表我重新写过了

{

pstu head  = null, first=null,ptr=null;

head = first = (pstu)malloc(size);

printf("input the number 1 information: ") ;

scanf("%s%d",&first->name,&first->score);

for ( int i = 1; i < n; i++)

{

ptr = (pstu)malloc(size);

printf("input the number %d information: ", i+1) ;

scanf("%s%d",&ptr->name,&ptr->score);

first->next = ptr;

first = ptr;

ptr = null;

}

first->next = null;

return head;

}

void deletenode(pstu f,int i)

{pstu first=null,now=null;

now=f;

while(--i)

{first=now;now=now->next;}

first->next=now->next;

free(now);

}

void freelink(pstu e)

{ pstu first=null,last=null;

first=e;

while(first!=null)///first不能是0改为null

{last=first->next; free(first);first=last;}

}

void insertnode(pstu d,int j)

{pstu now=null,pp=null, p;

now=d;

int i = 0;

printf("input the information:") ;

pp = (pstu)malloc(size);//你要插入的结点,要先分配内存给它

pp->next = null;

scanf("%s%d",pp->name,&pp->score);

while(now != null && i != j)

{p = now;

now=now->next;

}

p->next=pp;

pp->next=now;

}

pstu findnode(pstu c,int k)

{pstu now=null;

now=c;

while(--k)

{now=now->next;}

return now;

}

void printflink(pstu b)

{

while(b != null)//这里你要输出链表里的东西,用一个循环就ok

{

printf("%d\n",b->score);

b=b->next;

}

}

/*void sortlink(pstu a)

{pstu p=null,o=null;

int t;

char *tt;

for(p=a->next;p->next;p=p->next)

for(o=p->next;o;o=o->next)

if(p->scorescore||(p->score==o->score&&strcmp(o->name,p->name)))

{t=p->score;p->score=o->score;o->score=t;

tt=p->name;p->name=o->name;o->name=tt;

}

} */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值