C Language Learning course seventh chapter-Structure and Union (7)

Source: Internet
Author: User
Tags define null integer printf

[Example 7.14] set up the list above, delete the node, insert the function of the node together, build a function to output all the nodes, and then call them with the main function.
#define NULL 0
#define TYPE struct STU
#define LEN sizeof (struct stu)
struct STU
{
int num;
int age;
struct Stu *next;
};
TYPE * creat (int n)
{
struct Stu *HEAD,*PF,*PB;
int i;
for (i=0;i<n;i++)
{
pb= (TYPE *) malloc (LEN);
printf ("Input number and age\n");
scanf ("%d%d", &pb->num,&pb->age);
if (i==0)
PF=HEAD=PB;
else pf->next=pb;
pb->next=null;
PF=PB;
}
return (head);
}
Type * Delete (type * head,int num)
{
TYPE *PF,*PB;
if (head==null)
{printf ("\nempty list!\n");
Goto END;}
Pb=head;
while (Pb->num!=num && pb->next!=null)
{Pf=pb;pb=pb->next;}
if (pb->num==num)
{if (pb==head) head=pb->next;
else pf->next=pb->next;
printf ("The node is deleted\n"); }
Else
Free (PB);
printf ("The node not been found!\n");
End
return head;
}
Type * INSERT (type * head,type * pi)
{
TYPE *PB, *PF;
Pb=head;
if (head==null)
{head=pi;
pi->next=null; }
Else
{
while ((Pi->num>pb->num) && (pb->next!=null))
{PF=PB;
pb=pb->next; }
if (pi->num<=pb->num)
{if (HEAD==PB) Head=pi;
else pf->next=pi;
pi->next=pb; }
Else
{pb->next=pi;
pi->next=null; }
}
return head;
}
void Print (TYPE * head)
{
printf ("number\t\tage\n");
while (Head!=null)
{
printf ("%d\t\t%d\n", head->num,head->age);
head=head->next;
}
}
Main ()
{
TYPE * HEAD,*PNUM;
int n,num;
printf ("Input Number of node:");
scanf ("%d", &n);
Head=creat (n);
Print (head);
printf ("Input the deleted Number:");
scanf ("%d", &num);
Head=delete (Head,num);
Print (head);
printf ("Input" inserted number and Age: ");
pnum= (TYPE *) malloc (LEN);
scanf ("%d%d", &pnum->num,&pnum->age);
Head=insert (Head,pnum);
Print (head);
}
In this case, the print function is used to output the data field values for each node in the linked list. The initial value of the function's formal parameter head points to the first node of the linked list. In the while statement, the head value is changed to point to the next node after the value of the node is output. If you retain the head pointer, you should set another pointer variable, give it a heads value, and use it instead of the header. In the main function, n is the number of nodes to establish, NUM is the data field value of the node to be deleted; The head is a pointer to the linked list, pnum to the point of the node to be inserted. The meanings of the rows in the main function are:
Enter the node number of the linked list in line six;
The seventh line creat function establishes the linked list and returns the pointer to head;
The Eighth line print function output linked list;
Line Tenth Enter the study number of the node to be deleted;
The 11th line of the delete function deletes a node;
The 12th line print function output linked list;
The 14th line malloc function allocates a node's memory space, and assigns its address to pnum;
Line 15th Enter the data field value for the node to be inserted;
The 16th line inserts the function to insert the node which the pnum refers to;
Line 17th again the Print function output list.

From the operation results, first set up 3 nodes of the linked list, and output its value, and then delete the 103th node, only 105,108 nodes, and enter 106th node data, inserted in the list of nodes in 105,106,108. Joint "union" is also a data structure of a constructed type. There are many different types of data that can be defined within a "union", and a variable that is described as a "union" is allowed to load any one of the data defined by that union. This can not be done in any of the previous data types. For example, a variable defined as an integral type can only load integer data, and a variable that is defined as a solid can only be given a solid type of data.

There are many examples of this in practical matters. For example, in school teachers and students fill in the following form: Name Age Occupation unit "occupation" one can be divided into "teacher" and "student" two categories. For "unit" a student should fill in the class number, the teacher should fill in a certain department. The class can be used as an integer to indicate that the department can only use character types. To require that the two types of data be filled into the "unit" variable, the "unit" must be defined as a "union" of two types, including an integral type and a character-type array.

There are some similarities between "union" and "structure". But the two are fundamentally different. In the structure, each member has its own memory space, and the total length of a structure variable is the sum of the length of each member. In union, each member shares a memory space, and the length of a union variable equals the longest length of each member. It should be explained that the so-called sharing here does not mean that multiple members are loaded into a union variable at the same time, but that the union variable can be assigned to any member value, but only one value can be assigned at a time, and the new value is flushed to the old value. As described in the previous "unit" variable, such as defined as a can be loaded into the "class" or "Teaching and learning" after the union, you can give an integer value (Class) or string (Teaching section). Either give an integer value, or give it a string, and you cannot give both of them. Definitions of union types and descriptions of union variables a union type must be defined before the variable can be described as the Union type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.