C language/c++ (data structure) C language key points review examples (1/7)

Experimental purpose and requirements:

Familiar with the method of using structure type to customize variables;

Use the sizeof keyword to calculate the width of various data types;

Proficiency in the use of dynamic memory allocation functions malloc and free, etc.;

Familiar with the basic method of using C language to operate singly linked list;

Experiment content:

Experiment 1 : Define variables using structure types

Purpose: Familiar with the method of using structure type custom variables

Content: Define structure s1, which can store a student's data, which consists of student number, name (the length of the name does not exceed 15 English characters), age, and grades of 5 subjects. It occupies a total of 64 bytes, among which the student number occupies 4 bytes, the name occupies 16 bytes, the age occupies 4 bytes, and the results of each subject occupy 8 bytes. class1 is a structure array, representing a class that can store 50 students. To complete the call (input, output) of each component in the structure.

Experiment 2 : Use the sizeof keyword to calculate the width of various data types

Purpose: Familiar with the use of sizeof

Content: Write a program to directly test the width of basic data types in C language such as plastic, character, etc. in the main function, and measure the number of bytes occupied by the structure type variables defined in Experiment 1.

Experimental question 3 : Calling of functions related to dynamic allocation of memory

Purpose: Proficiency in the use of dynamic memory allocation functions malloc and free

Content: For various data types, use malloc to dynamically allocate memory in the main function

Save, use free to free up space before the program ends.

Experimental question 4 : Construct a simple linked list using the structure type

Purpose: To preview the related structure of the linked list to be learned in Chapter 2, and to be familiar with the basic method of using C language to operate the single linked list

Content: After debugging the first chapter of exercise 9, the figure shows the linked list represented by the program, marking the content and address of the head variable, and the content of the expression head->next->next and the memory address it represents.

Experimental steps and procedures:

Experimental question 1:

#include<stdio.h> //Experiment 1 uses structure type to define variables

 struct student

{

 char name[10]; //name

 int id; //Student number

 int age; //age

 float yu_wen; //Chinese score

   float shu_xue; //math score

  float ying_yu;//English score

 float shu_xue_fen_xi; //mathematical analysis results

 float shu_ju_jie_gou ;//data structure results

};

student s1;

int main()

{

struct student s1[100];

 int i,n;

  printf("How many students do you want to enter?\n");

   scanf("%d",&n);

printf("Received!\n");

 for (i = 0; i < n; i++)

 {

  printf("Please enter the basic information of the student:\nName:");

  scanf("%s", &s1[i].name, 50);

  printf("Student number:");

  scanf("%d", &s1[i].id);

  printf("Age:");

  scanf("%d", &s1[i].age);

  printf("Chinese score:");

  scanf("%f", &s1[i]. yu_wen);

   printf("Math score:");

  scanf("%f", &s1[i].shu_xue);

   printf("English score:");

  scanf("%f", &s1[i].ying_yu);

   printf("数学分析成绩:");

  scanf("%f", &s1[i].shu_xue_fen_xi);

   printf("数据结构成绩:");

  scanf("%f", &s1[i].shu_ju_jie_gou);

    printf("该学生信息录入成功\n");

 }

 printf("信息如下:\n");

 for (i = 0; i < n; i++)

 {printf("学号:%d   姓名:%s   年龄:%d\n   语文成绩:%.2f   数学成绩:%.2f  英语成绩:%.2f 数学分析成绩:%.2f 成绩:%.2f   \n", s1[i].id, s1[i].name, s1[i].age, s1[i].yu_wen,s1[i].shu_xue,s1[i].ying_yu,s1[i].shu_xue_fen_xi,s1[i].shu_ju_jie_gou);}

}

实验题2:

#include<stdio.h> //实验一利用结构体类型定义变量

 struct student

{

 char name[10];         //名字

 int id;                  //学号

 int age;                 //年龄

 float yu_wen;    //语文成绩

   float shu_xue; //数学成绩

  float ying_yu;//英语成绩

 float shu_xue_fen_xi; //数学分析成绩

 float shu_ju_jie_gou ;//数据结构成绩

};

student s1;

int main()

