Data structure and algorithm _1: Linear table sequential storage structure common operations

Source: Internet
Author: User
sequential storage structure of linear tables
definition

The data object collection for the linear table is {A1,A2,.... an}, and each element is of type datatype. wherein, except for the first element A1, each element is known and has only one direct precursor element, except the last element an, each element is known and has only one immediate successor element. The relationship between data elements is one-to-one.


Advantages and disadvantages of the sequential storage structure of linear tables: advantages: There is no need to add additional storage space for the logical relationship between elements in the table; You can quickly access elements O (1) in any location in the table: Insert and delete operations need to move a large number of elements O (n) When the length of the linear table changes greatly, it is difficult to determine the storage space capacity, resulting in the storage space of the "Fragment" sample program as follows (adapted from the "Liar Data Structure"):
Code Implementation

#include <iostream> using namespace std;

#define MAXSIZE typedef int ELEMTYPE;
	typedef struct {elemtype data[maxsize];
int length;

} sqlist;
	/* Initialization order Linear table/bool Initlist (SqList *ptr) {for (int i = 0; i < MAXSIZE; i++) ptr->data[i] = 0;
	ptr->length = 0;
return true;
	BOOL Listempty (SqList Sq) {if (sq.length = 0) return true;
else return false;
	BOOL Clearlist (SqList *ptr) {for (int i = 0; i < ptr->length; i++) ptr->data[i] = 0;
	ptr->length = 0;
return true; }/* Use PTR to return the value of the POS data element in Sq, note that the POS is the position, the 1th position array is starting from 0/bool Getelem (sqlist Sq, int pos, elemtype *ptr) {if sq.length = 0 || POS < 1 | |
	POS > Sq.length) return false;
	*ptr = sq.data[pos-1];
return true; }/* Returns the bit order of the 1th and Elem data element that satisfies the relationship in Sq, and if such data element does not exist, the return value is 0/int Locate (sqlist Sq, Elemtype Elem) {for (int i = 0; i < Sq.leng Th
	i++) {if (sq.data[i] = = Elem) return i + 1;
return 0; }/* Insert new data element elem,l length before pos position in sq add 1*/bool Listinsert (sqlist *ptr, int pOS, Elemtype Elem) {if (ptr->length = = MAXSIZE)/* Sequential linear table is full */return false;
	if (pos < 1 | | | pos > ptr->length + 1) return false;
			if (pos <= ptr->length) {///* The data element after the insertion position is moved backward by one */for (int i = ptr->length-1; i >= pos-1; i--) {
		Ptr->data[i + 1] = ptr->data[i]; }} Ptr->data[pos-1] = Elem;
	/* Inserts the new element into the */ptr->length++;
return true; /* Delete ps pos data element and return its value with PE, PS length minus 1*/bool Listdelete (sqlist *ps, int pos, elemtype *pe) {if (pos < 1 | | pos > ps-
	>length) return false;
	*pe = ps->data[pos-1];

	/* will delete position successor element forward/for (int i = pos; i < ps->length; i++) ps->data[i-1] = ps->data[i];

	ps->length--;

return true;

int Listlength (SqList Sq) {return sq.length;}
	/* Inserts all elements in the linear table PB but not in the PA into the PA/void Unionlist (SqList *pa, sqlist *pb) {int Lena = pa->length;
	int LenB = pb->length;

	int item; for (int i = 0; i < LenB i++) {if (Getelem (*PB, i + 1, &item)) {if (Locate(*PA, item) = = 0 Listinsert (PA, ++lena, item);
	int main (void) {sqlist Sq;
	Initlist (&AMP;SQ);

	for (int i = 1; i < 5; i++) Listinsert (&sq, I, I); if (!
		Listempty (Sq)) {cout << "sq:" << Endl;
	for (int i = 0; i < listlength (Sq); i++) cout << sq.data[i] << ";

	} cout << Endl;
	int pos = Locate (Sq, 2);
		if (pos!= 0) {int result;
		Listdelete (&AMP;SQ, POS, &result);
	cout << "Delete:" << result << Endl; } if (!
		Listempty (Sq)) {cout << "sq:" << Endl;
	for (int i = 0; i < listlength (Sq); i++) cout << sq.data[i] << ";

	} cout << Endl;
	SqList Sq2;
	Initlist (&AMP;SQ2);
	for (int i = 1; i < 4; i++) Listinsert (&AMP;SQ2, I, 6);
	Listinsert (&AMP;SQ2, 4, 7); if (!
		Listempty (SQ2)) {cout << "Sq2:" << Endl;
	for (int i = 0; i < listlength (SQ2); i++) cout << sq2.data[i] << ";

	} cout << Endl; Unionlist (&AMP;SQ, &AMP;SQ2); if (!
		Listempty (Sq)) {cout << "sq:" << Endl;
	for (int i = 0; i < listlength (Sq); i++) cout << sq.data[i] << ";

	} cout << Endl;
return 0; }
Results

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.