# Delete duplicate values in a single-chain table. duplicate values in a single-chain table.

Source: Internet
Author: User

Delete duplicate values in a single-chain table. duplicate values in a single-chain table.
Question: Delete the elements that repeat values in a single-chain table of the lead node (that is, leave each value with only one element)

Solution:

Use a dynamic auxiliary storage array. Each time you want to add an element to the secondary array, add 1 to the length of the secondary array. The maximum length is the same as that of a single-chain table. Set a pointer to p, let it point to the header node, starting from the first element in the single-chain table, put its value into the auxiliary array, and then access the elements behind the single-chain table in sequence, compare the value of this element with the values of all elements that have been assigned values in the array. If the value is not equal to the value of any element in the array, then let p's next point to the next pointer of the node, delete the node element, otherwise let p point to p's next pointer, and put the value of the currently accessed node element into the auxiliary array in sequence, access the last element in a single-chain table.

# Define ElemType int
Typedef struct LNode {
ElemType data;
LNode * next;

Algorithm Implementation:

Void deleteRepeatValue (LinkList & L) {// if the single-chain table is empty, or only the header node or the header node has only one element after it, it is impossible to have element with duplicate values if (L = NULL | L-> next = NULL) return; LNode * p = L; // The previous node of the current access node LNode * q = L-> next; // The currently accessed node int count = 1; elemType * temp = (ElemType *) malloc (sizeof (ElemType) * count); temp [count-1] = q-> data; while (q-> next) // continue accessing until the last node {q = q-> next; // access the next node bool flag = false; // indicates whether the value in the element of the current access node is the same as that of the element of the previous Access Node. for (int I = 0; I <coun T; I ++) {if (q-> data = temp [I]) {p-> next = q-> next; free (q); flag = true; break;} if (! Flag) {count ++; temp = (ElemType *) realloc (temp, sizeof (ElemType) * count); temp [count-1] = q-> data; q = q-> next; // Let q go further to ensure that p is the previous node of the next Access Node} q = NULL; // at this time, q may be a released pointer. If it is not pointed to NULL, it will become a wild pointer}

Related Keywords:

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.