1 / 23

结构体和链表

结构体和链表. 内容要点. 一、 C 语言中,数组是具有相同数据类型的数据集合,结构体是 不相同 数据类型的数据集合。 二、结构体说明 struct 结构体名 { 数据类型 成员名1; 数据类型 成员名2; … 数据类型 成员名 n; };. 三、结构体变量的定义 1、定义好结构体类型後,再定义结构体变量。此时只需写 struct 结构体名 结构体变量名; 2、定义结构体类型的同时,定义结构体变量。 struct 结构体名{

iniko
Télécharger la présentation

结构体和链表

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 结构体和链表

  2. 内容要点 一、C语言中,数组是具有相同数据类型的数据集合,结构体是不相同数据类型的数据集合。 二、结构体说明 struct 结构体名 { 数据类型 成员名1; 数据类型 成员名2; … 数据类型 成员名n; };

  3. 三、结构体变量的定义 1、定义好结构体类型後,再定义结构体变量。此时只需写 struct 结构体名 结构体变量名; 2、定义结构体类型的同时,定义结构体变量。 struct 结构体名{ 结构体成员表; } 结构体变量名表; 3、直接定义结构体变量,省去结构体名的说明。 struct { 结构体成员表;} 结构体变量名表;

  4. 4、结构体变量的引用 结构体变量名 . 成员名 指针变量名 ->成员名 (*指针变量名) . 成员名 5、结构体变量的初始化 struct 结构体名 变量名 = { 初始数据}; 四、结构体数组 1、结构体数组的定义 struct 结构体名 结构体数组名[体积]; 2、引用和初始化。

  5. 五、结构体指针 1、结构体变量指针的定义 struct 结构体名 *结构体指针名; 2、结构体指针(指向数组的元) struct 结构体名 结构体数组,指针; struct student stud[40], *p=&stud; 六、链表 1、结构体的成员中有指向本类结构体变量的指 针next。 2、链表的建立 (1) 用函数malloc(sizeof(struct 结构体名))向系统申请一段内存,并获得这段内存的首地址。

  6. (2) 向这段内存的数据域送一个结点的数据。 (3) 将这段内存的首地址赋给现有链表的某个指 针,将现有链表的某个地址赋给本结点的 next 指针。这段内存就作为一个结点链入原 链表。 (4) 新结点接入链表的方式有三: 新结点结在链表的头上; 新结点结在链表的尾上; 新结点按某种规则插入链表的恰当位置。

  7. 例:1(02秋) 编函数 create, 建立一个单链表,它有10个结点,num 值依次取1到10;编函数 list 输出该链表内容。 #include <stdio.h> struct node { int num; struct node *next; }; struct node *create(void) { struct node *p, *q; int n; q=NULL; for(n=1; n<=10; n++) { p=(struct node*) ; /*新结点接在链表头里*/ malloc(sizeof(struct node))

  8. p->next=q p->num=n; ; q=p; } return p; } void list(struct node *head) { struct node *p; for(p=head; ; p=p->next ) printf("%d", p->num); } p

  9. 例:2(02冬) 编函数,在链表的尾部插入一个节点,并返回 该链表的头节点指针。参数 head 为原链表头节点 指针,若原链表为空,则 head为 NULL,参数 num 与 pay为要插入职工的工号与工资。 #include <stdio.h> struct worker { int num; float pay; struct worker *next; };

  10. struct worker *insert(struct worker *head, int num, int pay) { struct worker *p, *q; for(p=head; p!=NULL&&p->next!=NULL; p=p->next); q=(struct worker*) ; q->num=num; q->pay=pay; q->next= ; if( ) { p->next=q; return head; } else return q; } malloc(sizeof(struct worker)) NULL head!=NULL或 p!=NULL

  11. 打印链表或销去链表都是在链表存在的前提下 进行,用循环语句实现。如打印, for(p=head; p; p=p->next) printf("%d, %s, %d.\n", p->num, p->name, p->score); 销去链表用函数 free( p),p是指向结点的指针。 void del_list( NODE*head ) { NODE*p, *u; p=head; while( p) { u=p->next; free( p); p=u;} }

  12. 上机练习 1、(01秋) 为了判断一个式子中的括号(小括号、中括号和大括号)是否配对,已经建立一个单向链表,结点定义如下: struct gh /* 括号结构体 */ { char gch; /* 括号字符 */ int pos; /* 括号所在位置 */ struct gh *next; /* 下一结点指针 */ };

  13. 试编写一个函数,int isghok( struct gh *head ); 统计链表中各种括号的个数,若括号个数匹配,则返回1,否则返回0。如上例中,‘(’个数为1,‘)’个数为2,不相等,故返回0。 注意:若式子为 {x=(5+6)/4*a[n};],由于各种括号均为一个,故也将返回1。

  14. 上机练习 2、(01冬) 以下函数返回链表中工资最高的职工的结点指 针,设工资不可能为负数。请完成该函数。 struct worker { int num; char name[20]; float pay; struct worker *next; };

  15. struct worker *maxpay( struct worker *head ) { struct worker *p, *q=NULL; float max; max = 0; for ( p=head; p!=NULL; ③ ) if ( ④ ) { max = p->pay; q = p; } return ⑤ ; } p=p->next p->pay>max head

  16. 上机练习 3、(01春) 阅读以下程序写出其输出。 #include <stdio.h> # include <malloc.h> struct node { int n; struct node *next; }; void insert(struct node *head, int newn) { struct node *p, *q ;

  17. for(p=head; p->next!=NULL; p=p->next ) { q=p->next; if((newn%10)<(q->n%10)) break; } q=(struct node*)malloc(sizeof(struct node)); q->n=newn; q->next=p->next; p->next=q; }

  18. main() { struct node head, *p,, *q; head.next=NULL; insert(&head, 56); insert(&head, 38); insert(&head, 72); insert(&head, 49); for(p=head.next; p!=NULL; p=p->next) printf(“%d ”, p->n); printf(“\n”);

  19. for(p=head.next; p!=NULL;) { q=p; p=p->next; free(q); } } 输出?

  20. 上机练习 4. 建立一个单链表,它有9个结点,data值依次取1, 3, 5, 6, 8, 9, 10, 11, 15;请插入一个结点,它的data值为 4, 插入后仍保持链表各结点data值的升序。再按上述要求插入一个结点,其data值为-3, 检验你程序的正确性。

More Related