程序设计课程设计报告.docx
《程序设计课程设计报告.docx》由会员分享,可在线阅读,更多相关《程序设计课程设计报告.docx(8页珍藏版)》请在冰豆网上搜索。
![程序设计课程设计报告.docx](https://file1.bdocx.com/fileroot1/2022-11/27/a850d4f1-e673-48e4-830c-6aff8ae81328/a850d4f1-e673-48e4-830c-6aff8ae813281.gif)
程序设计课程设计报告
程序设计课程设计报告
程序设计课程设计报告
设计人:
电气Q1142
李艳平
(11150038)
日期:
2012-11-29
指导教师:
桂超
设计题目:
链表操作
一、设计任务
1、创建一个单链表,并返回头结点的地址head
2、编写函数circulachain,将上述单链表改为单循环链表
3、编写函数addnode,在数据为x的结点之
后插入数据为y的结点
X的结点
对他们从1开始编号,即1,2,3,n,
如果从某一个人开始报数,他报数1,按顺时针进行,紧挨的人报数2,再往下的人跟
着报3……,一直报到k,凡报到k的人出列,再往下,继续从1开始报数,2,3,……,
一直报到k,凡报到k的人出列。
试输出该问题中顺序出列的人的名字。
二、功能描述
创建单循环链表函数:
chain*circulchain(char
*ch)
销毁链表:
voidDestroy(chain*head)显示链表中每个结点的数据:
voidShow(chain*head)
插入结点函数:
voidAddNode(chain*head,charx,chary)
删除结点函数:
voidDeleNode(chain*head,charx)
三、程序实现
1、单链表的基本操作:
#include
usingnamespacestd;
structchain
(
chardata;
chain*next;
};
chain*crechain(char*ch)〃创建单链表
(
inti;
chain*head=NULL,*p=NULL,*q=
NULL;
for(i=0;ch[i]!
=,\0"i++)(
p=newchain;
p->data=ch[i];
if(head==NULL)
(
head=p;
)
else
(
q->next=p;
}
q=P;
}
q->next=NULL;
return(head);
}voidAddNode(chain*head,charx,chary)/*
插入结点*/
chain*r,*s;
if(head==NULL)
cout«''链表为空!
H«endl;
else
(
r=head;
while(r!
=NULL)
(
if(r->data!
=x)
(
r=r->next;
)
elsebreak;
}
if(r==NULL)
cout<<”没有数据为x的节点!
n«endl;
else
(
s=newchain;
s->data=y;
s->next=r->next;
r->next=s;
}
}
voidDeleNode(chain*head,charx)/*删除
结点*/
(
chain*p=head,*s;
if(head==NULL)
cout«''链表为空!
"vvendl;
if(head->data==x)
(
head=p・>next;
deletep;
}
else
(
while(p->next!
=NULL)
(
if(p->next->data!
=x)
p=p->next;
else
s=p->next;
p->next=s->next;
deletes;
break;
)
}
}
}
voidShow(chain*head)〃输出链表
(
chain*p;
p=head;
while(p!
=NULL)
{
cout«p->data;
p=p->next;
)
cout«H\nH;
voidDestroy(chain*head)〃销毁链表
chain*p;
while(head!
=NULL)
(
p=head;
head=p・>next;
deletep;
)
}
intmain()
(
chain*head;
head=crechain(Habcdn);
Show(head);
AddNode(head,b,T);
Show(head);
DeleNode(head,'c');
Show(head);
Destroy(head);
return0;
}
2、单循环链表:
chain*circulchain(char*ch)
inti;
chain*head=NULL,*p=NULL,NULL,*r;
for(i=0;ch[i]!
=,\0"i++)
{
p=newchain;
p->data=ch[i];
if(head==NULL)
(
head=p;
)
else
(
q->next=p;
)
q=p;
)
q->next=head;
return(head);
}
3、约瑟夫问题:
#include
usingnamespacestd;
structJoseph
(
charch;
Joseph*next;
};
Joseph*head,//Joseph结构指针,全局变
voidcreate(intn)〃给每个结点数据赋值
(
inti;
Joseph*q;
p=(Joseph*)malloc(sizeof(Joseph));
p->ch='A';
p->next=NULL;
head=p;
q=p;
for(i=1;ip=(Joseph*)malloc(sizeof(Joseph));
p->ch='A'+i;
〃将p结点加到链表尾部
〃让q指向链表尾部结
q->next=p;
〃链表尾部指向空
q=P;点
p->next=NULL;
}
tail=q;
tail->next=head;
}
voidDisplay(intm)〃依次输出出列的人的名字(
intx=0;
Joseph*q;
q=tail;
do
p=q->next;//p赋值为q相邻的下一个结点
X++;
if(x%m==0)
(
cout«n出列的人为"vvp・>chvvendl;
q->next=p->next;
deletep;
p=NULL;
)
elseq=p;〃q指向相邻的下一
结点p
)
while(q!
=q->next);〃剩余结点数不为1,
则继续循环
head=q;
}
intmain()
intn,m;
head=NULL;
cout«”请输入总人数\n”;
cin»n;
cout«"请输入间隔\n";cin»m;
create(n);
Display(m);
coutvv”最后一个人是"vvhead.>chvvendl;
deletehead;
return0;