9. Users establish their own data model

Posted by gmcalp on Mon, 20 Dec 2021 03:47:37 +0100

Defining and using structure variables

C language allows users to establish a combined data structure composed of different types of data, which is called structure
Struct is the keyword that declares the structure type
Declare the general form of a struct type:
Struct structure name
{
Member table column
}
Declare each member: type name member name

Define structure type variables

1. Declare the structure type first, and then define the variable of this type
struct Student student1,student2
2. Define variables while declaring types
struct structure name
{
Member table column
}Variable name table column;
3. Directly define the structure type variable without specifying the type name
Struct
{
Member table column
}Variable name table column;

Initialization and reference of structure variables

When defining a structure variable, you can initialize it, that is, give it an initial value. Then reference this variable.

#include<stdio.h>
int main()
{
	struct Student
	{
		long int num;
		char name[20];
		char sex;
		char addr[20];
	}a={10101,"Li Lin",'M',"123 Beijing Road"};
	printf("NO:%d ,%s ,%c ,%s \n",a.num,a.name,a.sex,a.addr);
	return 0;
}

(1) When a structure variable is defined, its members can be initialized. The initialization list is some constants enclosed in curly braces, which are assigned to each member in the structure variable at one time
Struct Student b ={“.name = “zs”};// Member b.name in B
(2) You can reference the value of a member in a structure variable by
Structure variables member name
(3) If the member itself is a structure, you need to use several member operators to find the lowest level member level by level
(4) Members of structural variables can be operated like ordinary members
(5) Structural variables of the same kind can be assigned to each other
(6) You can reference the address of the structure variable member / structure variable

Example 9.2

#include<stdio.h>
int main()
{
	struct Student
	{
		int num;
		char name[20];
		float score;
	};
	struct Student student1 = {10101,"Wang",89
	};
	struct Student student2 = {10103,"Ling",90
	};
	if(student1.score>student2.score)
	{
		printf("%d %s %f\n",student1.num,student1.name,student1.score);
	 } 
	 else if(student1.score<student2.score)
	{
		printf("%d %s %f\n",student2.num,student2.name,student2.score);
	 }
	 else
	 {
		printf("%d %s %f\n",student1.num,student1.name,student1.score);
		printf("%d %s %f\n",student2.num,student2.name,student2.score);
	 } 
	  
	return 0;
}

Use structure array

Define structure array
Example 9.3

#include<stdio.h>
#include<string.h>
struct Person
{
	char name[20];
	int count;
}leader[3]={"Li",0,"Zhang",0,"Sun",0};

int main()
{
	int i,j;
	char leader_name[20];
	for(i=1;i<=10;i++)
	{
		scanf("%s",leader_name);
		for(j=0;j<3;j++)
		   if(strcmp(leader_name,leader[j].name) == 0)  leader[j].count ++; 
	}
	printf("\Result:\n");
	for(i=0;i<3;i++)
	   printf("%5s:%d\n",leader[i].name,leader[i].count);
	
	  
	return 0;
}

The general form of defining structure array is:

1.struct structure name
{member table column} array name [array length]
2. First declare a structure type (such as struct Person), and then define the structure array with the subtype
For example: struct Person leader[3];

The form of initializing the structure array is to add after the definition array
={initial value table column};

Example 9.4 (some problems)

#include<stdio.h>
#include<string.h>
struct Student
{
	int num;
	char name[20];
	float score;
};

