C++模拟实现静态顺序表

来源:互联网 发布:淘宝开店企业交税 编辑:程序博客网 时间:2024/06/05 23:29
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<stdio.h>#include<stdlib.h>#include<assert.h>using namespace std;const int Max = 10;struct SeqListNode{    int data[Max];    size_t _size;};typedef SeqListNode List;void init(List* pHead);//初始化void print(List* pHead);//打印int size(List* pHead);//大小void pushBack(List* pHead,int data);//尾插void popBack(List* pHead);//尾删void pushFront(List* pHead,int data);//头插void popFront(List* pHead);//头删bool Empty(List* pHead);//判空void insert(List* pHead,int pos,int data);//在指定位置插入元素void erase(List* pHead, int pos);//删除指定位置的元素void remove(List* pHead,int data);//删除值为data的元素int Find(List* pHead,int data);//查找值为data的元素
#include"SeqList.h"void init(List* head){    head->_size = 0;    memset(head->data,0,Max*sizeof(int));}void print(List* head){    assert(head);    for (size_t i = 0; i < head->_size; i++)    {        cout << head->data[i]<<"  ";    }    cout << "\n";}int size(List* head){    return head->_size;}void pushBack(List* head, int data){    if (head->_size == Max)        return;    head->data[head->_size] = data;    head->_size++;}void popBack(List* head){    if (head->_size>0)    {        head->_size--;    }}void pushFront(List* head, int data){    if (head->_size == Max)        return;    else    {        for (int i = head->_size; i >=0; i--)//元素依次往后移        {            head->data[i] = head->data[i - 1];        }        head->data[0] = data;        head->_size++;    }}void popFront(List*head){    assert(&head);    if (head->_size == 1)    {        head->_size--;    }    else    {        for (size_t i = 1; i <head->_size; i++)        {            head->data[i - 1] = head->data[i];        }        head->_size--;    }}bool Empty(List* head){    return head->_size == 0;}void insert(List* head, int pos, int data){    if (pos<0 || pos>=Max)        return;    else    {        for (int i =head->_size; i >=pos; i--)        {            head->data[i + 1] = head->data[i];        }        head->data[pos] = data;        head->_size++;    }}void erase(List* head, int pos){    assert(head);    if (pos < 0 || pos >= Max)        return;    else    {        for (size_t i = pos; i < head->_size; i++)        {            head->data[i] = head->data[i + 1];        }        head->_size--;    }}void remove(List* head, int data){    assert(head);    for (size_t i = 0; i < head->_size; i++)    {        if (head->data[i] == data)        {            for (size_t j = i; j < head->_size; j++)            {                head->data[j] = head->data[j + 1];            }            head->_size--;        }    }}int Find(List* head, int data){    assert(head);    for (size_t i = 0; i < head->_size; i++)    {        if (head->data[i] == data)            return i;    }    return 0;}void test(){    List head;    init(&head);    //cout<<Empty(&head)<<endl;    pushBack(&head, 2);    pushBack(&head, 3);    pushBack(&head, 4);    pushBack(&head, 5);    popBack(&head);    pushFront(&head, 1);    pushFront(&head, 5);    popFront(&head);    //print(&head);    insert(&head,2,7);    insert(&head, 1, 8);    //print(&head);    erase(&head,3);    //print(&head);    remove(&head,2);    pushBack(&head, 3);    pushBack(&head,9);    print(&head);    remove(&head, 3);    cout << Find(&head, 9) << endl;    print(&head);    cout << size(&head) << endl;    //cout << Empty(&head) << endl;}int main(){    test();    system("pause");    return 0;}