Data - Lesson 9 - Static list

Lesson 9 - Static list

 

The perfect single-chain sequence table to solve the problem!

There are other methods to improve the order of the table do?

Student A: singly linked list is perfect, I think the order of the table can retire.

Student B: I think, why the teacher should teach us the order list it?

Student A: That is not to show a single list of powerful thing!

Student B: It seems that we can completely abandon the order table.

C small cattle were played.

The order table has advantages, also have disadvantages singly linked list!

Student C: Although single-chain together better than the order of the table, but there are drawbacks.

Student B: What are the shortcomings of it, Linux kernel are used single-chain Oh!

Student A: that is, the general large-scale projects can be found in a single list figure!

Student C: I think you will understand the relative disadvantage of a single list, single-chain rely heavily pointer! Data element must contain an additional pointer field! No pointer programming language can not be achieved!

 

One. Improved sequence table

1. Definition of static list

(1) The order of elements in the array table data by two domains: data and next.

(2) data field for storing data.

(3) next to a field element stored in the index in the array.

 

2. Static list is the use of single-chain arrays implemented on the basis of the order on the table!

typedef struct _tag_StaticListNode

{

         unsigned int data;

         int next;  

}TStaticListNode;

Junction structure is defined

 

typedef struct _tag_StaticList

{

         int capacity;

         TStaticListNode header;

         TStaticListNode node [];  

}TStaticList;

Static list structure is defined

two. Static chain operations

1. Obtaining operating element pos

(1) determining whether the linearity becomes valid.

(2) determine whether the legal position.

(3) starting from the next by a header field pos certain times, i.e., the current field elements next element to get the array index.

sList->node[0] = sList->header;

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

{

         current = sList->node[current].next;

}

object = sLit->node[current].next;

 

2. Insert the element into the position pos algorithm

(1) determining a linear table is legitimate.

(2) determining the insertion position is legitimate.

(3) Find an idle position index in the array.

(4) from the header by the next field begins moving pos views, next field, the current element of the current element is to be inserted.

(5) inserting a new element.

(6) plus the length of a linear form.

for(i=0; (i<pos) && (sList->node[current].next != 0); i++)

{

         current = sList->node[current].next;

}

sLisst->node[index].next = sLisst->node[current].next;

sLisst->node[current].next = index;

 

3. Delete the first algorithm pos elements

(1) determining a linear table is legitimate.

(2) determining the insertion position is legitimate.

(3) Get the first pos elements.

(4) the first pos element removed from the list.

(5) the linear form length minus 1.

object = sLisst->node[current].next;

sLisst->node[current].next = sLisst->node[object].next;

 

three. Create reusable static list

StaticList.h

#ifndef _STATICLIST_H_

#define _STATICLIST_H_

 

typedef void StaticList;

typedef void StaticListNode;

 

StaticList* StaticList_Create(int capacity);

 

void StaticList_Destroy(StaticList* list);

 

void StaticList_Clear(StaticList* list);

 

int StaticList_Length(StaticList* list);

 

int StaticList_Capacity(StaticList* list);

 

int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);

 

StaticListNode* StaticList_Get(StaticList* list, int pos);

 

StaticListNode* StaticList_Delete(StaticList* list, int pos);

 

#endif

 

StaticList.c

#include <stdio.h>

#include <malloc.h>

#include "StaticList.h"

 

#define AVAILABLE -1

 

typedef struct _tag_StaticListNode

{

    unsigned int data;

    int next;

} TStaticListNode;

 

typedef struct _tag_StaticList

{

    int capacity;

    TStaticListNode header;

    TStaticListNode node [];

} TStaticList;

 

StaticList* StaticList_Create(int capacity) // O(n)

{

    TStaticList * ret = NULL;

    int i = 0;

   

    if( capacity >= 0 )

    {

        ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity + 1));

    }

   

    if( ret != NULL )

    {

        ret->capacity = capacity;

        direction> header.data = 0;

        ret->header.next = 0;

       

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

        {

            ret->node[i].next = AVAILABLE;

        }

    }

   

    Return the right;

}

 

void StaticList_Destroy(StaticList* list) // O(1)

{

    free(list);

}

 

void StaticList_Clear(StaticList* list) // O(n)

{

    TStaticList* sList = (TStaticList*)list;

    int i = 0;

   

    if( sList != NULL )

    {

        sList->header.data = 0;

        sList->header.next = 0;

       

        for(i=1; i<=sList->capacity; i++)

        {

            sList->node[i].next = AVAILABLE;

        }

    }

}

 

