Pointer - I might just learn c ++ fur

C. linear list of insertion and deletion

A single point of time: 2.0 sec

Memory Limit: 256 MB

Achieve a linear linked list insertion and deletion operations

Just to complete the definition given function.

NODE* insertLinklist(NODE* head, int tar, int val) { // TODO } NODE* deleteLinklist(NODE* head, int tar) { // TODO } 

among them

NODEIt represents a linked list structure is defined as follows

typedef struct Node
 { int Data ; // store data struct Node * Next ; // pointer to point to the next node } NODEs ;

headPointing to the first node of the linked list, if the list is empty, headasNULL

Clearly, in order to complete the list of the maintenance work, you need to achieve these two auxiliary functions

Insert operation:

tar, valRepresents the new value will be stored V A L node is inserted into the T A R & lt after node

(Ensure that when the list is not empty, the value of T A R & lt node must be in the list, if a plurality of nodes stored values are T A R & lt Thereafter, the inserted first, if the list is empty, then ignoring T a R & lt value, V a L is inserted into headthe back, about to headpoint to the new node)

Delete operation:

tarIt means to delete stored value T A R & lt node (if the node is stored in the plurality of values T A R & lt , then remove the first)

If the list is empty then ignore the current operation

prompt

Only use C or C ++ submit.

For C / C ++, a program similar topic sentence, as follows:

#include <stdio.h> #include <stdlib.h> typedef struct Node { int Data ; struct Node * Next ; } NODEs ; NODEs * insertLinklist ( NODEs * head , int the tar , int Val ); NODEs * deleteLinklist ( NODEs * head , int the tar ); / * your code will be embedded at this location * / int main ()
 { / * Input and other processing details hidden from table * / NODEs * head = createLinklist ( / * create a list, the details hidden from table * / ); for ( / * number of operations, details are hidden from the table * / ) IF ( / * Analyzing insert or delete details hidden from table * / ) head = insertLinklist (); the else head = deleteLinklist (); / * a subsequent sentence title, details hidden from table * / return 0 ; }

Although this is only a not test water title, but it made me have a big head;
NODE* insertLinklist(NODE* head, int tar, int val) 
{
    if(head==NULL)//
    {
        NODE* p=(NODE*)malloc(sizeof(NODE));
        p->data=val;
        p->next=NULL;
        head=p;
    }
    else
    {
        NODE* p=head;
        NODE* pnew=(NODE*)malloc(sizeof(NODE));//动态申请指针 
        while(p->data!=tar)
        {
            p=p->next;
        }
        pnew->data=val;
        pnew->next=p->next;
        p->next=pnew;
    }
    
    return head;
} 

NODE* deleteLinklist(NODE* head, int tar)
{
    if(head==NULL) return head;
    if(head->data==tar)
    {
        head=head->next;
        return head;
    }
    NODE* last=head;
    NODE* now=head->next;
    while(now->data!=tar)
    {
        now=now->next;
        last=last->next;
    }
    last->next=now->next;
    return head;
}

Pointers or with -> it, or would CE;

NODE * p and NODE * p equivalent, see the habit;

Also to be noted pointer = pointer type to be optimistic;

If you want to find the address of a variable, use int * p = & a;

& Take Address

 

Guess you like

Origin www.cnblogs.com/WHFF521/p/11223735.html