自学写的一个顺序表

来源:互联网 发布:酒店网络营销策划方案 编辑:程序博客网 时间:2024/04/28 21:54
#include <iostream>#include <malloc.h>#include <stdlib.h>using namespace std;typedef int  LElemType;const int LISTSIZE = 8;const int LISTINCREMENT = 5;typedef struct {LElemType *elem;int length;int listsize;} SqList;void InitList_Sq(SqList *L){(*L).elem = (LElemType *)malloc(LISTSIZE * sizeof(LElemType));if(!(*L).elem){cout << "动态内存分配失败!";exit(-1);}(*L).length = 0;(*L).listsize = LISTSIZE;cout << "动态内存分配成功!\n";}/*在该线性表末尾追加元素*/void List_Append(SqList *L){int val;if((*L).length >= (*L).listsize){cout << "线性表已满!追加元素失败!\n";exit(-1);}cout << "请输入要追加的元素:\t";cin >> val;while(cin && (*L).length < (*L).listsize){LElemType *p;        p = &(*L).elem[(*L).length];        *p = val;        (*L).length++;        cout << "第" << (*L).length  << "个元素追加成功!\n";cout << "请输入要追加的元素:\t";cin >> val;}cout << "元素追加结束\n";}/*显示该线性表中的元素*/void Show_List(SqList *L){for(int i=0; i<(*L).length; i++)cout << (*L).elem[i] << endl;}/*判断顺序表是否为空*/bool ListLength_Sq(SqList *L){if((*L).length == 0)return true;elsereturn false;}/*用e接收顺序表第i个元素*/void GetElem_Sq(SqList *L,int i, LElemType &e){if(i<1 || i>(*L).length){cout << "所取元素不存在\n";exit(-1);}e = (*L).elem[i-1];}/*返回顺序表中首个与e满足compare关系的元素的位序*/int Station_Sq(SqList *L, int e){int i = 0;while (i<(*L).length && (*L).elem[i] != e)i++;if (i >= (*L).length)return 0;elsereturn i+1;}/*将顺序表翻转*/void Reverse_Sq(SqList *L){int i = 0;int j = (*L).length-1;for(i,j; i<j ; i++, j--){int temp;temp = (*L).elem[i];(*L).elem[i] = (*L).elem[j];(*L).elem[j] = temp;}}/*对线性表按非递减顺序排列*/void List_sort(SqList *L){int i,j;for(i=0; i < (*L).length-1;i++){for(j = i+1; j < (*L).length; j++){int temp;if((*L).elem[i] > (*L).elem[j]){temp = (*L).elem[i];(*L).elem[i] = (*L).elem[j];(*L).elem[j] = temp;}}}}/*用pre_e接收cur_e的前驱*/void PreElem_Sq(SqList *L, LElemType cur_e, LElemType &pre_e){int i = 1;if((*L).elem[0] != cur_e)   //第一个结点无前驱{while(i <(*L).length && (*L).elem[i] != cur_e )++i;if(i < (*L).length){pre_e = L.elem[i-1];}}}/*用next_e接收cur_e的后继元素*///void NextElem_Sq(SqList *L, LElemType cur_e, LElemType &next_e)//该函数代码可模仿上一个函数的代码/*在顺序表L的第i个位置插入e*/void ListInsert_Sq(SqList *L, int i, LElemType e){if(i<1 || i>(*L).length+1){cout << "选择插入位置有误,插入元素失败!\n";exit(-1);}LElemType *newbase;LElemType *p,*q;newbase = (LElemType *)malloc(((*L).length+LISTINCREMENT)*sizeof(LElemType));if(!newbase){cout << "动态内存分配失败!";exit(-1);}(*L).elem = newbase;(*L).listsize += LISTINCREMENT;q = &(*L).elem[i-1];for (p = &(*L).elem[(*L).length-1]; p >= q; --p) //注意这里是元素地址的变化*(p+1) = *p;*q = e;(*L).length++;cout << "在顺序表第" << i << "个位置插入元素" << e << "成功!\n";}int main(){int i;SqList L;LElemType e;InitList_Sq(&L);if(ListLength_Sq(&L))cout <<"该线性表为空!\n";elsecout << "该线性表不为空!\n";List_Append(&L);Show_List(&L);cout <<"输入入您想取出的元素的位置: ";cin >> i;GetElem_Sq(&L,i,e);cout << "您所取出的元素为: " << e << endl;i = Station_Sq(&L,4);cout << "4为顺序表中第" << i << "个元素\n";        Reverse_Sq(&L);cout << "将顺序表翻转" << endl;Show_List(&L);cout << "将该顺序表按递减顺序排列后:\n";List_sort(&L);Show_List(&L);PreElem_Sq(&L,4,e);cout << "4的前驱元素为" << e << endl;ListInsert_Sq(&L,4,22);cout << "插入新的元素之后的顺序表为:\n";Show_List(&L);return 0;}

原创粉丝点击