int StaticList_Length(StaticList* list) // O(1)

{

    TStaticList* sList = (TStaticList*)list;

    int ret = -1;

   

    if( sList != NULL )

    {

        K = sList-> header.data;

    }

   

    Return the right;

}

 

int StaticList_Capacity(StaticList* list) // O(1)

{

    TStaticList* sList = (TStaticList*)list;

    int ret = -1;

   

    if( sList != NULL )

    {

        ret = sList->capacity;

    }

   

    Return the right;

}

 

int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)  // O(n)

{

    TStaticList* sList = (TStaticList*)list;

    int ret = (sList! = NULL);

    int current = 0;

    int index = 0;

    int i = 0;

   

    ret = ret && (sList->header.data + 1 <= sList->capacity);

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

   

    if( ret )

    {

        for(i=1; i<=sList->capacity; i++)

        {

            if( sList->node[i].next == AVAILABLE )

            {

                index = i;

                break;

            }

        }

       

        sList->node[index].data = (unsigned int)node;

       

        sList->node[0] = sList->header;

       

        for(i=0; (i<pos) && (sList->node[current].next != 0); i++)

        {

            current = sList->node[current].next;

        }

       

        sList->node[index].next = sList->node[current].next;

        sList->node[current].next = index;

       

        sList->node[0].data++;

       

        sList->header = sList->node[0];

    }

   

    Return the right;

}

 

StaticListNode* StaticList_Get(StaticList* list, int pos)  // O(n)

{

    TStaticList* sList = (TStaticList*)list;

    StaticListNode* ret = NULL;

    int current = 0;

    int object = 0;

    int i = 0;

   

    if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )

    {

        sList->node[0] = sList->header;

       

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

        {

            current = sList->node[current].next;

        }

       

        object = sList->node[current].next;

       

        ret = (StaticListNode*)(sList->node[object].data);

    }

   

    Return the right;

}

 

StaticListNode* StaticList_Delete(StaticList* list, int pos) // O(n)

{

    TStaticList* sList = (TStaticList*)list;

    StaticListNode* ret = NULL;

    int current = 0;

    int object = 0;

    int i = 0;

   

    if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )

    {

        sList->node[0] = sList->header;

       

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

        {

            current = sList->node[current].next;

        }

       

        object = sList->node[current].next;

       

        sList->node[current].next = sList->node[object].next;

       

        sList->node[0].data--;

       

        sList->header = sList->node[0];

       

        sList->node[object].next = AVAILABLE;

       

        ret = (StaticListNode*)(sList->node[object].data);

    }

   

    Return the right;

}

 

main.c

#include <stdio.h>

#include <stdlib.h>

#include "StaticList.h"

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

 

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

{

    StaticList* list = StaticList_Create(10);

   

    int index = 0;

   

    int i = 0;

    int j = 1;

    int k = 2;

    int x = 3;

    int y = 4;

    int z = 5;

   

    StaticList_Insert(list, &i, 0);

    StaticList_Insert(list, &j, 0);

    StaticList_Insert(list, &k, 0);

   

    for(index=0; index<StaticList_Length(list); index++)

    {

        int* p = (int*)StaticList_Get(list, index);

       

        printf("%d\n", *p);

    }

   

    printf("\n");

   

    while( StaticList_Length(list) > 0 )

    {

        int* p = (int*)StaticList_Delete(list, 0);

       

        printf("%d\n", *p);

    }

   

    printf("\n");

   

    StaticList_Insert(list, &x, 0);

    StaticList_Insert(list, &y, 0);

    StaticList_Insert(list, &z, 0);

   

    printf("Capacity: %d Length: %d\n", StaticList_Capacity(list), StaticList_Length(list));

   

    for(index=0; index<StaticList_Length(list); index++)

    {

        int* p = (int*)StaticList_Get(list, index);

       

        printf("%d\n", *p);

    }

   

    StaticList_Destroy(list);

   

         return 0;

}

 

summary

l A static list is actually another way to achieve a single list.

L realize static list of "media" but not a pointer array.

l still do not support pointer list is mainly used programming languages.

L realize static linked list is a simple method of memory management.

 

Question: Why is a static list structure defined in a header members again, without directly using the node [0]?

Guess you like

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