Implements an algorithm to return the nth element (keep it up) from a single-chain table, and to a single-chain keep

Source: Internet
Author: User

Implements an algorithm to return the nth element (keep it up) from a single-chain table, and to a single-chain keep
We maintain two pointers with a distance of n. Then, I move these two pointers synchronously on the single-chain table to keep them at a distance of n. So,

When the second pointer is null, the first pointer is required.

#include <iostream>struct Node{int   data;Node* next;};void initList(Node* vNode){for (int i=0; i < 20; ++i){Node* TempNode = new Node;TempNode->data = i;TempNode->next = vNode->next;vNode->next    = TempNode;}}Node* getNthBackWards(const Node* vNode, int vN){if (vN < 1 || vNode == NULL) return NULL;Node* p = vNode;Node* q = vNode;while (vN>0){++q;if (q == NULL) return NULL;--vN;}while (q != NULL){++q;++p;}return p;}int main(){Node* Root = new Node;Root->next = NULL;initList(Root);Node* Result = getNthBackWards(Root, 7);std::cout << Result->data << std::end;return 0;}



Find the last K elements in a single-chain table with N elements

Weird.
I already know that there are N elements? What is the difference between reciprocal and positive number? The last K, is the positive number of N-K + 1, the pointer from the beginning of the node to move the N-K + 1 returned is to the elements.
If you do not know how many elements there are, you can only create one queue. the queue length is K. You can use a pointer to start searching from the first node and then start to join the queue at one node, the queue goes out when it is full. when the Pointer Points to the end node, the first element of the queue is the last K.
You can write specific algorithms by yourself. It is not complicated.

Design an algorithm to delete duplicate elements from a single-chain table and keep the relative order of the remaining elements unchanged.

If it is a leading pointer:
Void Derepeat (LinkList & L)
{
LinkList p1, p2, p3;
P1 = p3 = L-> next;
P2 = L-> next;
While (p1-> next)
{
While (p2-> next)
{
If (p2-> data = p1-> data)
{
P3-> next = p2-> next;
P2 = p2-> next;
}
Else
{
P3 = p3-> next;
P2 = p2-> next;
}
}
P1 = p1-> next;
P3 = p1;
P2 = p1-> next;
}
}
I don't know if I have never run it, right? I hope it will help you.

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.