#include<stdio.h>
#include<malloc.h> //注意这个分配地址的头文件必须得加
#define INITIAL_SIZE 100 //定义线性表的初始大小
#define INCREACEMENT 10 //定义线性表的分配增量
//定义一个线性表结构
struct List
{
int *Elem; //线性表的基址
int Length; //当前长度
int Allocate_size; //当前分配的容量
}*L;
//初始化线性表
void Init_List(List &L)
{
L.Elem=(int *)malloc(INITIAL_SIZE* sizeof(int)); //分配基地址
if (!L.Elem ) return;
L.Length =0; //初始长度为0
L.Allocate_size =INITIAL_SIZE; //初始分配的大小为100
}
//向线性表中插入一个数
void InsertInto_List(List &L,int i,int e)
{
int *p,*q;
if (i>L.Length+1 || i<1)return; //先判断要插入的序号在不在线性表长度之内
if(L.Length>=L.Allocate_size) //如果线性表的长度大于等于分配的大小时,需重新分配线性表大小
{
int *newbase=(int *)realloc(L.Elem ,(L.Length +INCREACEMENT)*sizeof(int));
if (!newbase)return;
L.Elem =newbase; //基地址改变
L.Allocate_size +=INCREACEMENT; //分配的大小加倍
}
q=&(L.Elem [i-1]); //要插入的位序
for (p=&(L.Elem [L.Length-1]);p>=q;p--) //循环实现要插入位置以后的数都要后移一位
{
*(p+1)=*p;
}
*q=e; //给要插入的位序赋值要插入的值
L.Length +=1; //线性表加1
}
//删除线性表中一个数
void delete_List(List &L,int i,int &e)
{
int *p,*q;
if (i<1||i>L.Length )return ; //先判断要删除的位序的值是否在线性表中
else
{
p=&L.Elem [i-1]; //要删除的位序
e=*p; //将删除的数赋值给e
q=L.Elem +L.Length -1; //线性表最后的位序
for (++p;p<=q;++p)*(p-1)=*p; //循环实现删除位序后的数据前移一位
--L.Length ; //线性表长度减一
}
}
//实现两个数相比较
bool compare(int a,int b)
{
if (a==b)return true;
else return false;
}
//查找一个数在线性表中的位序
int Locate_List(List &L,int e)
{
for (int i=1;i<L.Length;i++ )
{
if (compare(e,L.Elem[i]))
return i;
}
return 0;
}
int main()
{
//要插入的值
int InsertNum=89;
int SeletNum=76;
int compareNum=76;
int returnValue; //要返回的值
//注意要首先初始化L,因为一开始L是不指向任何地址的,
//只有malloc一个地址给他,L才指向一个有意义的地址,*L才能真正得到一个struct List,之后的所有对L的使用才有意义
L=(List *)malloc(sizeof(struct List));
Init_List(*L); //初始化线性表
//插入数据
InsertInto_List(*L,1,InsertNum);
InsertInto_List(*L,2,InsertNum);
InsertInto_List(*L,3,SeletNum);
delete_List(*L,2,returnValue); //删除数据
int Locate= Locate_List(*L,InsertNum); //查找一个数在线性表中的位序
printf("%d 在线性表中的位序为: %d\n",compareNum,Locate); //输出查找到的位序
return 0;
}
约瑟夫环
#include <stdio.h>
#include <malloc.h>
/*构建结构体*/
typedef struct Node{
int Num;
struct Node *next;
}JoseNode, *PNode, *HNode;
/**********初始化循环单链表*********/
int JoseInit(HNode *h)
{
if (!h)
{
printf("初始化链表错误!\n");
return 0;
}
(*h)->next = (*h);//循环单链表
return 1;
}
/*************单链表插入操作**********/
int JoseInsert(JoseNode *h, int pos, int x)
{
PNode p=h,q;
int i=1;
if (pos == 1)/*尾插法*/
{
p->Num = x;
p->next = p;
return 1;
}
while(i<pos-1)
{
p=p->next;
i++;
}
q=(PNode)malloc(sizeof(JoseNode));
q->Num=x;
q->next=p->next;
p->next=q;
return 1;
}
/*遍历*/
void TraverseList(HNode h, int M)
{
int i = 0;
PNode p = h;
printf("参与的人的编号为:\n");
while (i<M)
{
printf("%d\t", p->Num);
p = p->next;
i++;
}
printf("\n");
}
/**************出局函数****************/
int JoseDelete(HNode h, int M, int k)
{ int i;
PNode p=h,q;
while(M>1)
{
for(i=1;i<k-1;i++)
{
p=p->next;
}
q=p->next;
p->next=q->next;
printf("出局的人为:%d号\n",q->Num);
free(q);
p=p->next;
M--;
}
printf("***************获胜者为:%d号***************",p->Num);
return 1;
}
/***************************************/
int main()
{
int i;//计数器
int N;//参与的人数
int k;//报数密码
printf("请输入参与人数:");
scanf("%d",&N);
printf("请输入出局密码:");
scanf("%d",&k);
/**************得到头结点****************/
HNode h = ((HNode)malloc(sizeof(JoseNode)));
/***************初始化单链表************/
JoseInit(&h);
/******将编号插入到循环单链表中******/
for (i = 1; i <=N; i++)
{
JoseInsert(h, i, i);
}
/**************遍历单链表***************/
TraverseList(h,N);
/***************出局函数************/
if(k > 1)
JoseDelete(h, N, k);
else
{
for(i = 1; i < N; i++)
printf("出局的人为:%d号\n",i);
printf("***************获胜者为:%d号***************",N);
}
printf("\n");
printf("\n");
return 0;
}