List - other basic operations

Way linked list of the following:

Creating
Output
seeking long table
lookup value
insert the value
delete nodes

Code:

#include <the iostream> 
#include < String > 
#include <the cmath> 
#include <algorithm>
 the using  namespace STD;
 / * 
Create 
output 
required length table 
lookup value 
added value (node) 
delete nodes 
* / 
// node structure 
struct the Node 
{ 
    int V; 
    the Node * Next; 
}; 
// Create a list 
the Node create_lian * ( int n-) 
{ 
    the Node * = A new new the Node, * B; 
    B = A; 
    CIN>>a->v;
    a->next=NULL;
    for(int i=2;i<=n;i++)
    {
        a->next=new Node;
        a=a->next;
        cin>>a->v;
        a->next=NULL;
    }    
    cout<<"Node created."<<endl;
    return b;
}
//输出链表 
void out_lian(Node *p)
{
    do
    {
        coutp-<<> V << endl; 
        P = p-> Next; 
    } 
    the while (! P = NULL); 
} 
// find the table long 
int lian_len (the Node * P) 
{ 
    int C = 0 ;
     the while (P =! NULL) 
    { 
        C ++ ; 
        P = p-> Next; 
    } 
    return C; 
} 
// find the location where the value of 
int lian_search (the Node X *, int Y) 
{ 
    int P = 0 ;
     the while (X =!  NULL)
    { 
        P ++ ;
         IF (X-> V == Y) 
        { 
            return P; 
        } 
        X = X-> Next; 
    } 
    return P; 
} 
// Append value (node) 
void lian_append (the Node X *, int Y) 
{ 
    // prepare additional nodes to be 
    the node T; 
    TV = Y; 
    t.next = NULL;
     // find the end of the node list 
    the while (X-> Next =! NULL) 
    { 
        X = X-> Next; 
    } 
    // append 
    X-> the Next = & t; 
} 
// Delete the value (node) because there may be the first point is to delete the values, you must return to the first address of the new list. 
* Lian_del the Node (the Node X *, int Y) 
{ 
    the Node * X = head, * pre;
     // processing element to be deleted (continuous) beginning with the case of 
    the while (X-> V = X == Y &&! NULL) 
    { 
        head = X-> Next;
         Free (X); 
        X = head; 
    } 
    pre = head; 
    X = head-> Next;
     the while (! X = NULL) 
    { 
        IF (X-> V == Y) 
        {
            pre -> X-Next => Next;
             Free (X); 
            X = pre-> Next; 
        } 
        the else 
        { 
            pre = X; 
            X = X-> Next; 
        } 
    } 
    return head; 
} 
main () 
{ 
    int n-, X ; 
    the Node * head; 
    CIN >> n-; 
    head = create_lian (n-);
     / * // display nodes 
    COUT << lian_len (head) << endl; 
    // Find position x where 
    cin >> x; 
    cout << lian_search (head, x) ; 
    // value added at the end of the list (node)
    X >> CIN; 
    lian_append (head, X); * / 
    // from the list Remove value 
    CIN >> X; 
    head = lian_del (head, X); 
    out_lian (head); 
}

end here

Guess you like

Origin www.cnblogs.com/wanjinliu/p/11409997.html