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

Source: Internet
Author: User

The creat function is used to create a linked list with n nodes, which is a pointer function that returns a pointer to the STU structure. A pointer variable of three STU structures is defined within the creat function. Head pointer, pf is a pointer variable that points to the previous node of the two adjacent nodes. PB is the pointer variable of the latter node. Within a For statement, the malloc function is used to establish a space with the length equal to the Stu length as a node, and the first address is given PB. Then enter the node data. If the current node is the first node (i==0), the PB value (the node pointer) is assigned to head and PF. If not the first node, the PB value is given to the pointer domain member next of the node referred to by PF. PB refers to the node as the current last node, and its pointer field is NULL. The PB value is then given to PF for the next cycle preparation.
The form parameter n of the Creat function, representing the number of nodes in the linked list, as the loop count for the for statement. Figure 7.4 shows the process of executing the creat function.

[Example 7.11] Write a function that finds the node in the list by school number.
Type * Search (type *head,int N)
{
TYPE *p;
int i;
P=head;
while (P->num!=n && p->next!=null)
p=p->next; /* Not to find the node to move one step.
if (p->num==n) return (p);
if (p->num!=n&& p->next==null)
printf ("Node%d has not been found!\n", n
}
The symbolic constant type used in this function is the same as the macro definition for example 7.10, equal to struct stu. The function has two parameters, the head is the pointer variable to the linked list, and N is the number to find. Enter the while statement, checking to see if the NUM member of the node is equal to N, and if not equal to N and the pointer field is not equal to null (not the last node), move back one node to continue the loop. If the node is found, the node pointer is returned. If the end of the loop is still not found, the message "not Found" is printed.

[Example 7.12] Write a function that deletes the specified node in the linked list. There are two ways to delete a node:
1. The deleted node is the first node. This situation simply causes the head to point to the second node. namely Head=pb->next. The process is shown in Figure 7.5.
2. The deleted node is not the first node, which causes the previous node of the deleted node to point to the last node of the deleted node. namely Pf->next=pb->next. The process is shown in Figure 7.6.
The function is programmed as follows:
Type * Delete (type * head,int num)
{
TYPE *PF,*PB;
if (head==null) * * Such as empty table, output hint information * *
{printf ("\nempty list!\n");
Goto END;}
Pb=head;
while (Pb->num!=num && pb->next!=null)
/* When the node is not to be deleted, and is not the last node, continue the loop.
{Pf=pb;pb=pb->next;} /*PF point to the current node, PB point to the next node * *
if (pb->num==num)
{if (pb==head) head=pb->next;
* * If a deleted node is found and is the first node, the head is pointed to the second node.
Otherwise, the pointer of PF's point points to the next node.
else pf->next=pb->next;
Free (PB);
printf ("The node is deleted\n");}
Else
printf ("The node not been foud!\n");
End
return head;
}
The function has two parameters, the head is the pointer variable that points to the first node of the list, and the number of the NUM deletion node. First of all, it is impossible to determine whether the linked list is empty or not. If not empty, the PB pointer points to the first node in the list. Search for deleted nodes one by one after entering the while statement. Find the deleted node and see if it's the first node. If the head points to the second node (that is, the first node is deleted from the chain), otherwise the deleted node of the previous node (PF refers to) point to the deleted node after the end of the node (Deleted node pointer field refers to). If the end of the loop is not found to delete the node, then the output "find" message. Finally, the head value is returned.

[Example 7.13] Write a function that inserts a node at the specified position in the list. Inserting a node at a specified position in a linked list requires that the list itself be ordered in accordance with a certain regularity. For example, in a student data list, you need to insert a node in the order in which you want to learn numbers. The pointer to the inserted node is pi. Can be inserted in three different situations.
1. The original table is empty table, just make head point to be inserted node. See Figure 7.7 (a)
2. The value of the inserted node is the smallest and should be inserted before the first node. In this case, the head is pointed to the inserted node, and the pointer field of the inserted node points to the original first node. namely:pi->next=pb;
Head=pi; See Figure 7.7 (b)
3. Insert at other locations, see Figure 7.7 (c). In this case, the pointer field of the previous node of the insertion position is pointed to the inserted node, so that the pointer field of the inserted node points to the latter node of the insertion position. That is the:pi->next=pb;pf->next=pi;
4. Insert at the end of the table, see Figure 7.7 (D). In this case, the end node pointer field of the original table is pointed to the inserted node, and the inserted node pointer field is NULL. That
pb->next=pi;
pi->next=null; type * INSERT (type * Head,type *pi)
{
TYPE *PF,*PB;
Pb=head;
if (head==null)/* Empty table Insert * *
(HEAD=PI;
Pi->next=null;}
Else
{
while ((Pi->num>pb->num) && (pb->next!=null))
{PF=PB;
pb=pb->next; }/* find the insertion position * *
if (pi->num<=pb->num)
{if (HEAD==PB) head=pi;/* inserted before the first node. *
else pf->next=pi;/* Insert in other position * *
pi->next=pb; }
Else
{pb->next=pi;
Pi->next=null;} /* Insert at end of table * *
}
return head;}
This function has two parameters are pointer variables, head pointing to the linked list, Pi point to the inserted node. The function first determines whether the linked list is empty or empty, so that the head points to the inserted node. If the table is not empty, the insert position is searched with a while statement. Find and then determine whether to insert before the first node, if the head point to the inserted node is inserted node pointer field points to the original first node, otherwise inserted in other locations, if the inserted node is greater than all nodes in the table, then inserted at the end of the table. This function returns a pointer, which is the head pointer of the linked list. When the insertion position precedes the first node, the inserted new node becomes the first node in the list, so the head value is changed, so the pointer needs to be returned to the keynote function.

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.