c language singly linked list

When we use an array to store data, not a better grasp of memory to achieve continuous wide open with a small, consume too much, so the dynamic application space, to achieve a reasonable use of memory, so the chain store, in actual use It could be particularly important, and we must have a linked list of data fields to store data, but also a pointer to point to the next node in the node, in order to put together all of the nodes. As shown below

 

Each node has a lower data and the pointer points to a node in order to facilitate good operation with a first join node so the first node of a head pointer pointing to the first node in the domain assigned to the last node NULL

Thus, the structure of a single list

typedef int elemType; // Data type Data 
typedef struct ListNode 
{ 
    elemType Data; // data field 
    ListNode * Next; // pointer field 
} ListNode; 
typedef struct 
{ 
    ListNode * head; // points to the first node of the head node
     int cursize; // record the number of nodes 
} List;

To achieve a single list has the following functions

* Buynode ListNode ();       //    purchasing a node 
void Freenode (the p-ListNode *);   //   destroy node 
void InitList (List & mylist);      //   initialize node 
void insert_item (List & mylist, int POS, elemType Val); //   to node insert data 
void push_back (List & mylist, elemType Val); //   End plug 
void push_front (List & mylist, elemType Val); //   end inserted 
void Insert_fill_n (List & mylist, unsigned int size, elemType Val); // insert size data 
void Insert_Ar (List & mylist, elemType * _F, elemType * _L);   //   The array element in the linked list 
ListNode * FindValue (List & mylist, elemType Val);        //   by value lookup returns a node address 
ListNode * FindPreValue (List & mylist, elemType Val);   //   by value lookup returns a node precursor address 
ListNode * FindPos (List & mylist , int pos);                   //    by location lookup returns a node address 
ListNode * FindPrePos (List & mylist, int pos);              //    lookup returns a node address by location precursor 
void Erase (List & mylist, int pos);                              //    delete pos position on value 
void pop_back (List & mylist);                                      //   tail deleted 
voidpop_front (List & mylist);                                  //    head delete 
void the Remove (List & mylist, elemType Val);                 //    by value, delete the first 
void remove_all (List & mylist, elemType Val);            //    by value to delete all 
void ReversList (List & mylist );                                 // list Retrograde 
BOOL MergrList (List & mylist, & helist List, List & sheliet);     //   according to the size of two data merge list
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
typedef int ElemType;
typedef struct ListNode
{
    ElemType data;
    ListNode *next;
}ListNode;
typedef struct
{
    ListNode *head;
    int cursize;
}List;
ListNode * Buynode()
{
    ListNode *s =(ListNode*)malloc(sizeof(ListNode));
    if(NULL == s) exit(1);
    memset(s,0,sizeof(ListNode));
    return s;
}
void Freenode(ListNode *p)
{
    free(p);
}

void InitList(List &mylist)
{
    mylist.head = Buynode(); // head;
    mylist.cursize = 0;
}
void Insert_Item(List &mylist,int pos,ElemType val)
{
    if(pos <= 0 || pos > mylist.cursize +1) return ;
    ListNode *p = mylist.head;
    int i = 1;
    while(i<pos) { p = p->next; ++i;}
    ListNode *s = Buynode();
    s->data = val;
    s->next = p->next;
    p->next = s;
    mylist.cursize+=1;
}
void push_back(List &mylist,ElemType val)
{
    Insert_Item(mylist,mylist.cursize+1,val);
}
void push_front(List &mylist,ElemType val)
{
    Insert_Item(mylist,1,val);
}
void Insert_fill_n(List &mylist,unsigned int size,ElemType val)
{
    while(size--)
    {
        push_back(mylist,val);
    }
}
void Insert_Ar(List &mylist,ElemType *_F,ElemType *_L)
{
    assert(_F != NULL && _L != NULL);
    for(; _F != _L; ++_F)
    {
        push_back(mylist,*_F);
    }
}
ListNode * FindValue(List &mylist,ElemType val)
{
    ListNode * p = mylist.head->next;
    while(p != NULL && p->data != val)
    {
        p = p->next;
    }
    return p;
}
ListNode * FindPreValue(List &mylist,ElemType val)
{
    ListNode *p = mylist.head;
    while(p != NULL && p->next != NULL  && p->next->data != val)
    {
        p = p->next;
    }
    if(p->next == NULL)
    {
        p = NULL;
    }
    return p;
}ListNode * FindPos(List &mylist,int pos)
{
    if(pos < 1 || pos > mylist.cursize) return NULL;
    ListNode * p = mylist.head->next;
    int i = 1;
    while(i<pos)
    {
        p = p->next;
        ++i;
    }
    return p;
}
ListNode * FindPrePos(List &mylist,int pos)
{
    if(pos < 1 || pos > mylist.cursize) return NULL;
    ListNode * p = mylist.head;
    int i = 1;
    while(i<pos)
    {
        p = p->next;
        ++i;
    }
    return p;
}
void Erase(List &mylist,int pos)
{
    ListNode *p = FindPrePos(mylist,pos);
    if(NULL == p) return ;
    ListNode *q = p->next;
    p->next = q->next;
    Freenode(q);
    mylist.cursize -=1;
}
void pop_back(List &mylist)
{
    Erase(mylist,mylist.cursize);
}
void pop_front(List &mylist)
{
    Erase(mylist,1);
}

void remove(List &mylist,ElemType val)
{
    ListNode *p = FindPreValue(mylist,val);
    if(NULL == p) return ;
    ListNode *q = p->next;
    p->next = q->next;
    Freenode(q);
    mylist.cursize -=1;
}
void remove_all(List &mylist,ElemType val)
{
    ListNode *p = mylist.head;
    while(p != NULL && p->next != NULL)
    {
        if(p->next->data == val)
        {
            ListNode *q = p->next;
            p->next = q->next;
            Freenode(q);
            mylist.cursize-=1;
        }
        else
        {
            p = p->next;
        }
    }
}

void ReversList(List &mylist)
{
    if(mylist.cursize < 2) return ;
    ListNode *p = mylist.head->next;
    mylist.head->next = NULL;
    while(p != NULL)
    {
        ListNode *s = p;
        p = p->next;
        s->next = mylist.head->next;
        mylist.head->next = s;
    }
}

 

Guess you like

Origin www.cnblogs.com/lc-bk/p/11576365.html