Data structure (1), data structure

Source: Internet
Author: User

Data structure (1), data structure

1. Sequential Storage

// List. h # ifndef LIST_H _ # define MAXSIZE 100 typedef struct _ Poly {int a; int n; bool operator = (_ Poly e) {if (a = e. a & n = e. n) return true; else return false;} Poly; # define ElemType Polytypedef struct _ Node {ElemType data [MAXSIZE]; int last;} Node; class List {private: node * data; public: List ();~ List (); void Insert (ElemType e, int index); void Remove (int index); int Find (ElemType e); ElemType FindKth (int k ); int Length () ;};# endif // List. cpp # include "List. h "# include <iostream> List: List () {data = new Node (); data-> last =-1;} List ::~ List () {delete data;} void List: Insert (ElemType e, int index) {if (data-> last = MAXSIZE-1) {std :: cout <"the table is full" <std: endl; return;} if (index <0 | index> (data-> last + 1) {std :: cout <"invalid location" <std: endl; return ;}for (int I = data-> last; I >= index; I --) {data-> data [I + 1] = data-> data [I];} data-> data [index] = e; data-> last ++ ;} void List: Remove (int index) {if (index <0 | index> data-> last) {std :: cout <"delete location discord" <std: endl; return;} for (int I = index; I <data-> last; I ++) {data-> data [I] = data-> data [I + 1];} data-> last --;} int List: Find (ElemType e) {for (int I = 0; I <data-> last; I ++) {if (data-> data [I] = e) return I ;} return-1;} ElemType List: FindKth (int k) {if (k <0 | k> data-> last) {std :: cout <"invalid search location" <std: endl;} return data-> data [k];} int List: Length () {return (data-> last + 1 );}

 

2. chained Storage

// PList. h # ifndef PLIST_H _ # define MAXSIZE 100 typedef struct _ Poly {int a; int n; bool operator = (_ Poly e) {if (a = e. a & n = e. n) return true; else return false;} Poly; # define ElemType Polytypedef struct _ PNode {ElemType data; _ PNode * next;} PNode; class PList {private: PNode * head; public: PList ();~ PList (); void Insert (ElemType e, int index); void Remove (int index); int Find (ElemType e); ElemType FindKth (int k ); int Length () ;};# endif // PList. cpp # include "PList. h "# include <iostream> PList: PList () {head = new PNode (); head-> next = NULL;} PList ::~ PList () {delete head;} void PList: Insert (ElemType e, int index) {PNode * p = head; PNode * s = new PNode (); s-> data = e; s-> next = NULL; if (index> Length () std: cout <"Insertion Location Error" <std: endl; int I = 0; while (p & I <index) {p = p-> next; I ++;} s-> next = p-> next; p-> next = s;} void PList: Remove (int index) {PNode * p = head; PNode * s = new PNode (); if (index> Length () std: cout <"Remove location error" <std: endl; int I = 0; while (p & I <index) {p = p-> next; I ++;} s = p-> next; p-> next = s-> next; delete s;} int PList :: find (ElemType e) {PNode * p = head; int I = 0; while (p) {if (p-> data = e) return I; I ++; p = p-> next;} return-1;} ElemType PList: FindKth (int k) {PNode * p = head; if (k <0 | k> Length () {std: cout <"the search location is invalid" <std: endl;} int I = 0; while (p & I <k) {p = p-> next; I ++;} return p-> next-> data;} int PList: Length () {PNode * p = head; int I = 0; while (p) {I ++; p = p-> next;} return I ;}

3. Test Cases

//main.cpp#include <iostream>#include <stdlib.h>#include "PList.h"using namespace std;int main(){    PList L;    for (int i = 0; i < 5; i++)    {        Poly t = { i, i };        L.Insert(t, i);    };    cout << "last:" << L.Length() << endl;    Poly e = { 2, 2 };    cout << "Find return:" << L.Find(e) << endl;    for (int i = 0; i < 4; i++)    {        cout << L.FindKth(i).a << "X" << L.FindKth(i).n  << " + ";    }    cout << L.FindKth(4).a << "X" << L.FindKth(4).n << endl;    system("pause");    return 0;}

 

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.