Data Structure-self-built Algorithm Library-sequence table, self-built Algorithm

Source: Internet
Author: User

Data Structure-self-built Algorithm Library-sequence table, self-built Algorithm

Practice is carried out simultaneously during the course of "Data Structure. It is a fundamental task to implement basic operations under various storage structures of each logical structure. For more information about the learning method, see the recommended method in [Video] section of "0207 program algorithm conversion ".
This article is the first in the algorithm library. It implements various basic operations for the sequential Storage Structure in linear tables.
The algorithm library contains two files:
Header file: list. h, containing the Code for defining the sequence table data structure, macro definition, and Declaration of functions to implement algorithms;
Source File: list. cpp, including the definition of functions that implement various algorithms
  
List. h

# Ifndef LIST_H_INCLUDED # define LIST_H_INCLUDED # include <stdio. h> # include <malloc. h> # define MaxSize 50 typedef int ElemType; typedef struct {ElemType data [MaxSize]; int length;} SqList; void CreateList (SqList * & L, ElemType a [], int n); // create a linear table void InitList (SqList * & L) with an array; // initialize the linear table InitList (L) void DestroyList (SqList * & L ); // destroy the linear table DestroyList (L) bool ListEmpty (SqList * L); // determine whether the table is empty. ListEmpty (L) int ListLength (SqList * L ); // obtain the length of the linear table ListLength (L) void DispList (SqList * L); // output the linear table DispList (L) bool GetElem (SqList * L, int I, elemType & e); // obtain the value of a Data Element GetElem (L, I, e) int LocateElem (SqList * L, ElemType e ); // search for LocateElem (L, e) bool ListInsert (SqList * & L, int I, ElemType e) by element value; // Insert the data element ListInsert (L, I, e) bool ListDelete (SqList * & L, int I, ElemType & e); // Delete the data element ListDelete (L, I, e) # endif // LIST_H_INCLUDED # endif

List. cpp

# Include <stdio. h> # include <malloc. h> # include "list. h "// use an array to create a linear table void CreateList (SqList * & L, ElemType a [], int n) {int I; L = (SqList *) malloc (sizeof (SqList); for (I = 0; I <n; I ++) L-> data [I] = a [I]; l-> length = n;} // initialize the linear table InitList (L) void InitList (SqList * & L) // reference pointer {L = (SqList *) malloc (sizeof (SqList); // allocate space for storing the linear table L-> length = 0;} // destroy the linear table DestroyList (L) void DestroyList (SqList * & L) {free (L);} // determines whether the ListEmpty table is empty. (L) bool ListEmpty (SqList * L) {return (L-> length = 0);} // evaluate the length of a linear table ListLength (L) int ListLength (SqList * L) {return (L-> length) ;}// output linear table DispList (L) void DispList (SqList * L) {int I; if (ListEmpty (L) return; for (I = 0; I <L-> length; I ++) printf ("% d", L-> data [I]); printf ("\ n");} // evaluate the value of a Data Element GetElem (L, I, e) bool GetElem (SqList * L, int I, ElemType & e) {if (I <1 | I> L-> length) return false; e = L-> data [I-1]; return true ;}/ /Search for LocateElem (L, e) int LocateElem (SqList * L, ElemType e) {int I = 0; while (I <L-> length & L-> data [I]! = E) I ++; if (I >= L-> length) return 0; else return I + 1;} // Insert the data element ListInsert (L, I, e) bool ListInsert (SqList * & L, int I, ElemType e) {int j; if (I <1 | I> L-> length + 1) return false; // If the parameter is incorrect, false I -- is returned. // the sequence table logical sequence number is converted to the physical sequence number for (j = L-> length; j> I; j --) // set data [I .. n] An element moves behind a position L-> data [j] = L-> data [J-1]; L-> data [I] = e; // insert element e L-> length ++; // return true if the length of the sequence table is increased by 1; // return true if the insertion is successful} // Delete the data element ListDelete (L, I, e) bool ListDelete (SqList * & L, int I, ElemType & e) {int j; if (I <1 | I> L-> length) // return false when the parameter is incorrect; I --; // convert the sequence table logical sequence number to the physical sequence number e = L-> data [I]; for (j = I; j <L-> length-1; j ++) // convert data [I .. n-1] Moving element forward L-> data [j] = L-> data [j + 1]; L-> length --; // returns true if the length of the sequence table is reduced by 1; // returns true If deletion is successful}

  
You can define the code in another file and use the results in these infrastructures.
For example, design mian. cpp for testing:
Main. cpp

#include "list.h"int main(){    SqList *sq;    ElemType x[6]= {5,8,7,2,4,9};    CreateList(sq, x, 6);    DispList(sq);    return 0;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.