Calculate the number of vertices to which the linked list belongs and the number of vertices to which the List belongs.

Source: Internet
Author: User

Calculate the number of vertices to which the linked list belongs and the number of vertices to which the List belongs.

Using two pointers is similar to determining whether a linked list forms a ring.

Code:

#include <iostream>#include <list>using namespace std;typedef struct node {int data;struct node  *next ;}Node,*pNode;void creatNode( pNode &pHead ){bool isFirst=true;pNode p,q;int temp;scanf("%d",&temp);while(temp){p=(pNode)malloc(sizeof(Node));p->data = temp;p->next = NULL;if(isFirst){q=pHead=p;isFirst = false;}else{q->next = p;q = p;}scanf("%d",&temp);}}void print(pNode p){while(p){cout<<p->data << " ";p = p->next;}}pNode printLast(pNode pHead,unsigned int m){if(NULL == pHead || m==0)return NULL ;pNode pAhead = pHead;pNode pBehind = pHead;for(int i=1;i<=m;i++){if(pAhead->next != NULL)pAhead = pAhead->next;elsereturn NULL;}while(pAhead){pAhead = pAhead->next;pBehind = pBehind->next;}return pBehind;}int main(){   pNode pHead=NULL;creatNode(pHead);print(pHead);    cout<<endl<< printLast(pHead,2)->data;    return 0;}

Running result:



K-th node at the bottom of a single-chain table

Node * create_Link (int num)
{
Node * head, * p, * q;
P = new Node ();
P-> data = 1;
Head = p;
For (int I = 2; I <= num; I ++)
{
Q = new Node ();
Q-> data = I;
P-> next = q;
P = q;
}
P = NULL; // The Last node points to NULL
Return head;
}

Node * FindK (Node * head, int k)
{
Node * pslow, * pfast;
Pslow = pfast = head;
Int I = 0;
While (I <k)
{
Pfast = pfast-> next;
I ++;
}
While (pfast)
{
Pfast = pfast-> next;
Pslow = pslow-> next;
}
Return pslow;
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Node * head = create_Link (10 );
Node * kNode = FindK (head, 2 );
Cout <kNode-> data <endl;

System ("pause ");
Return 0;
}

The number of the last k elements in a single-chain table must be: the number of elements in the linked list cannot be used.

Since the number of elements in the linked list cannot be used, you have to find another method.
Let me give you some ideas. You have written the program yourself.
Algorithm idea:
1. If you know the pointer to the header node in a single-chain table, you can keep searching for the element along the pointer field of the node.
2. In this way, we can start from the first node and determine whether this node is the last k to the end of the single-chain table. If not, continue to take the second node value, then, judge whether the node ends at the end of the single-chain table as the last k. (The judgment is a loop)

Another algorithm, the most stupid of which is to convert a linked list to a sequence table. However, the space of this sequence table is a problem.

I don't want to do that anymore. Practice by yourself.

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.