Learning summary of linear table I

As a coder from general engineering to cs, I finally began to learn data structure in this semester. Here are some learning summaries. Here are some functions to be realized:
  • Create linked list
  • Find the length of chain list
  • print data
  • Find by sequence number
  • Search by value
  • Insertion of single chain table by sequence number
  • Delete the ith element of the list
  • Inverted linked list
  • Delete duplicate nodes in single chain table, sort ideas, double loops for comparison
  • Bubble sort

    Define structure
    struct node
    {
    int num;
    struct node *next;
    };
    typedef struct node *LinkList,Node;

Create linked list

LinkList create_LinkList();
LinkList create_LinkList()
{
    LinkList h=(LinkList)malloc(sizeof(Node));
    h->next=NULL;
    Node *s,*r=h;
    int x;
    scanf("%d",&x);
    while(x!=-1)
    {
        s=(LinkList)malloc(sizeof(Node));
        s->num=x;
        r->next=s;
        r=s;
        //s->next=r->next;
        //r->next=s;
        //r=s;
        scanf("%d",&x);
    }
    r->next=NULL;
    return h;
}

Find the length of chain list

int getLength_LinkList(LinkList h);
int getLength_LinkList(LinkList h)
{
    Node *p=h;
    int Length=0;
    while(p->next!=NULL)
    {
        p=p->next;
        Length++;
    }
    return Length;
}

print data

void print_LinkList(LinkList h);
void print_LinkList(LinkList h)
{
    Node *p;
    p=h->next;
    if(p==NULL)
        printf("LinkList is NULL!");
    printf("Head:");
    while(p!=NULL)
    {
        printf("%d -> ",p->num);
        p=p->next;
    }
    printf("NULL");
}

Find by sequence number

LinkList get_LinkList(LinkList h, int k);
LinkList get_LinkList(LinkList h, int k)
{
    Node *p=h;
    int i=0;
    while(p->next != NULL && i<k)
    {
        p=p->next;
        i++;
    }
    if(i==k)
        return p;
    else
        return NULL;
}

Search by value

LinkList locate_LinkList(LinkList h,int x);
LinkList locate_LinkList(LinkList h,int x)
{
    Node *p=h->next;
    while(p!=NULL&&p->num!=x)
        p=p->next;
    if(p==NULL)
        return NULL;
    else
        return p;
}

Insertion of single chain table by sequence number

LinkList insert_LinkList(LinkList h, int i, int x);
LinkList insert_LinkList(LinkList h, int i, int x)
{
    Node *p,*s;
    p=get_LinkList(h,i-1);
    if(p==NULL)
    {
        printf("Insertion position i Wrong!");
        return;
    }
    else
    {
        s=(LinkList)malloc(sizeof(Node));
        s->num=x;
        s->next=p->next;
        p->next=s;
        printf("Insert successfully!");
    }
}

Delete linked list

LinkList delete_LinkList(LinkList h, int i);
LinkList delete_LinkList(LinkList h, int i)
{
    Node *p,*q;
    p=get_LinkList(h,i-1);
    if(p==NULL||p->next==NULL)
    {
        printf("The first i-1 Nodes do not exist!");
        return;
    }
    else
    {
        q=p->next;
        p->next=p->next->next;
        free(q);
        printf("Delete successfully!");
    }
}

Inverted linked list

void remove_LinkList(LinkList h);
void remove_LinkList(LinkList h)
{
    Node *p,*q;
    p=h->next;
    h->next=NULL;
    while(p)
    {
        q=p;
        p=p->next;
        q->next=h->next;
        h->next=q;
    }
}

Delete duplicate nodes in single chain table, sort ideas, double loops for comparison

void pur_LinkList(LinkList h);
void pur_LinkList(LinkList h)
{
    Node *p,*q,*r;
    p=h->next;
    if(p!=NULL)
    while(p->next)
    {
        q=p;
        while(q->next)
        {
            if(p->num==q->next->num)
            {
                r=q->next;
                q->next=q->next->next;
                free(r);
            }
            else
                q=q->next;
        }
        p=p->next;
    }
}

Bubble sorting (can be optimized)

void bubblesort_LinkList(LinkList h);
void bubblesort_LinkList(LinkList h)
{
    Node *p,*q,*x,*y;
    p=h;
    if(p->next==NULL||p->next->next==NULL)
        return;
    for(;p->next->next!=NULL;p=p->next)
        for(q=p;q->next->next!=NULL;q=q->next)
        {
            if(q->next->num > q->next->next->num)
            {
                y=q->next->next;
                x=q->next;
                q->next=y;
                x->next=y->next;
                y->next=x;
            }
        }
}
The above is basically all the operations of linear single chain table, which can be used for reference.
As I don't like to write notes very much, I need to think about some places and draw pictures. Please forgive me.

Posted by Dilbert137 on Fri, 03 Apr 2020 03:18:37 -0700