Single Cycle chain list (C language implementation) __c language

Source: Internet
Author: User
Tags assert prev

//clist.h
//struct body definition and function declaration

#ifndef clist_h #define Clist_h #include <stdio.h> #include <assert.h> #include <malloc.h> #include &l

t;iostream> typedef int ELEMTYPE;
    typedef struct NODE {elemtype data;
struct Node *next;

}node, *pnode;
    typedef struct LIST {pnode head;
    Pnode tail;
int size;

}list,*plist;
BOOL Initlist (plist list);             void create_t (plist list,elemtype x);             Tail insert void Create_h (plist list,elemtype x);                        Head insert void Del_back (plist list);                       Tail delete void Del_front (plist list);                        Head delete void sortlist (plist list);           sort void Insert_val (plist list,elemtype x);
Insert Pnode Find (plist list,elemtype x) by value;
void Del_val (plist list,elemtype x);
void Modify (plist list,elemtype x1,elemtype x2);
void Clear (plist list);
void Destroy (plist list);
void Reserve (plist list);
int Length (plist list);
void menu ();
void Showlist (plist list);
void Show_tail (plist list); Elemtype Next (plist List,eleMtype x);
Elemtype Prio (plist list,elemtype x);

Pnode prev (plist list,pnode p); #endif

//clist.cpp
//function implementation

#include "CList.h" void menu ()///provide options for the menus function {printf ("*******************************************************
    \ n ");
    printf ("* [0] quit_system [1] create_t [2] create_h [3] showlist *\n");
    printf ("* [4] del_back [5] del_front [6] insert_val [7] show_tail*\n");
    printf ("* [8] Find [9] del_val [ten] sortlist [one] modify *\n");
    printf ("* [12]clear [13]destroy] resver [15]length *\n");
    printf ("* [] next [17]prio *\n");
printf ("***************************************************************\n"); BOOL Initlist (plist list) {List->head = (pnode) malloc (sizeof (Node));/Initialize a header node assert (List->head!= NULL)               ;            Assertion, the expression is true, and then down to execute List->head->next = list->head;

    Initializes the head and tail pointers so that they all point to the header node List->tail = list->head;                           list->size = 0;
Length initialized to 0 return true; } void create_t (plist list,Elemtype x)//tail interpolation {pnode s = (pnode) malloc (sizeof (Node));
    ASSERT (S!= NULL);                         S->data = x;                Fill data field list->tail->next = S;                Tail points to the last node, linking the newly established node to the last s->next of the linked list = list->head;                      Single cycle linked list, next point of new node List->tail = s;
Change the pointing list->size++; of the tail pointer
        } void Showlist (plist list)//list display function {if (1>list->size) {printf ("--linked list is empty \ n");
    return;
    } Pnode p = list->head->next;
        while (List->head!= p)//traversal of the linked list {printf ("%d", p->data);
    p=p->next;
printf ("\ n");
    } void Create_h (plist list,elemtype x)//head interpolation {pnode s = (pnode) malloc (sizeof (Node));
    ASSERT (S!= NULL);                             S->data = x;              Populating data fields S->next = list->head->next;                    The new node points to the first node List->head->next = s;
The head node next points to the new node s    if (list->size = 0)//If it is the first header, change the tail pointer and the next point of the tail node, and then do not change {List->tail = s;        
    List->tail->next = list->head;        } list->size++;
    Insert a, length plus 1} void Del_back (plist list)//tail Delete {if (0==list->size) return;
    Pnode p = list->head;
    while (List->head!= p->next->next)//Find the penultimate node {p = p->next;                   } P->next = list->head;                       Separate the last node from free (list->tail);                         Releases the last node list->tail = p;
    The tail pointer points to the original penultimate node (now the penultimate one) printf ("--the tail node has been deleted \ n");
