完整的顺序表例子_byseyOrd

Posted seyord

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完整的顺序表例子_byseyOrd相关的知识,希望对你有一定的参考价值。

 

 

 

  1 #include "stdafx.h"
  2 #include<stdio.h>
  3 #include<malloc.h>
  4 #include<stdlib.h>
  5 
  6 #define LINK_INIT_SIZE 100
  7 #define LISTINCREAMENT 10
  8 #define ElemType int
  9 #define OVERFLOW -2
 10 #define OK 1
 11 #define ERROR 0
 12 
 13 typedef int status;
 14 
 15 //定义顺序表,并定义一个新类型Sqlist
 16 typedef struct Sqlist{
 17     ElemType *elem;
 18     int listsize;
 19     int length;
 20 }Sqlist;
 21 
 22 //函数声明
 23 status InitList(Sqlist &L);
 24 status CreatList(Sqlist &L);
 25 status PrintfList1(Sqlist L);
 26 status PrintfList2(Sqlist L);
 27 status DeleteList(Sqlist &L, int i);
 28 status ListEmpty(Sqlist L);
 29 status ListLength(Sqlist L);
 30 status InsertList(Sqlist &L, int i, ElemType e);
 31 status DestroyList(Sqlist &L);
 32 
 33 int main()
 34 {
 35     int i, n, j;
 36     ElemType e;
 37     Sqlist L1;
 38     InitList(L1);    //顺序表初始化操作
 39 
 40     ListEmpty(L1);    //顺序表判空
 41 
 42     CreatList(L1);    //顺序表赋值操作
 43 
 44     printf("正序输出为:
");
 45     PrintfList1(L1);    //顺序表正序输出操作
 46     printf("
");
 47     printf("逆序输出为:
");
 48     PrintfList2(L1);    //顺序表逆序输出操作
 49     printf("
");
 50 
 51     ListLength(L1);    //求顺序表表长
 52 
 53     printf("请输入插入的位置i=");
 54     scanf_s("%d", &i);
 55     e = rand() % 100 + 1;
 56     InsertList(L1, i, e);    //顺序表插入操作
 57     PrintfList1(L1);
 58     printf("
");
 59 
 60     printf("请输入删除的位置j=");
 61     scanf_s("%d", &j);
 62     DeleteList(L1, j);    //顺序表删除操作
 63     PrintfList1(L1);
 64 
 65     DestroyList(L1);    //顺序表销毁操作
 66     PrintfList1(L1);
 67     return 0;
 68 }
 69 
 70 //构建空的顺序表
 71 status InitList(Sqlist &L)
 72 {
 73     L.elem = (ElemType*)malloc(LINK_INIT_SIZE*sizeof(ElemType));
 74     if (!L.elem)
 75         exit(OVERFLOW);
 76     L.length = 0;    //将新的顺序表长度定为0
 77     L.listsize = LINK_INIT_SIZE;    //初始化顺序表大小为LINK_INIT_SIZE
 78     return OK;
 79 }
 80 
 81 //顺序表插入操作
 82 status InsertList(Sqlist &L, int i, ElemType e)
 83 {
 84     ElemType *newbase, *q, *p;
 85     if (i<1 || i>L.length + 1)
 86     {
 87         printf("未插入成功!!!
");
 88         return ERROR;
 89     }
 90     if (L.length >= L.listsize)
 91     {
 92         newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREAMENT)*sizeof(ElemType));
 93         if (!newbase)
 94             exit(OVERFLOW);
 95         L.elem = newbase;
 96         L.listsize += LISTINCREAMENT;
 97     }    //检验线性表是否满了,满了则重新增加存储空间
 98     q = &(L.elem[i - 1]);
 99     for (p = &(L.elem[L.length - 1]); p >= q; p--)
100         *(p + 1) = *p;
101     *q = e;
102     L.length++;
103     return OK;
104 }
105 
106 //顺序表输入操作
107 status CreatList(Sqlist &L)
108 {
109     ElemType i = 0, e;
110     for (i = 0; i < 10; i++)    //顺序表赋值操作
111     {
112         L.elem[i] = rand() % 100 + 1;
113         L.length++;
114     }
115     return 0;
116 }
117 
118 //顺序表正序输出操作
119 status PrintfList1(Sqlist L)
120 {
121     int i;
122     if (L.length == 0)
123     {
124         printf("该线性表为空!
");
125         return 0;
126     }
127     for (i = 0; i < L.length; i++)
128         printf("L.elem[%d]=%d
", i + 1, *(L.elem + i));
129 }
130 
131 //顺序表删除操作
132 status DeleteList(Sqlist &L, int i)
133 {
134     int *p, *q, e;
135     if (i<1 || i>L.length + 1)
136     {
137         printf("不存在此删除位置!!!
");
138         return ERROR;
139     }    //检验输入的i是否在线性表所存在的范围内
140     p = &(L.elem[i - 1]);
141     e = *p;
142     q = L.elem + L.length - 1;
143     for (p++; p <= q; ++p)
144         *(p - 1) = *p;
145     L.length--;
146     printf("删除的元素为%d
", e);
147     return 0;
148 }
149 
150 //顺序表逆序输出操作
151 status PrintfList2(Sqlist L)
152 {
153     int i;
154     if (L.length == 0)
155     {
156         printf("该线性表为空!
");
157         return 0;
158     }
159     for (i = L.length - 1; i >= 0; i--)
160         printf("L.elem[%d]=%d
", i + 1, *(L.elem + i));
161 }
162 
163 //求顺序表是否为空
164 status ListEmpty(Sqlist L)
165 {
166     if (L.length == 0)
167         printf("The List is Empty!!!
");
168     else
169         printf("The List is not Empty!!!
");
170     return 0;
171 }
172 
173 //求顺序表长度
174 status ListLength(Sqlist L)
175 {
176     printf("线性表的长度为%d
", L.length);
177     return OK;
178 }
179 
180 status DestroyList(Sqlist &L)
181 {
182     free(&L.elem[0]);
183     L.length = 0;
184     return 0;
185 }

 

以上是关于完整的顺序表例子_byseyOrd的主要内容,如果未能解决你的问题,请参考以下文章

pytest自定义测试固件_byseyOrd

python日志配置_byseyOrd

python的函数_byseyOrd

用python写入css文件(dom方式)_byseyOrd

用ET find和findall进行xml数据配置_byseyOrd

01=顺序表(python完整代码)