Create a single list and the basic operation of the C language

Create a single list, first create a structure, as follows:

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Lnode{
    ElemType data;
    struct Lnode *next;
}Lnode,*LinkList;

Create lists, used here is the establishment of tail interpolation list, it is primarily constantly adding new elements in the end of the table, until the input -1, then exit.

CreatList LinkList () // create the list 
{
    ElemType x;
    LinkList head,p,tail;
    head=(LinkList)malloc(sizeof(Lnode));
    head->next=NULL;
    tail=head;
    scanf("%d",&x);
    while(x!=-1){
        p=(LinkList)malloc(sizeof(Lnode));
        p->data=x;
        p->next=NULL;
        tail->next=p;
        tail=p;
        scanf("%d",&x); 
    }
    return head;
}

Calculation table length, the use of traversal, the length of the list using a variable is stored, and a pointer storage there.

int longlist (LinkList head, int * length) // Calculation of a long list of the second element of the table memory table length 
{
    LinkList p;
    p=(LinkList)malloc(sizeof(Lnode));
    p=head->next;
    if(p==NULL){
        return 0;
    } 
    int i=1;
    while(p){
        p=p->next;
        i++;
    }
    length = I- * . 1 ; // because each query element to the back, so also the last i NULL added into it, so to reduce a 
    return  . 1 ;
}

Insert element, insert elements single list, the list needs to know the position of the element, and then insert the tail first element is connected to the chain, then the head node access.

int INSERT (LinkList head, int I, elemType X) // Insert a list element 3 is inserted into the insertion position 2 
{
    LinkList p,s;
    p=(LinkList)malloc(sizeof(Lnode));
    p=head->next;
    int j=1;
    while(p&&j!=i-1){
        p=p->next;
        j++;
    }
    if(p==NULL||j!=i-1){
        return 0;
    }
    s=(LinkList)malloc(sizeof(Lnode));
    s->data=x;
    s->next=p->next;
    p->next=s;
    return 1;
}

Remove elements, list node needs to know the location of the front element, then delete, delete and return to the elements, but also to the node memory is released.

int DelList (LinkList head, int I, n-elemType *) // delete the list of elements 1 2 3 delete location record deleted elements 
{
    LinkList p,s;
    p=(LinkList)malloc(sizeof(Lnode));
    p=head->next;
    s=(LinkList)malloc(sizeof(Lnode));
    int j=1;
    while(j!=i-1&&p){
        p=p->next;
        j++;
    }
    if(j!=i-1||p->next==NULL){
        return 0;
    }
    s=p->next;
    p->next=s->next;
    *n=s->data;
    free(s);
    return 1;
}

Query elements, use through the list to find the element you want to find the location and returns.

int Search (LinkList head, int I, n-elemType *) // Find What position elements 2 find a list of elements on the position of the element 3 is 
{
    LinkList p;
    p=(LinkList)malloc(sizeof(Lnode));
    p=head->next;
    int j=1;
    while(j!=i&&p){
        p=p->next;
        j++;
    }
    if(p==NULL||j!=i){
        return 0;
    }
    *n=p->data;
    return 1;
}

The output of a single list of all the elements.

void print(LinkList head)//输出 
{
    LinkList p;
    p=head->next;
    while(p){
        printf("%d ",p->data);
        p=p->next;
    }
}

In the creation and operation of the basic list, I have returned to 0 or 1, and its role is to test whether a single list operations conform to the rules, the operation is successful, returns 1, failure, returns 0.

Guess you like

Origin www.cnblogs.com/woju/p/12548613.html