What are linear tables?

Source: Internet
Author: User

What are linear tables?

I will take my own test next month. I have been introducing data structures in the past few days. This book contains two parts: the storage structure (table, tree, and graph) and the operations (search and sorting) on data ). Today I want to talk about two storage structures (sequential storage and chained storage) of linear tables ).


Sequential storage, as its name implies, stores the nodes in the table in a group of continuous storage units in computer memory. The joining relationship between data elements in a linear table determines their storage location in the bucket, that is, the storage locations of adjacent nodes in the logical structure are also adjacent. arrays are generally used to represent sequential tables.

Sequential storage is the simplest and most primitive storage method. Because arrays need to be pre-defined, some space is wasted, the I data element is stored in the position where the array subscript is "I-1. Its basic operations are insert, delete, and locate.

Insert the corresponding question type is generally: In the table with the length of N inserted before the M element of the table, the number of elements to be moved to the N-M + 1

Delete the corresponding question type: Delete the M-th element of the table whose table length is N. Then, the elements after M are moved to the left, and the table length is reduced by 1.

Locate the corresponding question type: Generally, it is related to the time complexity. we can imagine that the positioning must be one by one, so a loop is used, when the table length is N, the time complexity is O (N ).


Chain Stores commonly include single-chain tables, cyclic linked lists, and bidirectional cyclic linked lists. A pointer is added to point to the next data element. The tail node pointer is a Null pointer. If the header Pointer Points to the tail pointer, It is a Null single-chain table. The pointer to the last node of the circular linked list points to the first node. A two-way cyclic linked list is added with a pointer prior pointing to the front node.

Note that the insert operation requires a pointer to the source node of the node to be deleted. In a double-loop linked list, you can directly delete it.


When comparing the two methods, we generally compare their time complexity, which is also a frequent problem. During location-based search operations, the sequence table is random and the time complexity is O (1). A single-chain table needs to scan for loops, so it is O (N ). Cycle is used for locating operations, so the time complexity is O (N ). During the insert or delete operation, all operations need to be located, so the time complexity is O (N ).






Basic operations on Linear tables in C Language

# Include "stdio. h"
# Include <malloc. h>

Typedef char ElemType;

Typedef struct LNode
{ElemType data;
Struct LNode * next;
} LinkList;

Void CreatListF (LinkList * & L, ElemType a [], int n) // create a table using the header Insertion Method
{
LinkList * s; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = NULL;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
S-> next = L-> next;
L-> next = s;
}
}

Void CreateListR (LinkList * & L, ElemType a [], int n) // create a table by means of end insertion
{
LinkList * s, * r; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
R = L;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
R-> next = s;
R = s;
}
R-> next = NULL;
}

Void InitList (LinkList * & L) // initialize the linear table
{
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = NULL;
}

Void DestroyList (LinkList * & L) // destroy the linear table
{
LinkList * p = L, * q = p-> next;
While (q! = NULL)
{
Free (p );
P = q;
Q = p-> next;
}
Free (p );
}

Int ListEmpty (LinkList * L) // determines whether the linear table is empty.
{
Return (L-> next = NULL );
}

Int ListLength (LinkList * L) // evaluate the length of a linear table
{
LinkList * p = L; int n = 0;
While (p-> next! = NULL)
{
N ++; p = p-> next;
}
Return (n );
}

Void DispList (LinkList * L) // output linear table
{
LinkList * p = L-> next;
While (p! = NULL)
{
Printf ("% c", p-> data );
P = p-> next;
}
}

Int GetElem (LinkList * L, int I, ElemType & e) // evaluate the value of a data element in a linear table.
{
Int j = 0;
LinkList * p = L;
While (j <I & p! = NULL)
{
J ++; p = p-> next;
}
If (p = NULL)
Retu ...... remaining full text>

Basic operations on Linear tables in C Language

# Include "stdio. h"
# Include <malloc. h>

Typedef char ElemType;

Typedef struct LNode
{ElemType data;
Struct LNode * next;
} LinkList;

Void CreatListF (LinkList * & L, ElemType a [], int n) // create a table using the header Insertion Method
{
LinkList * s; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = NULL;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
S-> next = L-> next;
L-> next = s;
}
}

Void CreateListR (LinkList * & L, ElemType a [], int n) // create a table by means of end insertion
{
LinkList * s, * r; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
R = L;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
R-> next = s;
R = s;
}
R-> next = NULL;
}

Void InitList (LinkList * & L) // initialize the linear table
{
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = NULL;
}

Void DestroyList (LinkList * & L) // destroy the linear table
{
LinkList * p = L, * q = p-> next;
While (q! = NULL)
{
Free (p );
P = q;
Q = p-> next;
}
Free (p );
}

Int ListEmpty (LinkList * L) // determines whether the linear table is empty.
{
Return (L-> next = NULL );
}

Int ListLength (LinkList * L) // evaluate the length of a linear table
{
LinkList * p = L; int n = 0;
While (p-> next! = NULL)
{
N ++; p = p-> next;
}
Return (n );
}

Void DispList (LinkList * L) // output linear table
{
LinkList * p = L-> next;
While (p! = NULL)
{
Printf ("% c", p-> data );
P = p-> next;
}
}

Int GetElem (LinkList * L, int I, ElemType & e) // evaluate the value of a data element in a linear table.
{
Int j = 0;
LinkList * p = L;
While (j <I & p! = NULL)
{
J ++; p = p-> next;
}
If (p = NULL)
Retu ...... remaining full text>

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.