C++编程题
使用动态链表结构写出选猴王的函数,要求依次输出退出圈外的猴子编号和猴王的编号
使用动态链表结构写出选猴王的函数,要求依次输出退出圈外的猴子编号和猴王的编号
答案:#include<iostream>
#include<Windows.h>
using namespace std;int g_iGame=0;//用于给各节点赋值
struct GAME
{
int m_Num;
GAME *m_pNext;
};struct NUMBER
{
int m_Num;
}NUM[300];GAME *EatMouseList(int num,int count,GAME *pHead)
{
int i=0;
FILE *fp;
fp=fopen("mouse.bat","wb");
if(NULL==fp)
{
cout<<"Error"<<endl;
}
GAME *pCur=pHead;
GAME *pPre=pHead;
int iNumOut=0;//统计出局人数
int iCount=1;//报数
if(1==count)
{
while(pCur->m_pNext!=NULL)
{
Sleep(500);
pHead=pCur->m_pNext;
cout<<pCur->m_Num<<"易做图掉了!"<<endl;
delete pCur;
pCur=pHead;
}
return pHead;
}
while(iNumOut<num-1)
{
iCount++;
pPre=pCur;
if(pCur->m_pNext!=NULL)
{
pCur=pCur->m_pNext;
}
else
{
pCur=pHead;
}
if(iCount==count)
{
//Sleep(500);
cout<<pCur->m_Num<<"易做图掉了!"<<endl;
NUM[i].m_Num=pCur->m_Num;
fwrite(&NUM[i].m_Num,sizeof(NUM[i].m_Num),1,fp);
i++;
if(pCur==pHead)
{
pHead=pCur->m_pNext;
delete pCur;
pCur=pHead;
}
else if(NULL==pCur->m_pNext)
{
pPre->m_pNext=NULL;
delete pCur;
pCur=pHead;
}
else
{
pPre->m_pNext=pCur->m_pNext;
delete pCur;
pCur=pPre->m_pNext;
}
iCount=1;
iNumOut++;
}
}
fclose(fp);
return pHead;
}GAME *CreateList(int num)
{
GAME *pHead=NULL;
GAME *pCur;
GAME *pLast;
pCur=new GAME;
g_iGame++;
pCur->m_Num=g_iGame;
while(num>0)
{
num--;
if(NULL==pHead)
{
pHead=pCur;
}
else
{
pLast->m_pNext=pCur;
}
pLast=pCur;
pCur=new GAME;
g_iGame++;
pCur->m_Num=g_iGame;
}
if(0==num)
{
pLast->m_pNext=NULL;
}
delete pCur;
pCur=NULL;
return pHead;
}void PrintList(GAME *pHead)
{
GAME *pTemp=pHead;
if(NULL==pTemp)
{
cout<<"Empty!";
}
while(pTemp!=NULL)
{
cout<<pTemp->m_Num<<" ";
pTemp=pTemp->m_pNext;
}
cout<<endl;
}
//链表反序
GAME *ReverseGame(GAME *pHead)
{
GAME *pResult=NULL;
GAME *pAfter;
GAME *pCur;
pCur=pHead;
if(NULL==pHead)
{
return pResult;
}
while(pCur!=NULL)
{
pAfter=pCur->m_pNext;
pCur->m_pNext=pResult;
pResult=pCur;
pCur=pAfter;
}
return pResult;
}
int main()
{
int iNum,iCount;
cout<<"Pleas input Num:"<<endl;
cin>>iNum;
cout<<"Pleas input Count:"<<endl;
cin>>iCount;
GAME *pHead=NULL;
GAME *pSurv=NULL;
//创建并遍历链表
pHead=CreateList(iNum);
// cout<<"Before reverse:"<<endl;
// PrintList(pHead);//链表反序
// GAME *pReverseList;
// cout<<"After reverse:"<<endl;
// pReverseList=ReverseGame(pHead);
// PrintList(pReverseList);
//求出最后生还者
pSurv=EatMouseList(iNum,iCount,pHead);
cout<<pSurv->m_Num<<"捡回了小命!"<<endl;
FILE *fp=fopen("mouse.bat","rb");
if(NULL==fp)
{
cout<<"Error"<<endl;
}
NUMBER iPrint;
fopen("mouse.bat","rb");
fseek(fp,97*sizeof(NUMBER),SEEK_SET);
fread(&iPrint.m_Num,sizeof(iPrint.m_Num),1,fp);
fclose(fp);
cout<<iPrint.m_Num<<endl;
while(!feof(fp))
f
return 0;}
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*Link,*Position;
typedef struct
{
Link head,tail;
int len;
}LinkList;
int InitList(LinkList *L)
{
L->head=L->tail=NULL;
L->len= 0;
return OK;
}
int ListInsert(LinkList *L,int i,int e)
{
Link p,q;
int j = 1;
if (i <= 0 || i > L->len+1)
{
printf("ilegle position %d(i)",i);
return OK;
}
p = (Link)malloc(sizeof(LNode));
p->data = e;
q = L->head;
p->next=NULL;
if (L->len == 0) //如果链表为空.
{
L->head = L->tail = p;
++L->len;
return OK;
}
if (i==L->len+1)//在最后插入结点.
{
L->tail->next = p;
L->tail = p;
++L->len;
return OK;
}
while (j<i)
{
++j;
q=q->next;
}
q->next=p->next;
p->next=q;
++L->len;
return OK;
}
int main()
{
int i,n,m,temp;
LinkList L;
Link p,last;
cout<<"Please input total number of the monkeys: ";
cin>>n;
cout<<"Piease input the jiange:";
cin>>m;
InitList(&L);
for(i=1;i<=n;i++)
ListInsert(&L,i,i);
L.tail->next=L.head;
p=L.head;