Linear table of Data Structure (sequence table) with linear Data Structure

Source: Internet
Author: User
Tags table definition

Linear table of Data Structure (sequence table) with linear Data Structure

Sequence Table

1. sequence table definition: The sequence representation of a linear table refers to storing data elements of a linear table in sequence with a set of sequential storage units. Assume that each element of a linear table occupies 1

Storage unit, and the storage address of the first unit as the storage location of the data element. The storage location of the I + 1 data element in a linear table is LOC (ai + 1) And I data.

The storage location of the element LOC (ai) meets the following requirements: LOC (ai + 1) = LOC (ai) + L, in general, the storage location of the I Data Element ai in a linear table is: LOC (ai) = LOC (a1)

+ (I-1) * L, in the formula LOC (ai) is the storage location of the first data element a1 of a linear table, usually called the starting position or base address of a linear table.

2. Data Structure of the sequence table

typedef struct SeqList{    ElemType *base;     size_t    capacity;    size_t    len;}SeqList;

 

3. perform the following operations in the sequence table:

void InitSeqList(SeqList *list);void ShowSeqList(SeqList *list);bool push_back(SeqList *list, ElemType x);bool push_front(SeqList *list, ElemType x);size_t Length(SeqList *list);bool insert_pos(SeqList *list, int pos, ElemType x);bool pop_back(SeqList *list);bool pop_front(SeqList *list);bool insert_val(SeqList *list, ElemType x);bool delete_pos(SeqList *list, int pos);bool delete_val(SeqList *list, ElemType key);int  find_key(SeqList *list, ElemType key);void reverse_list(SeqList *list);void sort_list(SeqList *list);void clear_list(SeqList *list);void destroy_list(SeqList *list);

The preceding operations include (1) initializing a sequence table to construct an empty sequence table. (2) display the sequence table. (3) tail insertion method. (3) header insertion. (4) Length of the sequence table.

(5) Insert a data element into the sequence table by location. (6) Delete elements at the end. (7) Delete the element in the header. (8) insert values into the sequence table. (9) delete by location

Except the elements in the sequence table. (10) Delete the elements in the sequence table by value. (11) Search for elements in the sequence table by value. (12) reverse the order table. (13) convert the elements of the sequence table

Sort. (14) Clear the sequence table. (15) destroy the sequence table.

4. The method declared above is implemented in the header file of SeqList. h as follows:

#ifndef _SEQLIST_H#define _SEQLIST_H

# Include <iostream>
# Include <assert. h>
Using namespace std;

# Define ElemType int

# Define SEQLIST_DEFAULT_SIZE 8

# Define SEQLIST_INC_SIZE 3

