034 keep it up)

Source: Internet
Author: User

034 keep it up)

 

 

Description:

Enter a linked list and output the last k nodes in the list.
(Hint: Be sure to use the linked list .)

 

Input:

The input may contain multiple test examples. The input ends with EOF.
For each test case, the first input behavior is composed of two integers n and k (0 <= n <= 1000, 0 <= k <= 1000 ): n indicates the number of elements in the linked list to be input, and k indicates the last element to be queried.
The second row of the input contains n count t (1 <=t <= 1000000): represents the elements in the linked list.

 

Output:

Corresponding to each test case,
If there is a result, the corresponding search result is output. Otherwise, the output is NULL.

 

Sample input:
5 21 2 3 4 51 05
Sample output:
4NULL
This question is relatively simple and straightforward:

 

 

#include 
           
            #include 
            
              typedef struct SNode{    int data;    struct SNode *next;}SNode; SNode* createNode(int vData){    SNode *Node = (SNode*)malloc(sizeof(SNode));    Node->data  = vData;    Node->next  = NULL;    return Node;} void deleteNode(SNode **vNode){    (*vNode)->next = NULL;    free(*vNode);    *vNode = NULL;} SNode* createLinkList(int vN){    int    i;    int    Data;    SNode *Head;    SNode *Node;    SNode *TempNode;     scanf(%d, &Data);    Head = createNode(Data);    TempNode = Head;     for (i = 1; i < vN; ++i)    {        scanf(%d, &Data);        Node = createNode(Data);        TempNode->next = Node;        TempNode = Node;    }     return Head;} SNode* findKey(SNode *vHead, int vN, int vK){    if (vN == vK) return vHead;     int    i;    SNode *pNode;    SNode *qNode;     i     = 0;    pNode = vHead;    qNode = vHead;     while (i < vK && qNode != NULL)    {        qNode = qNode->next;        ++i;    }     if (qNode == NULL) return NULL;     while (qNode != NULL)    {        pNode = pNode->next;        qNode = qNode->next;    }     return pNode;} void destroyLinkList(SNode **vHead){    SNode *Node;    while (*vHead != NULL)    {        Node = (*vHead)->next;        deleteNode(vHead);        *vHead = Node;    }} int main(){    int N;    int K;    SNode *Head;    SNode *Ret;     while (scanf(%d %d, &N, &K) != EOF)    {        if (K == 0 && K > N)         {            printf(NULL);            continue;        }         Head = createLinkList(N);        Ret  = findKey(Head, N, K);        if (Ret == NULL)        {            printf(NULL);        }        else        {            printf(%d, Ret->data);        }         destroyLinkList(&Head);    }     return 0;}/**************************************************************    Problem: 1517    User:     Language: C    Result: Accepted    Time:100 ms    Memory:912 kb****************************************************************/
            
           


 

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.