int main()
{
	int i,j,k;
	struct Student stu[3] = {{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86}};
	struct Student temp;
	const int n=3;
	printf("the order is:\n");
	for(i=0;i<n;i++)
	{
	k=i;
		for(j=i;j<n;j++)
		{
			if(stu[j].score > stu[k].score)
			  
			  k=j;
		    temp=stu[i];
			stu[i] =stu[k];
			stu[k] =  temp; 
		}
	}
	for(i=0;i<n;i++)
	{
		printf("%6d%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score);
	}

	  
	return 0;
}

Structure pointer

The structure pointer is the pointer to the structure variable, and the starting address of a structure variable is the pointer to the structure variable
Pointer to structure variable
Strcut Student *pt;

Example 9.5

#include<stdio.h>
#include<string.h>
int main()
{
	struct Student
	{
		long num;
		char name[20];
		char sex;
		float score;
	};
	
	struct Student stu1;
	struct Student *p;
	p = &stu1;
	stu1.num = 10101;
	strcpy(stu1.name,"Li Lin");
	stu1.sex='M';
	stu1.score = 89.5;
	printf("NO:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score);
	printf("NO:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
	return 0;
 } 

If p points to a structure variable, the following three usages are equivalent
1.Stu.num
2.(*p).num
3. P - > num pointing operator

Pointer to structure array

Example 9.6

#include<stdio.h>
#include<string.h>
struct Student
{
	int num;
	char name[20];
	float score;
};
struct Student stu[3] = {{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86}};
int main()
{
	struct Student *p;
	for(p=stu;p<stu+3;p++)
	{
		printf("%d %s %.2f\n",p->num,p->name,p->score);
	}
	return 0;
}

Structure variables and pointers to structure variables are used as function parameters

There are three ways to pass the value of a structure variable to another function
(1) Use the members of structure variables as parameters
(2) Using structure variables as arguments
(3) Use the pointer to the structure variable (or array element) as the argument to pass the address of the structure variable (or array element) to the formal parameter
Example 9.7

#include<stdio.h>
#include<string.h>
struct Student
{
	int num;
	char name[20];
	float score[3];
	float aver;
};

int main()
{
	struct Student stu[3],*p=stu;

	void input(struct Student stu[]);
	input(p);
	struct Student max(struct Student stu[]);
	
    void print(struct Student stu);
	print(max(p));
	return 0;
}

void input(struct Student stu[])
{
	int i;
	printf("Please enter student information: student number, name, three course scores:\n");
	for(i=0;i<3;i++)
	{
		scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
	
	stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/3.0;
}
}

struct Student max(struct Student stu[])
{
	int i,m=0;
	for(i=0;i<3;i++)
	{
		if(stu[i].aver >stu[m].aver) m=i;
	}
	return stu[m];
}

void print(struct Student stu)
{
	printf("\n Highest score:\n");
	printf("%d %s %f %f %f %f\n",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.aver);
}

Handle linked list with pointer

Linked list is a data structure that dynamically allocates storage. Open up storage units as needed.
The linked list has a "header pointer" variable that stores an address that points to the next address. Each element in the linked list is called a node. Each node consists of two parts: 1 Actual data required by users 2 The address of the next node until the last element, "end of table", and the address is "NULL"
The elements in the linked list are discontinuous. If there is no header pointer, the whole linked list cannot be accessed

Establish a simple static linked list

Example 9.8

#include<stdio.h>
#include<string.h>
struct Student
{
	int num;
	//char name[20];
	float score;
	struct Student *next;
};

int main()
{
	struct Student a,b,c,*head,*p;
	a.num=10101;a.score=89.5;
	b.num=10103;b.score=90;
	c.num=10107;c.score=85;
	head=&a;
	a.next=&b;
	b.next=&c;
	c.next=NULL;
	p=head;
	do
	{
		printf("%1d %5.1f\n",p->num,p->score);
		p=p->next;
	}while(p!=NULL);
	return 0;
}

Establish dynamic linked list

Open up nodes, input the data of each node, and establish the relationship between the front and rear phase chains
Functions related to dynamic allocation storage allocation: malloc calloc realloc free

Example 9.9 (some problems)

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#define LEN sizeof(struct Student)
struct Student
{
	long num;
	//char name[20];
	float score;
	struct Student *next;
};

int n;

struct Student *creat(void)
{
	struct Student *head;
	struct Student *p1,*p2;
	n = 0;
	p1 = p2 = (struct Student *)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=(struct Student *)malloc(LEN);
		scanf("%ld , %f",& p1->num, &p1->score);
	 } 
	 p2->next = NULL;
	 return(head);
}

int main()
{
	struct Student *pt;
	pt = creat();
	printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);
	return 0;
}

Output linked list

Output the nodes in the linked list in turn
Example 9.10

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#define LEN sizeof(struct Student)
struct Student
{
	long num;
	//char name[20];
	float score;
	struct Student *next;
};

int n;

void print(struct Student *head)
{
	struct Student *p;
	printf("\nNow,These %d records are:\n",n);
	p=head;
	if(head!=NULL)
	{
		do
		{
			printf("%ld %5.1f\n",p->num,p->score);
			p=p->next;
		}while(p!=NULL);
	}
}

Combine the two functions of 9.7 and 9.9

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#define LEN sizeof(struct Student)
struct Student
{
	long num;
	//char name[20];
	float score;
	struct Student *next;
};

int n;
struct Student *creat(void)
{
	struct Student *head;
	struct Student *p1,*p2;
	n = 0;
	p1 = p2 = (struct Student *)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=(struct Student *)malloc(LEN);
		scanf("%ld , %f",& p1->num, &p1->score);
	 } 
	 p2->next = NULL;
	 return(head);
}

