c language --- list

Array of structures simple and practical, but if you want to complete the insertion in the specified location or remove an element of the operating room a very troublesome, it needs to operate multiple elements behind the move, but does not alter the size of the array, in order to solve this problem, linked list data structures may be employed.

I. Overview of the list

In the list structure for each element of the memory cell becomes the node application.

 

struct List 
{ 
   int NO;
    int Score;
    struct List * Next;             
}; 
// * listp node list for the current operation, * p for the new node, * listhead head for the link pointer
struct * p, * listp, * listhead;
// malloc memory allocation size bytes, a successful return a pointer to an assigned address; failure returns NULL after
p = (struct list *) malloc (sizeof (struct list)); // get a new pointer

listhead = p; // the operation is the first time create new pointer
listp = the p-;
listp-> the Next = NULL;
listp-> NO = 5
listp-> Score = 86;

 

Example: Using a linked list O 5 student achievement and student number

#include<stdio.h>
#include<stdlib.h>

struct list
{
    int no;
    int score;
    struct list *next;
 }; 
 
int main()
{
    int k;
    struct list *p,*listhead,*listp;
    for(k=1;k<=5;k++){
        p = (struct list*)malloc(sizeof(struct list));
        if(k==1){
            listhead=p;
        }
        else{
            listp->next=p;
        }
        listp = p;
        listp->next = NULL;
        printf("please input %d number and score:",k);
        scanf("%d %d",&listp->no,&listp->score);
    }

//打印输出各个节点 
    p = listhead;
    while(p!=NULL)
    {
        printf("%d student score is %d\n",p->no,p->score);
        PP-=> Next; 
    } 
        return  0 ; 
    
    P = The listhead;
 // release memory space each node 
    the while (P =! NULL) { 
        listp = P; 
        P = p-> Next;
         Free (listp); 
    } 
}

Second, the basic operation of the list

(1) establish a list

All data nodes (2) the list of output

(3) Insert a new node in the linked list

  (4) delete a node list

#include<stdlib.h>
#include<stdio.h>

struct list{
    int data;
    struct list *next;
};

struct list *dele(struct list *head);
struct list *insert(struct list *head);
void display(struct list *head);
struct list * create();
struct list * deleall(struct list *listhead);

int main()
{
    int ch;
    struct list * head;
    head = NULL; 
    
    the while ( . 1 ) { 
        the printf ( " 1. established list \ n- " ); 
        the printf ( " 2. Insert node \ n- " ); 
        the printf ( " 3. delete nodes \ n- " ); 
        the printf ( " . 4. display node \ n- " ); 
        the printf ( " of 0. The exit the program \ n- " ); 
        Scanf ( " % D " , & CH); 
        
        Switch (CH) {
             Case  . 1 : 
                head = create();
                break;
            case 2:
                head = insert(head);
                break;
            case 3:
                head = dele(head);
                break;
            case 4:
                display(head);
                break;
            case 0:
                head = deleall(head);
        }
        if(ch == 0) break; 
    }
    
    return  0 ; 
} 
struct List * Create () {
     struct List * The listhead, * P, * listp;
     int X; 
    The listhead = NULL; 
    the printf ( " Enter Results: " ); 
    Scanf ( " % D " , & X); 
     the while (X = -! . 1 ) {
         IF ((P = ( struct List *) the malloc ( the sizeof ( struct List))) == NULL) { 
            the printf ( " memory application error! " ;)
            Exit ( 0 ); 
        } 
        IF (The listhead == NULL) { 
            The listhead = P; 
        } 
        the else { 
            listp -> Next = P; 
        } 
        listp = P; 
        listp -> Data = X; 
        listp -> Next = NULL; 
        the printf ( " Please enter next value (-1 input end) " ); 
        Scanf ( " % D " , & X); 
    } 
    return the listhead; 
} 

void the display (struct list *listhead){
    struct list *p;
    if(listhead == NULL){
        printf("链表为空!\n");
        return ;
    }
    p = listhead;
    while(p!=NULL){
        printf("%d\n",p->data);
        p = p->next;
    } 

}

struct list *insert(struct list *listhead){
    struct list *p,*listp;
    intK, I, X;
     IF (The listhead == NULL) { 
        the printf ( " list has not been established " ); 
         return The listhead; 
    } 
    the printf ( " Please enter the data to be inserted and the position w " ); 
    Scanf ( " % D% D " , X &, & I); 
    K = . 1 ; 
    listp = The listhead;
     the while ! (listp = NULL && K <I- . 1 ) { 
        listp = listp-> Next; 
        K = K + . 1 ; 
    } 
    IF (K <I- . 1I || < . 1 ) { 
        the printf ( " Invalid insertion point \ n-! " ); 
    } 
    the else  IF (I == . 1 ) {
         IF ((P = ( struct List *) the malloc ( the sizeof ( struct List))) == NULL) { 
            the printf ( " application memory error! " ); 
            Exit ( 0 ); 
        } 
        P -> Data = X; 
        P -> Next = The listhead; 
        The listhead = P; 
    } 
    the else{
        if((p=(struct list *)malloc(sizeof(struct list)))==NULL){
            printf("申请内存错误!");
            exit (0); 
        }
        p->data=x;
        p->next=listp->next;
        listp->next=p;
    }
    return listhead;
}

struct list *dele(struct list *listhead){
    struct list *listp,*p;
    int I, K;
     IF (The listhead == NULL) { 
        the printf ( " sub-list is empty! " );
         return The listhead; 
    } 
    the printf ( " Please enter the node to be deleted \ n- " ); 
    Scanf ( " % D " , & I); 
    K = . 1 ; 
    listp = The listhead;
     the while ! (listp = NULL && K <I- . 1 ) { 
        listp = listp-> Next; 
        K = K + . 1 ; 
    } 
    IF (K <I- . 1 || i<1){
        printf("此节点为无效插入点\n");
    }
    else if(k==1){
        p=listhead;
        listhead=p->next;
        free(p);
    }
    else if (i>1 && k>=i-1){
        p =listp->next;
        listp->next=p->next;
        free(p);
    }
    return listhead;
}

struct list *deleall(struct list *listhead){
    struct list *p;
    p = listhead;
    while(p!=NULL){
        listhead=p->next;
        free(p);
        p=listhead;
    }
    return p;
}

Operating results map

 

Guess you like

Origin www.cnblogs.com/changfan/p/11665500.html