1/**************************************
2线性表的顺序存储--顺序表
3
4 第i个元素的存储位置紧接在i-1个元素的存储位置后面,假定线性表的元素类型为ElemType,则
5 每个元素所占的存储空间大小(即字节数)为sizeof(ElemType),整个线性表所占的存储空间大
6 小为n*sizeof(ElemType),其中n表示线性表的长度
7*************************************/
8
9
10#include "stdafx.h"
11#define MaxSize 50 //线性表的长度
12typedef char ElemType; //定义线性表的元素类型为 char
13struct List
14{
15 ElemType list[MaxSize];
16 int size;
17};
18
19//****************************************************************置空表
20void setnull(struct List *p)
21{
22 p->size=0;
23}
24
25
26//****************************************************************插入结点
27void insert(struct List *p,ElemType x,int i)
28{
29 int j;
30 if(i<1||i>p->size+1) //例如: size=1或者4
31 printf("位置参数不正确,不能进行插入操作!\n");
32 else
33 {
34 p->size++; //(p->size)++
35 for(j=p->size-1;j>=i;j--)
36 p->list[j]=p->list[j-1]; //注意下标和size的关系j=size-1
37 p->list[j]=x;
38
39 }
40}
41//****************************************************************返回长度
42int length(struct List *p)
43{
44 return(p->size);
45}
46//****************************************************************取表中的第i个结点
47 int get(struct List *p,int i)
48 {
49 if (i>p->size)
50 return(-1);
51 else
52 return(p->list[i-1]);
53 }
54
55//****************************************************************按值查找
56 int locate(struct List *p,ElemType x)
57 {
58 int i=0;
59 while (i<p->size && p->list[i]!=x) i++;
60 if (i==p->size)
61 return(-1);
62 else
63 return(i+1);
64 }
65
66
67
68//****************************************************************显示顺序表
69void display(struct List *p)
70{
71 int j;
72 if(p->size==0)
73 printf("该顺序表为空,不能显示!\n");
74 else
75 {
76 printf("顺序表:");
77 if(p->size==1) //只有一个结点的情况
78 printf("%c",p->list[p->size-1]);
79 else//有一个以上结点的情况
80 {
81 for(j=0;j<p->size-1;j++)
82 printf("%c->",p->list[j]);
83 printf("%c",p->list[j]);//显示最后一个结点
84 }
85 printf("\n");
86 }
87}
88
89
90
91
92
93void main()
94{
95 struct List L;
96 setnull(&L);
97 insert(&L,'a',1);
98 insert(&L,'b',2);
99 insert(&L,'c',2);
100
101 display(&L);
102 printf("线性表的长度:%d\n",length(&L));
103
104}
105
2线性表的顺序存储--顺序表
3
4 第i个元素的存储位置紧接在i-1个元素的存储位置后面,假定线性表的元素类型为ElemType,则
5 每个元素所占的存储空间大小(即字节数)为sizeof(ElemType),整个线性表所占的存储空间大
6 小为n*sizeof(ElemType),其中n表示线性表的长度
7*************************************/
8
9
10#include "stdafx.h"
11#define MaxSize 50 //线性表的长度
12typedef char ElemType; //定义线性表的元素类型为 char
13struct List
14{
15 ElemType list[MaxSize];
16 int size;
17};
18
19//****************************************************************置空表
20void setnull(struct List *p)
21{
22 p->size=0;
23}
24
25
26//****************************************************************插入结点
27void insert(struct List *p,ElemType x,int i)
28{
29 int j;
30 if(i<1||i>p->size+1) //例如: size=1或者4
31 printf("位置参数不正确,不能进行插入操作!\n");
32 else
33 {
34 p->size++; //(p->size)++
35 for(j=p->size-1;j>=i;j--)
36 p->list[j]=p->list[j-1]; //注意下标和size的关系j=size-1
37 p->list[j]=x;
38
39 }
40}
41//****************************************************************返回长度
42int length(struct List *p)
43{
44 return(p->size);
45}
46//****************************************************************取表中的第i个结点
47 int get(struct List *p,int i)
48 {
49 if (i>p->size)
50 return(-1);
51 else
52 return(p->list[i-1]);
53 }
54
55//****************************************************************按值查找
56 int locate(struct List *p,ElemType x)
57 {
58 int i=0;
59 while (i<p->size && p->list[i]!=x) i++;
60 if (i==p->size)
61 return(-1);
62 else
63 return(i+1);
64 }
65
66
67
68//****************************************************************显示顺序表
69void display(struct List *p)
70{
71 int j;
72 if(p->size==0)
73 printf("该顺序表为空,不能显示!\n");
74 else
75 {
76 printf("顺序表:");
77 if(p->size==1) //只有一个结点的情况
78 printf("%c",p->list[p->size-1]);
79 else//有一个以上结点的情况
80 {
81 for(j=0;j<p->size-1;j++)
82 printf("%c->",p->list[j]);
83 printf("%c",p->list[j]);//显示最后一个结点
84 }
85 printf("\n");
86 }
87}
88
89
90
91
92
93void main()
94{
95 struct List L;
96 setnull(&L);
97 insert(&L,'a',1);
98 insert(&L,'b',2);
99 insert(&L,'c',2);
100
101 display(&L);
102 printf("线性表的长度:%d\n",length(&L));
103
104}
105