void print(struct Student *head)
{
	struct Student *p;
	printf("\nNow,These %d records are:\n",n);
	p=head;
	if(head!=NULL)
	{
		do
		{
			printf("%ld %5.1f\n",p->num,p->score);
			p=p->next;
		}while(p!=NULL);
	}
}

int main()
{
	struct Student *pt;
	pt = creat();
	print(pt);
	//printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);
	return 0;
}

Common body type

Use a memory unit to store different types of variables
Coverage technology, the latter covers the previous data. A structure that enables several different variables to share a piece of memory is called a common body type structure

General form:
Union common name
{
Member table column
}Variable list;

Common body and structure have similar definitions, but different meanings.

How to reference a common body variable
You cannot reference a common body variable. You can only reference members in a common body variable

Characteristics of common body type data

(1) The same memory segment can be used to store several different types of members, but only one member can be stored at a certain moment.
(2) Common body variables can be initialized, but there can only be one constant in the initialization table
(3) The last assigned member plays a role in the common body variable. After assigning a value to a member in the common body variable, the value in the original storage unit will be replaced
(4) The address of a common variable is the same as that of its members
(5) You cannot assign a value to a common variable name or attempt to reference a variable name to get a value
(6) You can use a pointer to a common body variable as a function parameter
(7) The common body type can appear in the structure type definition or define the common body array. On the contrary, the structure can also appear in the common body type definition, and the array can also be used as a member of the common body.

Example 9.11

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#define LEN sizeof(struct Student)
struct 
{
	int num;
	char name[10];
	char sex;
	char job;
	union
	{
		int clas;
		char position[10];
	}category;
}person[2];

int main()
{
	int i;
	for(i=0;i<2;i++)
	{
		printf("please enter the data of person:\n");
		scanf("%d %s %c %c",&person[i].num,&person[i].name,&person[i].sex,&person[i].job);
		if(person[i].job =='s')  scanf("%d",&person[i].category.clas);
		else if(person[i].job=='t')  scanf("%s",&person[i].category.position);
		else printf("input error!"); 
	}
	printf("\n");
	printf("NO.    name    sex    job   class/position\n");
	for(i=0;i<2;i++)
	{
		if(person[i].job=='s')
		   printf("%-6d%-10s%-4c%-4c%-10d\n",person[i].num,person[i].name,person[i].sex,&person[i].job,&person[i].category.clas);
		   else printf("%-6d%-10s%-4c%-4c%-10s\n",person[i].num,person[i].name,person[i].sex,&person[i].job,&person[i].category.position);
	}
	return 0;
}

Use enumeration type

List the possible values one by one, and the value of the variable is limited to the listed value range
Declare enumeration types starting with enum
General form of declaring enumeration types:
Enum [enum name] {enum element list}

Declare the new type name with typedef

1. Simply replace the original type name with a new type name
2. Command a simple type name instead of a complex type representation

Topics: data structure linked list pointer stack Singly Linked List