Data - Lesson 10 - circular list

Lesson 10 - circular list

 

l single list of limitations

(1) may be used to represent any single chain linear relationship.

(2) Some of the linear relationship is cyclic, i.e. without the tail element, i.e. without the tail element.

 

1. Definition of the circular list

The next pointer of the last data element in a single linked list pointer to the first element.

 

2. circular list has a single list of all operations

(1) to create lists.

(2) the destruction of the list.

(3) obtaining the length of the list.

(4) Clear list.

(5) obtaining a first operation element pos.

(6) inserting the element into the position pos.

(7) Delete position pos.

 

Examples - the single linked circular list rewrite

CircleList.h

#ifndef _CIRCLELIST_H_

#define _CIRCLELIST_H_

 

typedef void CircleList;

typedef struct _tag_CircleListNode CircleListNode;

struct _tag_CircleListNode

{

    CircleListNode* next;

};

 

CircleList* CircleList_Create();

 

void CircleList_Destroy(CircleList* list);

 

void CircleList_Clear(CircleList* list);

 

int CircleList_Length(CircleList* list);

 

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);

 

CircleListNode* CircleList_Get(CircleList* list, int pos);

 

CircleListNode* CircleList_Delete(CircleList* list, int pos);

 

CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);

 

CircleListNode* CircleList_Reset(CircleList* list);

 

CircleListNode* CircleList_Current(CircleList* list);

 

CircleListNode* CircleList_Next(CircleList* list);

 

#endif

 

 

CircleList.h

#include <stdio.h>

#include <malloc.h>

#include "CircleList.h"

 

typedef struct _tag_CircleList

{

    CircleListNode header;

    CircleListNode* slider;

    int length;

} TCircleList;

 

CircleList* CircleList_Create() // O(1)

{

    TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList));

   

    if( ret != NULL )

    {

        ret->length = 0;

        ret->header.next = NULL;

        direction> slider = NULL;

    }

   

    Return the right;

}

 

void CircleList_Destroy(CircleList* list) // O(1)

{

    free(list);

}

 

void CircleList_Clear(CircleList* list) // O(1)

{

    TCircleList* sList = (TCircleList*)list;

   

    if( sList != NULL )

    {

        sList->length = 0;

        sList->header.next = NULL;

        sList->slider = NULL;

    }

}

 

int CircleList_Length(CircleList* list) // O(1)

{

    TCircleList* sList = (TCircleList*)list;

    int ret = -1;

   

    if( sList != NULL )

    {

        ret = sList->length;

    }

   

    Return the right;

}

 

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) // O(n)

{

    TCircleList* sList = (TCircleList*)list;

    int ret = (sList != NULL) && (pos >= 0) && (node != NULL);

    int i = 0;

   

    if( ret )

    {

        CircleListNode* current = (CircleListNode*)sList;

       

        for(i=0; (i<pos) && (current->next != NULL); i++)

        {

            current = current->next;

        }

       

        node->next = current->next;

        current->next = node;

       

        if( sList->length == 0 )

        {

            sList->slider = node;

            node->next = node;

        }

       

        sList->length++;

    }

   

    Return the right;

}

 

CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

    int i = 0;

   

    if( (sList != NULL) && (pos >= 0) )

    {

        CircleListNode* current = (CircleListNode*)sList;

       

        for(i=0; i<pos; i++)

        {

            current = current->next;

        }

       

        ret = current->next;

    }

   

    Return the right;

}

 

CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

    int i = 0;

   

    if( (sList != NULL) && (pos >= 0) )

    {

        CircleListNode* current = (CircleListNode*)sList;

        CircleListNode* first = sList->header.next;

        CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - 1);

       

        for(i=0; i<pos; i++)

        {

            current = current->next;

        }

       

        ret = current->next;

        current->next = ret->next;

       

        sList->length--;

       

        if( first == ret )

        {

            sList->header.next = ret->next;

            last->next = ret->next;

        }

       

        if (sList-> == right slider)

        {

            sList->slider = ret->next;

        }

       

        if( sList->length == 0 )

        {

            sList->header.next = NULL;

            sList->slider = NULL;

        }

    }

   

    Return the right;

}

 

CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) // O(n)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

    int i = 0;

   

    if( sList != NULL )

    {

        CircleListNode* current = (CircleListNode*)sList;

       

        for(i=0; i<sList->length; i++)

        {

            if( current->next == node )

            {

                ret = current->next;

                break;

            }

           

            current = current->next;

        }

        

        if( ret != NULL )

        {

            CircleList_Delete(sList, i);

        }

    }

   

    Return the right;

}

 

CircleListNode* CircleList_Reset(CircleList* list) // O(1)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

   

    if( sList != NULL )

    {

        sList->slider = sList->header.next;

        K = sList-> slider;

    }

   

    Return the right;

}

 

CircleListNode* CircleList_Current(CircleList* list) // O(1)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

   

    if( sList != NULL )

    {

        K = sList-> slider;

    }

   

    Return the right;

}

 

CircleListNode* CircleList_Next(CircleList* list) // O(1)

{

    TCircleList* sList = (TCircleList*)list;

    CircleListNode* ret = NULL;

   

    if( (sList != NULL) && (sList->slider != NULL) )

    {

        K = sList-> slider;

        sList->slider = ret->next;

    }

   

    Return the right;

}

 

 

main.c

#include <stdio.h>

#include <stdlib.h>

#include "CircleList.h"

 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

 

struct Value

{

    CircleListNode header;

    int v;

};

 

int main(int argc, char *argv[])

{

    int i = 0;

    CircleList* list = CircleList_Create();

   

    struct Value v1;

    struct Value v2;

    struct Value v3;

    struct Value v4;

    struct Value v5;

    struct Value v6;

    struct Value v7;

    struct Value v8;

   

    v1.v = 1;

    v2.v = 2;

    v3.v = 3;

    v4.v = 4;

    v5.v = 5;

    v6.v = 6;

    v7.v = 7;

    v8.v = 8;

   

    CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));

   

    CircleList_Insert(list, (CircleListNode*)&v5, 5);

    CircleList_Delete(list, 0);

   

    for(i=0; i<2*CircleList_Length(list); i++)

    {

        struct Value* pv = (struct Value*)CircleList_Get(list, i);

       

        printf("%d\n", pv->v);

    }

   

    printf("\n");

   

    while( CircleList_Length(list) > 0 )

    {

        struct Value* pv = (struct Value*)CircleList_Delete(list, 0);

       

        printf("%d\n", pv->v);

    }

   

    printf("\n");

   

    CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));

    CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));

   

    for(i=0; i<CircleList_Length(list); i++)

    {

        struct Value* pv = (struct Value*)CircleList_Next(list);

       

        printf("%d\n", pv->v);

    }

   

    printf("\n");

   

    CircleList_Reset(list);

   

    while( CircleList_Length(list) > 0 )

    {

        struct Value* pv = NULL;

       

        for(i=1; i<3; i++)

        {

            CircleList_Next(list);

        }

       

        pv = (struct Value*)CircleList_Current(list);

       

        printf("%d\n", pv->v);

       

        CircleList_DeleteNode(list, (CircleListNode*)pv);

    }

   

    CircleList_Destroy(list);

   

         return 0;

}

 

3. The definition of the cursor

In the circular list you can define a "current" pointer that is generally referred to as a cursor, can traverse all the elements in the list by the cursor.

 

4. A list of the new operating cycle

(1) Get current point of the cursor data elements.

(2) reset the cursor points to the first data element of the list.

(3) Move the cursor to point to the next data element of the list.

(4) directly specifying a data element to delete the list.

CircleListNode* CircleList_Current(CircleList* list);

CircleListNode* CircleList_Reset(CircleList* list);

CircleListNode* CircleList_Next(CircleList* list);

CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);

 

 

 

 

Joseph problems

n individuals in a circle, first section, first from a start of the first individual one one clockwise packet number, the m individual report, make the column. And then from the next person clockwise from number 1 newspaper, report the m individual, then make it out of the line, ..., and so on, find the column order.

 

 

summary

l circular list just a single list on the basis of a strengthened done.

l circular list can completely replace the use of a single list.

l Next and Current circular list traversal operations efficiently.

All the elements in the list l.

Guess you like

Origin www.cnblogs.com/free-1122/p/11322743.html