经典小程序

10、链表

(1)链表的建立

①链表结点数未知

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

STU *creat(void)

{

STU *head;

STU *p1,*p2;

n=0;

p1=p2=(STU *)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{

n=n+1;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=(STU *)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

}

p2->next=NULL;

return head;

}

②链表结点数已知

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

STU *creat(int n)

{

int i=1;

STU *head=NULL,*p1,*p2;

head=p1;

while(i<=n)

{

p1=(STU *)malloc(LEN);

if(i==1) {head=p1;p2=p1;}

scanf("%ld%f",&p1->num,&p1->score);

p2->next=p1;

p2=p1;

i++;

}

p2->next=NULL;

return head;

}

(2)链表的输出

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

void print(STU *head)

{

STU *p;

p=head;

if(head!=NULL)

do

{printf("%ld %5.1f\n",p->num,p->score);

p=p->next;

}while(p!=NULL);

}

(3)链表的查找

①无返回值

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

void search(STU student *head,long num)

{

STU *p;

p=head;

if(p!=NULL)

{

while(p->num!=num &&p->next!=NULL)

p=p->next;

if(p->num==num)

printf("the %ld score is%f",num,p->score);

else printf("%ld notfound\n",num);

}

else printf("the list isnull\n");

}

②有返回值

#include <stdio.h>

#include <stdlib.h>

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

int search(STU*head,long num)

{

STU *p;

p=head;

if(p!=NULL)

{

while(p->num!=num &&p->next!=NULL)

p=p->next;

if(p->num==num) return 1;

else return 0;

}

else return 0;

}

(4)链表的删除结点

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

STU *del(STU*head ,long num)

{

STU *pPre,*pCur;

if(head==NULL)

{

printf("null");

return head;

}

pCur=head;

while(num!=pCur->num &&pCur->next!=NULL)//如果当前不是要删的并且不是最后

一个结点

{

pPre=pCur;

pCur=pCur->next;

}

if(num==pCur->num)

{

if(pCur==head) head=pCur->next;

else pPre->next=pCur->next;

}

else printf("Not been found\n");

return head;

}

(5)链表的插入结点

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

STU *InsertNode(STU *head,STU *node)

{

STU *p,*pr,*pt;

p=head;

pr=node;

if(head == NULL)

{

head = pr;

pr->next = NULL;

}

else

{

while(pr->num>p->num &&p->next!=NULL)

{

pt=p;

p=p->next;

}

}

if(pr->num<p->num)

{

if(head==p) head=pr;

else pt->next=pr;

pr->next=p;

}

else

{

p->next=pr;

pr->next=NULL;

}

return head;

}

(6)链表的合并

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct Student)

typedef struct Student

{

long num;

float score;

struct Student *next;

}STU;

int n;

STU *hebing(STU *head1,STU *head2)

{

STU *p=head1;

while(p->next!=NULL)

p=p->next;

p->next=head2;

return head1;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值