There are two linked lists A and B. The node contains the student ID and name. Delete the nodes with the same student ID from the linked list A and linked list B.

Source: Internet
Author: User
# Include <stdio. h>
# Include <stdlib. h>
# Define N 10
Typedef struct student
{
Int num;
Float score;
Struct student * next;
} Stu;

Stu * Create ()
{
Int I;
Stu * P, * head = NULL, * tail = head;
For (I = 0; I <n; I ++)
{
P = (Stu *) malloc (sizeof (Stu ));
Scanf ("% d % F", & P-> num, & P-> score );
P-> next = NULL;
If (p-> num <0)
{
Free (P );
Break;
}
If (Head = NULL)
Head = P;
Else
Tail-> next = P;
Tail = P;
}
Return head;
}

Void output (Stu * P)
{
While (P! = NULL)
{
Printf ("% d \ t %. 2f \ n", p-> num, p-> score );
P = p-> next;
}
}

Stu * del (Stu * a, Stu * B)
{
Stu * head, * P1, * P2;
P1 = P2 = head = A; // point the P1, P2, and head nodes to the head of linked list.
While (B! = NULL)
{
P1 = P2 = head; // Let P1 and P2 always point to the header of the deleted linked list before each loop
While (P1! = NULL)
{
If (B-> num = p1-> num) // if the student ID is the same, delete the node information.
If (p1 = head) // If the header node is deleted, the position of the header node needs to be moved back.
{
Head = p1-> next;
Free (P1 );
P1 = P2 = head;
}
Else // if you delete an intermediate node
{
P2-> next = p1-> next;
Free (P1 );
P1 = P2-> next;
}
Else // if the student ID is different, the P1 and P2 pointers move backward.
{
P2 = p1;
P1 = p1-> next;
}
}
B = B-> next;
}
Return head;
}

Int main (INT argc, char * argv [])
{
Stu * a, * B, * C;
Printf ("\ n enter the information of linked list A. If the student ID is less than zero, end the input: Format (student ID score) \ n ");
A = create ();
Printf ("\ n enter the information of linked list B. If the student ID is less than zero, end the input: Format (student ID score) \ n ");
B = create ();
System ("CLS ");
Printf ("\ n linked list a information: \ n ");
Output ();
Printf ("\ n linked list B information: \ n ");
Output (B );
C = del (A, B );
Printf ("\ n ");
Output (C );

Return 0;
}

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.