Typedef struct SeqList {ElemType * base; size_t capacity; size_t len;} SeqList;
Void InitSeqList (SeqList * list); void ShowSeqList (SeqList * list); bool push_back (SeqList * list, ElemType x); bool push_front (SeqList * list, ElemType x ); size_t Length (SeqList * list); bool insert_pos (SeqList * list, int pos, ElemType x); bool pop_back (SeqList * list); bool pop_front (SeqList * list ); bool insert_val (SeqList * list, ElemType x); bool delete_pos (SeqList * list, int pos); bool delete_val (SeqList * list, ElemType key); int find_key (SeqList * list, elemType key); void reverse_list (SeqList * list); void sort_list (SeqList * list); void clear_list (SeqList * list); void destroy_list (SeqList * list ); bool IsFull (SeqList * list) {return list-> len> = list-> capacity;} bool IsEmpty (SeqList * list) {return list-> len = 0 ;} void Swap (ElemType & a, ElemType & B) {ElemType tmp = a; a = B; B = tmp ;}
// Abnormal security bool Inc (SeqList * list) {size_t new_capacity = list-> capacity + SEQLIST_INC_SIZE; ElemType * new_base = (ElemType *) realloc (list-> base, sizeof (ElemType) * new_capacity); if (new_base = NULL) return false; list-> capacity = new_capacity; list-> base = new_base; return true ;}
Void InitSeqList (SeqList * list) {list-> base = (ElemType *) malloc (sizeof (ElemType) * SEQLIST_DEFAULT_SIZE); assert (list-> base! = NULL); list-> capacity = SEQLIST_DEFAULT_SIZE; list-> len = 0 ;}
Void ShowSeqList (SeqList * list) {for (int I = 0; I <list-> len; ++ I) {cout <list-> base [I] <"" ;}cout <endl;} bool push_back (SeqList * list, ElemType x) {if (list-> len> = list-> capacity &&! Inc (list) // if (! Inc (list) & list-> len> = list-> capacity) {cout <"the space is full," <x <"cannot be inserted at the end. "<endl; return false;} list-> base [list-> len ++] = x; // list-> len ++; return true ;} bool push_front (SeqList * list, ElemType x) {if (list-> len> = list-> capacity) {cout <"the space is full, "<x <" cannot be inserted in the header. "<endl; return false ;}for (int I = list-> len; I> 0; -- I) {list-> base [I] = list-> base [I-1];} list-> base [0] = x; list-> len ++; return true ;} size_t Length (SeqList * list) {return list-> len;} bool insert_pos (SeqList * list, int pos, ElemType x) {if (list-> len> = list-> capacity) {cout <"space is full," <x <"cannot be inserted. "<endl; return false;} if (pos <0 | pos> list-> len) {cout <" the inserted location is invalid. "<endl; return false ;}for (int I = list-> len; I> pos; -- I) {list-> base [I] = list-> base [I-1];} list-> base [pos] = x; list-> len ++; return true ;}
Bool pop_back (SeqList * list) {if (list-> len = 0) {cout <"the sequence table is empty and cannot be deleted. "<endl; return false;} list-> len --; return true;} bool pop_front (SeqList * list) {if (list-> len = 0) {cout <"the sequence table is empty and cannot be deleted. "<endl; return false ;}for (int I = 0; I <list-> len-1; ++ I) {list-> base [I] = list-> base [I + 1];} list-> len --; return true;} bool insert_val (SeqList * list, ElemType x) {if (list-> len> = list-> capacity) {cout <"space is full," <x <"cannot be inserted. "<endl; return false ;}for (int I = 0; I <list-> len; ++ I) {if (x <list-> base [I]) {for (int j = list-> len; j> I; -- j) {list-> base [j] = list-> base [J-1];} break ;}} list-> base [I] = x; list-> len ++; return true;} bool delete_pos (SeqList * list, int pos) {if (list-> len = 0) {cout <"the sequence table is empty and cannot be deleted. "<endl; return false;} if (pos <0 | pos> = list-> len) {cout <" the deleted location is invalid. The element cannot be deleted. "<endl; return false ;}for (int I = pos; I <list-> len-1; ++ I) {list-> base [I] = list-> base [I + 1];} list-> len --; return true;} bool delete_val (SeqList * list, ElemType key) {if (list-> len = 0) {cout <"the sequence table is empty and cannot be deleted. "<endl; return false;} int del_pos = find_key (list, key); if (del_pos =-1) {cout <" data to be deleted: "<key <" does not exist. "<endl; return false;} return delete_pos (list, del_pos);} int find_key (SeqList * list, ElemType key) {for (int I = 0; I <list-> len; ++ I) {if (key = list-> base [I]) return I;} return-1 ;} void reverse_list (SeqList * list) {if (list-> len> 1) {int low = 0; int high = list-> len-1; while (low

5. Call the above Implementation Method in the main function as follows:

# Include <iostream>
# Incldue "SeqList. h"
Using namespace std;
Int main () {SeqList mylist; InitList (& mylist); ElemType item; int pos; int select = 1; while (select) {cout <"*********************************** * ******* "<endl; cout <"* [1] push_back [2] push_front *" <endl; cout <"* [3] show_list [0] quit_system *" <endl; cout <"* [4] pop_back [5] pop_front *" <endl; cout <"* [6] insert_pos [7] insert_val *" <endl; cout <"* [8] delete_pos [9] delete_val *" <endl; cout <"* [10] find_key [11] length *" <endl; cout <"* [12] reverse_list [13] sort *" <endl; cout <"* [14] clear_list *" <endl; cout <"************************************ * ***** "<endl; cout <"select:>"; cin> select; switch (select) {case 1: cout <"Enter the data to be inserted (-1 ends):> "; while (cin> item, item! =-1) {push_back (& mylist, item) ;}break; case 2: cout <"Enter the data to be inserted (-1 ends):> "; while (cin> item, item! =-1) {push_front (& mylist, item);} break; case 3: ShowList (& mylist); break; case 4: pop_back (& mylist); break; case 5: pop_front (& mylist); break; case 6: cout <"Enter the location to insert:>"; cin> pos; cout <"Enter the value to be inserted:>"; cin> item; insert_pos (& mylist, pos, item); break; case 7: cout <"Enter the value to be inserted:>"; cin> item; insert_val (& mylist, item); break; case 8: cout <"Enter the location to delete:>"; cin> pos; delete_pos (& mylist, pos); break; c Ase 9: cout <"Enter the value to be deleted:>"; cin> item; delete_val (& mylist, item); break; case 10: cout <"Enter the value to be searched:>"; cin> item; p = find_key (& mylist, item); if (p = NULL) {cout <"value to be searched:" <item <"does not exist! "<Endl;} break; case 11: cout <" SeqList Length = "<Length (& mylist) <endl; break; case 12: reverse_list (& mylist); break; case 13: sort_list (& mylist); break; case 14: clear_list (& mylist); break;} system ("pause "); system ("cls");} destroy_list (& mylist); return 0 ;}

In the implementation of the above Code, bool Inc (SeqList * list); the implementation process of the method is to allow this sequence table to grow as data is inserted. The length of the sequence table also increases,Then

Try againOr you don't have to worry about the insertion failure when the space of the sequence table is full.

 

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.