list->size--;
    } void Del_front (plist list)//header Delete {if (0==list->size) return;
        else {Pnode p = list->head->next; if (1==list->size) {//Only one node, if deleted, need to change the tail pointer list->tail = LIST-&G
            T;head; List->head->next = list->head;      else {List->head->next = p->next;                             The head pointer points to the second node} free (p);
    Releases the first node, printf ("--the head node is deleted \ n");
list->size--;

} void Show_tail (plist list)/To test that the tail pointer changes correctly, display the last node {printf ("--the tail node of the list is:"%d \ n ", List->tail->data);}
    void Sortlist (plist list)//unordered list is sorted from small to large {if (2>list->size) return;
    Pnode p = list->head->next;
    Pnode q = p->next;  for (int i = 0;i<list->size-1;i++,p = List->head->next,q = P->next)//n Number comparison n-1 trip {for (int J        = 0;j<list->size-1-i;j++,p=q,q=q->next)//I trip compare n-i times {if (P->data > Q->data)
                If the previous number is greater than the back, swap {p->data = P->data + q->data;
                Q->data = p->data-q->data; P->data = P->data-q->dAta  }} void Insert_val (plist list,elemtype x)//list in order, insert {Pnode p = list->head->next,q =
    list->head;
        while (List->head!= p)//find the position that can be inserted, it will be between P and q {if (x<p->data) break;
        Q = p;
    p = p->next; } pnode s = (pnode) malloc (sizeof (Node));
    Initializes a new node s->data = x;              Q->next = s;
    Insert the new node into the list (insert between p,q) S->next = p;
        if (list->head = = p)//If the value of the new node is the largest, that is, the trailing interpolation, you need to change the end pointer and the trailing node's next point {List->tail = s;
    list->tail->next=list->head;
} list->size++;
    Pnode Find (plist list,elemtype x)//returns the address {Pnode p = list->head; for the previous one of the element to look up while (List->tail!= p && list->head!= p->next && x!= p->next->data) {p = p-&                   Gt;next;
  Loop ends, p points to the previous element of X} if (list->head = = p->next)//if P points to the last element, the description is not found  {printf ("-not found.)
        \ n ");
    return NULL;
} return p;
    } void Del_val (plist list,elemtype x)//delete specified value x {if (0 = = list->size) return;
    Pnode p = Find (list,x);
    Pnode q = NULL;               if (NULL!= p) {q = p->next;
            Q Point to the node to be deleted if (q = = List->tail)//If you delete the last node, change the tail pointer {p->next = list->head;
        List->tail = p;
        else {P->next = q->next;                  Free (q);
        Release the node to be deleted list->size--;
    printf ("--%d deleted!\n", x);
} return;
    } void Modify (plist list,elemtype x1,elemtype x2)//Change the original x1 to x2 {pnode p = find (LIST,X1);
    if (NULL!= p) p->next->data = x2;
else return;
    void Clear (plist list)//delete all nodes of the linked list, but do not delete the header node {pnode p = list->head->next;
    Pnode q = p;         while (List->head!= p) {p = p->next; P in turnAfter moving, the following Q releases the node free (q) in turn;
    Q = p;   } List->tail = list->head;
    Modify tail pointer and list length List->head->next = list->head;
    list->size = 0; printf ("--the list has been emptied.
\ n ");
    } void Destroy (Plist list)//Destroy the linked list, including all nodes and header node {clear (list);
    Free (list->head);
    List->head = NULL;
    List->tail = NULL; printf ("--the list has been destroyed.
\ n "); } pnode prev (plist list,pnode p)//return to the previous node that P points to {if (P!= list->head) {Pnode q = LIST-&GT;HEAD-&G
        T;next;
        while (q!= list->head && q->next!= P)//move backwards, knowing the front node of the tail pointer q=q->next;
    if (Q->next = = p) return q;
return NULL;       } void Reserve (plist list)//Inverse list {pnode s = (pnode) malloc (sizeof (Node));
    Establish a node S->next = list->tail;
    Pnode p = list->tail; while (List->tail!= list->head->next)///Connect the tail node of the original list to the first node, then to the new node {list->tail = prev (list,li
        St->tail); list->tail-&Gt;next = list->head;
        P->next = list->tail;
    p=p->next;              } P->next = S;         P points to the first node, the last node of the new list, the next point of the tail pointer to the head node S, and the linked list ends free (list->head);           Releasing the original head node List->head = s;

Turn s to new head pointer} int Length (plist list)//Find the length of the list {return list->size;}  
    Elemtype Next (plist list,elemtype x)//return X's successor {Pnode p = find (list,x);
    if (NULL = p) return-1;     
    if (P->next = = list->tail)//Because it is a single loop list, the next element of the tail node is the first node {return list->head->next->data;
    } p=p->next;
Return p->next->data;
    } elemtype Prio (plist list,elemtype x)//returns the precursor of x pnode p = find (list,x); if (NULL!= p) {if (p = = List->head | | | p = = list->tail) {return List->tail->da
        Ta
    Return p->data;
} return-1; }

Main.cpp
Test function

#include "CList.h"

int main ()
{
    List mylist;
    Initlist (&mylist);
    Elemtype item = 0;
    int pos = 0;
    int chose = 1;
    Pnode p = NULL;
    while (chose)
    {
        menu ();
        printf ("Give the serial number of the desired action: \ n");
        scanf ("%d", &chose);
        Switch (chose)
        {case
        0:
            destroy (&mylist);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.