PTA data Structure 6-2 single linked list element positioning __PTA data structure

Source: Internet
Author: User
6-2 Single Linked list element positioning (12 points)

It is required to find a node in the list that has the value x in the First data field, and return the bit order of the node. L is a single linked list of leading nodes, function listlocate_l (linklist L, Elemtype x) requires a node in the list to find the first data field value x, returns its bit order (starting from 1), and returns 0 if it is not found. For example, the elements of the original single linked list are 1,2,3,4, then listlocate_l (l, 1) returns 1,listlocate_l (L, 3) returns 3, while listlocate_l (l, 100) returns 0. function Interface Definition:

int listlocate_l (linklist L, Elemtype x);

Where L is a single linked list of leading nodes. X is a given value. The function must find the node in the list that has the first data field value X. If found, returns its bit order (starting from 1) and returns 0 if it is not located. Sample Referee Test procedure:

The library function header file contains #include <stdio.h> #include <malloc.h> #include <stdlib.h>//Function status Code definition #define TRUE 1 #de Fine FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE-1 #define OVERFLOW-2 typedef int S
Tatus; typedef int ELEMTYPE;
    It is assumed that the elements in the linear table are integral typedef struct LNODE {elemtype data;
struct Lnode *next;

}lnode,*linklist;   Status listcreate_l (linklist &l,int N) {Lnode *rearptr,*curptr;
    A tail pointer, a pointer to the new node l= (lnode*) malloc (sizeof (Lnode)); if (!
    L) exit (OVERFLOW);               l->next=null;  First, a single chain table rearptr=l of the leading node is established. The initial node is the tail, the rearptr points to the tail node for (int i=1;i<=n;i++) {//Each loop opens a new node, and the new node is spelled to the tail node after curptr= (lnode*) malloc (sizeof (Lnode));
        /Generate new node if (!curptr) exit (OVERFLOW);  scanf ("%d", &curptr->data);//INPUT element value curptr->next=null;
        Next empty rearptr->next=curptr; of the last node
    Rearptr=curptr;
return OK; //Below is the declaration of the function that needs to be implemented int listlocate_l (linklist L, ElemType x);
    int main () {linklist L;
    int n;   
    int x,k;  scanf ("%d", &n); The number of elements in the input list if (listcreate_l (l,n)!= OK) {printf (table creation failed ...)
          \ n ");
    return-1; } scanf ("%d", &x);
   Enter the element k=listlocate_l to find (l,x);
   printf ("%d\n", K);
return 0;
 /* Please fill in the answer here/*
Input Sample:
4
1 2 3 4
1
Output Sample:
1
The code is as follows
int listlocate_l (linklist L, Elemtype x) {  
    //Create a pointer to the current node  
    lnode* curptr = l->next;  
    Create counter counter, traversal, if the current node data field value =x, return position  
    int counter = 1;  
    If the current pointer next is empty, the loop ends while  
    (Curptr->next!= NULL) {  
        if (curptr->data = x) return  
            counter;  
        Otherwise, return 0 (for what?). ) 
        else {  
            curptr = curptr->next;  
            counter++  
        }}  
    }  
  

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.