Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116311
  • 博文数量: 106
  • 博客积分: 2025
  • 博客等级: 大尉
  • 技术积分: 1165
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-06 12:51
文章分类

全部博文(106)

文章存档

2012年(106)

我的朋友

分类: C/C++

2012-05-07 17:17:44

10、链表

(1)链表的建立

①链表结点数未知

#include

#include

#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

#include

#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

#include

#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

#include

#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

#include

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

#include

#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

#include

#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->numnum)

{

if(head==p) head=pr;

else pt->next=pr;

pr->next=p;

}

else

{

p->next=pr;

pr->next=NULL;

}

return head;

}

(6)链表的合并

#include

#include

#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;

}

阅读(205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~