赞
踩
终极版
- #include <cstdlib>
- #include <cstdio>
- #include <iostream>
- #include <windows.h>
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define INFEASIBLE -1
- #define OVERFLOW -2
- #define LIST_INIT_SIZE 100
- #define LISTINCREMENT 10
- //Status是函数的类型,其值是函数结果状态代码
- typedef int Status;
- typedef int ElemType;
-
- typedef struct
- {
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- using namespace std;
- Status InitList(SqList &L)
- {
- L.elem=(ElemType *) malloc(LIST_INIT_SIZE*sizeof(ElemType));
- if (!L.elem)
- exit(OVERFLOW);
- L.length=0;
- L.listsize=LIST_INIT_SIZE;
- return OK;
- }
- Status ListInsert_Sq(SqList &L, int i, ElemType e)
- {
- ElemType *newbase, *p,*q;
- if (i<1||i>L.length+1)
- return ERROR;
- if (L.length>=L.listsize)
- {
- newbase = (ElemType *)realloc(L.elem,
- (L.listsize+LISTINCREMENT)*sizeof(ElemType)) ;
- if (!newbase) exit (OVERFLOW);
- L.elem = newbase;
- L.listsize=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++;
- return OK;
- }
- Status ListDelete_Sq(SqList &L, int i, ElemType &e)
- {
- ElemType *p,*q;
- if ((i<1)||i>L.length) return ERROR;
- p=&L.elem[i-1];
- e=*p;
- q=L.elem+L.length-1;
- for (++p; p<=q; ++p)
- *(p-1)=*p;
- --L.length;
- return OK;
- } //ListDelete_Sq
- Status out(SqList L){
- cout << "该链表中含有的元素:" << endl;
- for(int i = 0;i < L.length;i++)
- cout << L.elem[i] << " " ;
- cout << endl;
- }
- Status in(SqList &L, int i){
- cout << "请依次" << i << "个输入数据" << endl;
- for(int j = 0;j < i; j++)
- cin >> L.elem[j];
- L.length = i;
- return OK;
- }
- int main()
- {
- SqList L;
- InitList(L);
- while(1)
- {
-
- int i = 0;
- cout << "请选择操作:" << endl
- << "1.输入数据" << " "
- << "2.插入数据" << " "
- << "3.删除数据" << " "
- << "4.输出" << " "
- <<"5.结束"<< endl;
- cin >> i;
- if(i == 1){
- int num = 0;
- cout << "请输入需要输入的个数:" << endl;
- cin >> num;
- in(L,num);
- }
- if(i == 2){
- int h,e;
- cout << "请输入插入位置和插入元素:" << endl;
- cin >> h >> e;
- ListInsert_Sq(L,h,e);
- }
- if(i == 3){
- int h,e;
- cout << "请输入删除位置:" << endl;
- cin >> h;
- ListDelete_Sq(L, h, e);
- }
- if(i == 4){
- out(L);
- }
- if(i == 5) break;
- cout << "任意键进行下一步:\n";
- getchar();
- getchar();
- system("cls");
- }
- cout << "game over" << endl;
- return 0;
- }
- #include <cstdlib>
- #include <iostream>
- #define TRUE 1
- #define FALSE 0 // 正错
- #define OK 1
- #define ERROR 0 //状态
- #define INFEASIBLE -1 //不可行
- #define OVERFLOW -2 //溢出
- #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
- #define LISTINCREMENT 10 // 线性表存储空间的分配增量
- // 一些常用表示
-
- typedef int ElemType; //类型,可改为 double ,long long、、、、
- typedef int Status; //状态
- typedef struct{
- int * elem; //存储空间基址
- int length; //当前长度
- int listsize; //当前分配的存储容量(以sizeof(Elemtype)为单位)
- }SqList;
-
-
- Status InitList(SqList &L){
- L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));//分配一个ElemType类型的LIST_INIT_SIZE长度的动态空间
- if(!L.elem) exit(OVERFLOW);//检查分配
- L.length = 0;//将当前长度赋值为0
- L.listsize = LIST_INIT_SIZE;//初始存储容量
- return OK;//创建成功,返回状态
- }
- using namespace std;
- int main()
- {
- SqList L;//定义一个链表
- InitList(L);//调用函数InitList
- return 0;
- }
草稿里躺了四年了,发出来吧。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。