# 线性顺序表
1.线性表的顺序存储是指用一组地址连续的存储单 元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。
2.空间效率更高
3.如果需要用到的存储空间比申请的少,会造成空间浪费,反之,则不足。
静态顺序表的实现
头文件的定义说明
#include "string.h"
#include "ctype.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
```
```c
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef char ElemType;
1.先定义结构体
data用来存放数据,cur指光标
typedef struct
{
ElemType data;
int cur;
} Component,StaticLinkList[MAXSIZE];
2.将数组中的一些分量链成备用链表;
一般第一个光标存放的是第一个没有元素的下表标,最后一个光标是第一个下标,且data最后有值的那个光标是0;
Status InitList(StaticLinkList space)
{
int i;
for (i=0; i<MAXSIZE-1; i++)
space[i].cur = i+1;
space[MAXSIZE-1].cur = 0; /* 目前静态链表为空,最后一个元素的cur为0 */
return OK;
}
//给光标赋值;
3.找出备用链表;
/* 若备用空间链表非空,则返回分配的结点下标,否则返回0 */
int Malloc_SSL(StaticLinkList space)
{
int i = space[0].cur; /* 当前数组第一个元素的cur存的值 */
/* 就是要返回的第一个备用空闲的下标 */
if (space[0]. cur)
space[0]. cur = space[i].cur; /* 由于要拿出一个分量来使用了, */
/* 所以我们就得把它的下一个 */
/* 分量用来做备用 */
return i;
}
4. 测量数组元素的长度
int ListLength(StaticLinkList L)
{
int j=0;
int i=L[MAXSIZE-1].cur;
while(i)
{
i=L[i].cur;
j++;
}
return j;
}
5. 在第i个元素前插入元素
Status ListInsert(StaticLinkList L, int i, ElemType e)
{
int j, k, l;
k = MAXSIZE - 1; /* 注意k首先是最后一个元素的下标 */
if (i < 1 || i > ListLength(L) + 1)
return ERROR;
j = Malloc_SSL(L); /* 获得空闲分量的下标 */
if (j)
{
L[j].data = e; /* 将数据赋值给此分量的data */
for(l = 1; l <= i - 1; l++) /* 找到第i个元素之前的位置 */
k = L[k].cur;
L[j].cur = L[k].cur; /* 把第i个元素之前的cur赋值给新元素的cur */
L[k].cur = j; /* 把新元素的下标赋值给第i个元素之前元素的ur */
return OK;
}
return ERROR;
}
6.删除第i 个元素
/* 删除在L中第i个数据元素 */
Status ListDelete(StaticLinkList L, int i)
{
int j, k;
if (i < 1 || i > ListLength(L))
return ERROR;
k = MAXSIZE - 1;
for (j = 1; j <= i - 1; j++)
k = L[k].cur;
j = L[k].cur;
L[k].cur = L[j].cur;
Free_SSL(L, j);
return OK;
}
7.浏览打印元素
Status ListTraverse(StaticLinkList L)
{
int j=0;
int i=L[MAXSIZE-1].cur;
while(i)
{
visit(L[i].data);
i=L[i].cur;
j++;
}
return j;
printf("\n");
return OK;
}
```
```c
Status visit(ElemType c)
{
printf("%c ",c);
return OK;
}
8. 主函数对它们调用
int main()
{
StaticLinkList L;
Status i;
i=InitList(L);
printf("初始化L后:L.length=%d\n",ListLength(L));
i=ListInsert(L,1,'F');
i=ListInsert(L,1,'E');
i=ListInsert(L,1,'D');
i=ListInsert(L,1,'B');
i=ListInsert(L,1,'A');
printf("\n在L的表头依次插入FEDBA后:\nL.data=");
ListTraverse(L);
i=ListInsert(L,3,'C');
printf("\n在L的“B”与“D”之间插入“C”后:\nL.data=");
ListTraverse(L);
i=ListDelete(L,1);
printf("\n在L的删除“A”后:\nL.data=");
ListTraverse(L);
printf("\n");
return 0;
}
采用循环链表写关于约瑟夫问题
头文件和结构体定义
#include
#include
typedef int Elemtype;
typedef struct Node
{
Elemtype data;
struct Node *next;
}Node;
创造一个循环链表并给链表赋值;
struct Node* creat(int n)
{
int i=1,num;
Node *p=NULL,*head,*p1;
head=(Linklist)malloc(sizeof(Node));
p=head;
if(n!=0)
{
while(idata=i++;
p->next =p1;
p=p1;
}
p1->next=head->next ;//把没有数据头节点删除掉;
free(head);
return p1->next;/*返回的是第一个元素的地址;*/
}
}
主函数调用
int main()
{
int i=1,n,m;
Node *p;
Node *q;
printf("请输入要进行游戏的人数:");
scanf("%d",&n);
p=creat(n);
m=n%3;//求余数 ,数到第三是把第三去掉
while(p!=p->next )
{
for(i=1;inext ;
}
printf("%d->",p->next->data );
q=p->next;
p->next =q->next ;
free(q) ;
p=p->next ;
}
printf("%d\n",p->data );
return 0;
}