{

struct student s1[100];

 int i,n;

  printf("你想录入几个学生的信息呢?\n");

   scanf("%d",&n);

printf("收到!\n");

 for (i = 0; i < n; i++)

 {

  printf("请输入学生基本信息:\n姓名:");

  scanf("%s", &s1[i].name, 50);

  printf("学号:");

  scanf("%d", &s1[i].id);

  printf("年龄:");

  scanf("%d", &s1[i].age);

  printf("语文成绩:");

  scanf("%f", &s1[i]. yu_wen);

   printf("数学成绩:");

  scanf("%f", &s1[i].shu_xue);

   printf("英语成绩:");

  scanf("%f", &s1[i].ying_yu);

   printf("数学分析成绩:");

  scanf("%f", &s1[i].shu_xue_fen_xi);

   printf("数据结构成绩:");

  scanf("%f", &s1[i].shu_ju_jie_gou);

    printf("该学生信息录入成功\n");

 }

 printf("信息如下:\n");

 for (i = 0; i < n; i++)

 {

  printf("学号:%d   姓名:%s   年龄:%d\n   语文成绩:%.2f   数学成绩:%.2f  英语成绩:%.2f 数学分析成绩:%.2f 成绩:%.2f   \n", s1[i].id, s1[i].name, s1[i].age, s1[i].yu_wen,s1[i].shu_xue,s1[i].ying_yu,s1[i].shu_xue_fen_xi,s1[i].shu_ju_jie_gou);

 }

 printf("student 结构体类型所占内存空间的大小:%d\n", sizeof(student));

}

实验题3:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct s1

{int buf[50];

 int size;

}s;

void func1(s* p)

{

 p = (s*)malloc(sizeof(s));

 p->size = 10;

 return;

}

s* func2(void)

{

 s* p;

 p = (s*)malloc(sizeof(s));

 p->size = 10;

 return p;

}

void func3(s** p)

{

 *p = (s*)malloc(sizeof(s));

 (*p)->size = 10;

 return;

}

int main(int argc, const char * argv[]) {

 s *p1 = NULL;

 s *p2 = NULL;

 s *p3 = NULL;

 func1(p1);

 p2 = func2();

 func3(&p3);

 if (p1 == NULL)

 { printf("p1=NULL\n");}

 else

 {printf("p1->size:%d\n", p1->size);}

 if (p2 == NULL)

 {printf("p2=NULL\n");}

 else

 {printf("p2->size:%d\n", p2->size);}

 if (p3 == NULL)

 {printf("p3=NULL\n");}

 else

 {printf("p3->size:%d\n", p3->size);}

 return 0;}

 void func_free(s* p)

{

 free(p);

 return;

}

实验4:

#include <iostream>

using namespace std;

struct Node{

 Node(int data) :data(data), next(NULL) {}

 int data;

 Node *next;

};

int main()

{

 Node *head = new Node(1);

 head->next = new Node(2);

 head->next->next = new Node(3);

 head->next->next->next = new Node(4);

 Node *p = head;

 while (p!=NULL) {

  cout << p->data << "->";

  p = p->next; 

 }

 cout<< endl;

 system("pause");

 return 0;

}

运行结果:

实验题1:

实验题2:

实验题3:

实验题4:

结果分析与讨论:

  1. C中定义一个结构体类型要用typedef,先声明结构体类型再定义变量名并且在声明类型的同时定义变量
  2. sizeof关键字计算各种数据类型宽度,比如我的这个程序语句是sizeof(student)。
  3. 熟练运用动态分配内存函数malloc和free等;malloc所分配的是一块连续的内存,以字节为单位,并且不带任何的类型信息。 free用于将动态内存撤销。
  4. 如果要查找某结点则需要对链表中的结点进行逐个遍历,如果要更改某结点的数据:则需要遍历找到该结点,然后直接更改数据的值。向链表中插入节点:将新结点的指针指向插入位置后的结点,将插入位置前的结点的next指针指向插入结点。而链表删除节点:将结点从链表中摘下来,可以利用free函数撤销。

Guess you like

Origin blog.csdn.net/qq_59819866/article/details/131450512