11
This presentation is the property of its rightful owner.
Sponsored Links
1 / 49

第11章 结构体与共同体 PowerPoint PPT Presentation


  • 129 Views
  • Uploaded on
  • Presentation posted in: General

第11章 结构体与共同体. 11.1 概述 11.2 结构体变量的定义及使用 11.3 结构体数组 11.4 结构体指针 11.5 链表 11.6 共用体. 11.1 概述. 我们已讨论过的数据类型有简单型(整、实、字)、数组类型和指针类型。这些变量之间无固定内在的联系,分别表示独立数据,无从属关系。 数组虽能同时处理多个数据,但必须是同类型的。指针类型依赖于所指变量的类型。 这些数据类型难以处理 有从属关系 的 数据结构 。. 例如: 在计算机等级考试管理系统中,每个考生都 有如下信息 :.

Download Presentation

第11章 结构体与共同体

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


11

11

11.1

11.2

11.3

11.4

11.5

11.6


11

11.1


11

C

:


11

11.2

11.2.1


11

int x; float y; char a[30];


11

11.2.2

struct student

{ int num;

char name[20];

char class[10];

char sex;

float score;

}

struct student stu1, stu1;


11

structstudent

{ int num;

char name[20];

char class[10];

char sex;

float score;

}stu1,stu2;


11

birthday

stu

num

name

sex

age

score

month

year

day

struct student1

{ int num;

char name[20];

char class[10];

char sex;

structdate birth;

float score[4];

}stu;

structdate

{

int month;

int day;

int year;

};


11


11

11.2.3

.

. .


11

structstudent stu={ 2001802, , 011, f , 85.2 };

structstudent 1 stu={ 2001802, , 011, f ,

12, 3, 1986, 85.2 };


11

struct s

{ long num;

char name[20];

char sex;

}student={ 97031, "li ming" , 'm' } ;

struct stu

{ long num=97031;

char name[20] ="li ming";

char sex='m';

}student


11

1

scanf("%d", &stu );

scanf("%d", student.num );

scanf("%s", student.name );

student.sex=getchar( );

student.birth.day=12;


11

2

struct s

{ long int no;

float score;

}stu1,stu2={ 200123, 76.5 };

stu1=stu2;

3

4


11

11.3

:


11

p.266

1

stu[1]=97001;

stu[1].num=97001;


11

2

struct examinees s1,s[10];

s1=s[2]; s[1]=s[0];

1

30


11

11.4

struct student x,*p;

p=&x;

structstudent

{ int num;

char name[20];

char sex;

float score;

};

pnum


11

.

p

*p.

*p . * .

P.269 11.4


11

11.5

11.5.1


11


11

A

B

C


11

BACB


11


11

stu1

stu2

stu3

head

0001

0002

0003

&stu1

78

67

80

&stu2

&stu3

NULL


11

1

2

3

NULL


11

stu1

stu2

stu3

head

0001

0002

0003

&stu1

78

67

80

&stu2

&stu3

NULL


11

stu1

stu2

stu3

head

0001

0002

0003

&stu1

78

67

80

&stu2

&stu3

&stu1


11

11.5.2

1

void * malloc(size); void *calloc( n, size );

2

void free (void *p);

"malloc.h"


11

void * malloc(size);

size0


11

11.5.3

1 3

head ,

prear

pnew


11

2 3

head = NULL ;

pnew = (struct stu *)malloc(LEN) ;

head = pnew ;

prear = pnew ;

3:

1) malloc

2

3


11

5()

#define LEN sizeof( struct student )

#define STU struct student

struct student

{ long num;

float score;

struct student next;

};

head, pnew, prear.


11

STU creat( )

{ //

STU head, p1, p2 int n=0;

head=NULL;

p1= (STU ) malloc(LEN);

scanf("%d, %f ", &p1num, &p1score);


11

while ( p1num != 0 )

{ n+ +;

if( n= =1) head=p1 ;

else p2next = p1 ;

p2=p1; p2

p1 = (STU ) malloc(LEN);

scanf("%ld, %f", &p1num, &p1score);

}

p2next = NULL;

return(head);

}


11

void print ( STU head )

{ STU p ;

p = head ;

if (p = = NULL) return ;

do

{ printf("%ld %5.1f \n", pnum, pscore) ;

p = pnext ;

}while (p != NULL) ;

}


11

stu1

stu2

stu3

head

0001

0002

0003

&stu1

78

67

80

&stu2

&stu3

NULL

p2

p1

2 p2.next = p1.next

1 p.next = p.next.next


11

1

2

3

STU del( STU head, long num )

{ STU p1, p2 ;

if ( head= =NULL )

{ printf("\n list null!=\n"); return (head); }


11

p1 = head;

while (num!=p1num && p1next!=NULL)

{ p2=p1; p1=p1next; }

if (num= =p1num)

{ if (p1= =head) head=p1next;

else p2next=p1next;

printf ("delete: %ld \n", num); n ;

}

else printf ("%ld not been found!\n", num);

}


11

003

004

001

\0

002

head

:

new.next=p.next;

p.next=new;


11

13new()p1()p2p1

2

3

1

2


11

STU insert( STU *head, STU *new )

{ STU *p1,*p2;

p1=head;

if( head==NULL)

{ head=new; newnext=NULL; }

else

while(( newnum > p1num )&&( p1next != NULL ))

{ p2=p1; p1=p1next; }


11

if( newnum <=p1num )

{ if (head == p1) head=new;

else p2next=new;

newnext=p1;

}

else

{ p1next=new ; newnext=NULL; }

n++;

return( head );

} 11-7-1.c 11-7-11.c


11

11.6

structunion

=


11

1001

v

1005

n

c

1007

2001

c

n

2002

v

2003

2004

struct memb

{ float v;

stag7

int n;

char c;

} stag;

union memb

{ float v;

utag

int n;

char c;

} ustag;


11

().

1

2

union

{ int i;

char ch;

}x={6, A} ;


11

36.7

13107

11-6-1:

#include<stdio.h>

union memb

{ float v;

int n;

char c;

} ;

main( )

{ union memb utag;

utag.c='T '

utag.n=18

utag.v=36.7

printf("%5.1f\n%d\n%c\n", utag.v, utag.n, utag.c);

}


11

utag.v=36.7

utag.n=18

utag.c='T '

36.5

84

T


11

11-6-2:(11-6-2.c )

3256025ABC DC


  • Login