Data - Lesson 8 - Storage Structure (not) the linear form

Storage Structure linear form - Lesson 8

 

 

The biggest problem is the order of the table insertion and deletion of elements need to move a lot! How to solve?

Student A: between the linear position vacated table data element, for subsequent use insert.

Student B: Do not do that! No matter how many intermediate space are likely to run out!

Student A: Well it is not no solution!

Student B: I think so that each element knows his next element on the line, how empty plug which.

 

1. Storage Structure

In order to express a logical relationship between each element with its immediate successor data elements, each storage element in addition to the information itself, but also need to store information indicating its immediate successor.

 

l chain store logic structure

n-node link chain into a linear chain structure is called a table, when each node contains a pointer field only, is called a single linked list.

 

l header node

The first node in the list, contains a pointer to the first list of data elements and some of the information itself.

 

Data node l

Node data representative of the list of elements, and comprising information pointer points to the next data element of a data element.

 

End node l

The last node in the linked list of data that the next element pointer is null, indicating no successor.

 

2. C language description

In the C language may be used to define the structure pointer field in the linked list.

The list header node may be implemented with the structure.

typedef struct _tag_LinkList

{

LinkListNode header;

int length;

} TLinkList; // head node is defined

typedef struct _tag_LinkListNode LinkListNode

struct _tag_LinkListNode

{

LinkListNode* next;

int length;

} TLinkList; // node pointer field defines the

struct Value

{

LinkListNode header;

int v;

}; // instance data element definitions

 

 

3. Get the first element operating pos

(1) determining a linear table is legitimate.

(2) determine whether the legal position.

(3) after the start of the header by the next pointer moves pos pass, the current element, i.e., the next pointer pointing to the element to be retrieved.

LinkListNode* current = (LinklistNode*)List;

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

{

current = crrrent->next;

}

ret = current->next;

 

 

4. Insert the element to the position pos algorithm

(1) determining a linear table is legitimate.

(2) determining the insertion position is legitimate.

(3) after the start of the header by the next pointer moves pos pass, the current element, i.e., the next pointer pointing to a position to be inserted.

(4) inserting a new element.

(5) plus the length of a linear form.

LinkListNode* current = (LinklistNode*)List;

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

{

current = current->next;

}

node->next = current->next;

current->next = node;

sList->length++;

 

5. 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.

TLinkList* sList = (TLinkList*)list;

Link List Node * right = NULL;

int i = 0;

if((sList != NULL) && (0 <=pos) && (pos < sList->length))

{

LinkListNode* current = (LinkListNode*)list;

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

{

current = current->next;

}

ret = current->next;

current->next = ret->next;

sList->length--;

}

 

6. Hands program

LinkList.h

#ifndef _LINKLIST_H_

#define _LINKLIST_H_

 

typedef void LinkList;

typedef struct _tag_LinkListNode LinkListNode;

struct _tag_LinkListNode

{

    LinkListNode* next;

};

 

LinkList* LinkList_Create();

 

void LinkList_Destroy(LinkList* list);

 

void LinkList_Clear(LinkList* list);

 

int LinkList_Length(LinkList* list);

 

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

 

LinkListNode* LinkList_Get(LinkList* list, int pos);

 

LinkListNode* LinkList_Delete(LinkList* list, int pos);

 

#endif

 

LinkList.c

#include <stdio.h>

#include <malloc.h>

#include "LinkList.h"

 

typedef struct _tag_LinkList

{

    LinkListNode header;

    int length;

} TLinkList;

 

LinkList* LinkList_Create() // O(1)

{

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

   

    if( ret != NULL )

    {

        ret->length = 0;

        ret->header.next = NULL;

    }

   

    Return the right;

}

 

void LinkList_Destroy(LinkList* list) // O(1)

{

    free(list);

}

 

void LinkList_Clear(LinkList* list) // O(1)

{

    TLinkList* sList = (TLinkList*)list;

   

    if( sList != NULL )

    {

        sList->length = 0;

        sList->header.next = NULL;

    }

}

 

int LinkList_Length(LinkList* list) // O(1)

{

    TLinkList* sList = (TLinkList*)list;

    int ret = -1;

   

    if( sList != NULL )

    {

        ret = sList->length;

    }

   

    Return the right;

}

 

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)

{

    TLinkList* sList = (TLinkList*)list;

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

    int i = 0;

   

    if( ret )

    {

        LinkListNode* current = (LinkListNode*)sList;

       

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

        {

            current = current->next;

        }

       

        node->next = current->next;

        current->next = node;

       

        sList->length++;

    }

   

    Return the right;

}

 

LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)

{

    TLinkList* sList = (TLinkList*)list;

    Link List Node * right = NULL;

    int i = 0;

   

    if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )

    {

        LinkListNode* current = (LinkListNode*)sList;

       

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

        {

            current = current->next;

        }

       

        ret = current->next;

    }

   

    Return the right;

}

 

LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)

{

    TLinkList* sList = (TLinkList*)list;

    Link List Node * right = NULL;

    int i = 0;

   

    if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )

    {

        LinkListNode* current = (LinkListNode*)sList;

       

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

        {

            current = current->next;

        }

       

        ret = current->next;

        current->next = ret->next;

       

        sList->length--;

    }

   

    Return the right;

}

 

main.c

#include <stdio.h>

#include <stdlib.h>

#include "LinkList.h"

 

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

 

struct Value

{

    LinkListNode header;

    int v;

};

 

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

{

    int i = 0;

    LinkList* list = LinkList_Create();

   

    struct Value v1;

    struct Value v2;

    struct Value v3;

    struct Value v4;

    struct Value v5;

   

    v1.v = 1;

    v2.v = 2;

    v3.v = 3;

    v4.v = 4;

    v5.v = 5;

   

    LinkList_Insert(list, (LinkListNode*)&v1, LinkList_Length(list));

    LinkList_Insert(list, (LinkListNode*)&v2, LinkList_Length(list));

    LinkList_Insert(list, (LinkListNode*)&v3, LinkList_Length(list));

    LinkList_Insert(list, (LinkListNode*)&v4, LinkList_Length(list));

    LinkList_Insert(list, (LinkListNode*)&v5, LinkList_Length(list));

   

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

    {

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

       

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

    }

   

    while( LinkList_Length(list) > 0 )

    {

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

       

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

    }

   

    LinkList_Destroy(list);

   

    return 0;

}

 

 

advantage:

(1) do not need to customize the list of one-time capacity.

(2) insertions and deletions without moving data elements.

Disadvantages:

(3) the data element must be stored position information of the subsequent element.

(4) obtaining the specified data element before the element operation requires sequential access.

 

After-school exercise

1. The table in order to realize why the saved address specific data elements? The saved address is stored in unsigned int, rather than specific pointer type? Void * Why is not it?

 

2. The following statements about the chain storage structure is correct

A. Storage Structure not sequential access structure.

Shall be adjacent on the adjacent node B. physical logic.

C. iii elements may be determined on the i th element directly by calculation.

D. easy insertion and deletion operations, without having to move the other nodes.

 

3. In the implementation and application of a single linked list, there is a non-single header node list. According to the operation we have listed, programming

No single linked list to achieve this header node, and the way which is better and Comparative header implementations achieved without header.

 

4. Add a reversal operation for us to achieve a single list and the order table. The order of the list so that the operation is reversed, i.e. first, i.e., a first element becomes the last element, the second element goes down

Number of second element. Such as: abcd reverse the result of the result is reversed

dcba.

Required to achieve the time complexity O (n).

SeqList_Reverse(SeqList* list);

LinkList_Reverse(LinkList* list);

Guess you like

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