#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;
}