您的位置:首页 > 编程语言 > PHP开发

thinkphp的field方法的用法总结,或许还有你不知道的

2015-07-02 09:02 561 查看
循环链表CLinkList.h

#include <iostream>
using namespace std;

const int CardNumber=13;
//链表存储结构的定义
typedef struct CLinkList{
int data;
struct CLinkList *next;
}node;

void ShowMenu();

void ShowList(node **pNode);

//初始化
void ds_init(node **pNode);

//插入结点
//参数:链表的第一个结点,插入位置
void ds_insert(node **pNode,int i);

void ds_delete(node **pNode,int i);

int ds_search(node *pNode,int elem);

void Josephus();
//Josephus
node *create(int n);

//判断是否有环,方法1:判断步数
int HasLoop1(node *head);
//判断是否有环,方法2:快慢指针
int HasLoop2(node *head);

//魔术师发牌问题
void Magic();

void DestoryList(node **list);

node *CreateLinkList();

void Magician(node *head);

//拉丁方阵

void latin();

node *CreateLatinList(int num);

void PrintLatin(node *p,int i);

 

CLinkList.cpp

#include "CLinkList.h"

int main(){
int cmd;
int e;
CLinkList *head=NULL;
ShowMenu();
cin>>cmd;
while(cmd){
switch(cmd){
case 1:
ds_init(&head);
break;
case 2:
ShowList(&head);
break;
case 3:
ds_insert(&head,2);
ShowList(&head);
break;
case 4:
ds_delete(&head,2);
ShowList(&head);
break;
case 5:
cout<<"查找数据:100,位置为:"<<ds_search(head,100)<<endl;
break;
case 6:
Josephus();
break;
case 7:
HasLoop1(head);
break;
case 8:
HasLoop2(head);
break;
case 9:
Magic();
break;
case 10:
latin();
break;
case 0:exit(0);break;
default:
exit(-1);
break;
}
ShowMenu();
cin>>cmd;
}
return 0;
}

void ShowMenu(){
char *str="\t\t****************************";
cout<<str<<endl;
cout<<"\t\t1.初始化"<<endl;
cout<<"\t\t2.显示列表"<<endl;
cout<<"\t\t3.插入元素"<<endl;
cout<<"\t\t4.删除元素"<<endl;
cout<<"\t\t5.查找元素"<<endl;
cout<<"\t\t6.约瑟夫问题"<<endl;
cout<<"\t\t7.步数判断循环"<<endl;
cout<<"\t\t8.快慢指针判断循环"<<endl;
cout<<"\t\t9.魔术师发牌"<<endl;
cout<<"\t\t10.拉丁方阵"<<endl;
cout<<"\t\t0.退出"<<endl;
cout<<str<<endl;
}

void ShowList(node **pNode){
int i=0;
CLinkList *p;
cout<<"\t\t";
for(p=*pNode;p->next!=(*pNode);p=p->next){
if(i%5==0){
cout<<endl;
cout<<"\t\t";
}
cout<<p->data<<"  ";
i++;
}
if(i%5==0){
cout<<endl;
cout<<"\t\t";
}
cout<<p->data<<"  ";
cout<<endl;
}

void ds_init(node **pNode){
int item;
node *p,*q;
cout<<"输入结点的值,输入0完成初始化"<<endl;

while(1){
scanf("%d",&item);
fflush(stdin);
if(item==0){
return;
}

if(*pNode==NULL){
*pNode=(node *)malloc(sizeof(CLinkList));
(*pNode)->data=item;
(*pNode)->next=(*pNode);
}else{
for(p=*pNode;p->next!=(*pNode);p=p->next);
q=(node *)malloc(sizeof(CLinkList));
q->data=item;
q->next=(*pNode);
p->next=q;
}
}
}

void ds_insert(node **pNode,int i){
node *temp;
node *target;

int j,item;
cout<<"请输入需要插入元素的节点信息:";
scanf("%d",&item);
fflush(stdin);

if(i==1){
temp=(node *)malloc(sizeof(CLinkList));
if(!temp)exit(0);
for(target=*pNode;target->next!=*pNode;target=target->next);
temp->data=item;
temp->next=*pNode;
target->next=temp;
}else{
target=*pNode;
temp=(node *)malloc(sizeof(CLinkList));
if(!temp)exit(0);
j=1;
for(;j<(i-1);++j){
target=target->next;
}

temp->data=item;
temp->next=target->next;
target->next=temp;
}
}

void ds_delete(node **pNode,int i){
node *target;
node *temp;
int j=1;

if(i==1){
for(target=*pNode;target->next!=*pNode;target=target->next);
temp=*pNode;
*pNode=(*pNode)->next;
target->next=*pNode;
free(temp);
}
else{
target=*pNode;
for(;j<i-1;++j){
target=target->next;
}
temp=target->next;
target->next=temp->next;
free(temp);
}
}

int ds_search(node *pNode,int elem){
node *target;
int i=1;

for(target=pNode;target->data!=elem&&target->next!=pNode;++i){
target=target->next;
}
if(target->data==elem){
return i;
}
if(target->next=pNode){
return 0;
}else{
return i;
}
}

node *create(int n){
node *p,*temp,*head;

int i=1;

head=(node *)malloc(sizeof(node));
p=head;
if(n!=0){
while(i<=n){
temp=(node *)malloc(sizeof(node));
temp->data=i++;
p->next=temp;
p=temp;
}
temp->next=head->next;
}
free(head);
ShowList(&(p->next));
return p->next;

}

void Josephus(){
int n=41;
int m=3;
int i;

node *p=create(n);
node *temp;
m%=n;

while(p!=p->next){
for(i=1;i<m-1;i++){
p=p->next;
}
printf("%d->",p->next->data);
temp=p->next;
p->next=temp->next;
free(temp);
p=p->next;
}
printf("%d\n",p->data);
}

//比较步数的方法
int HasLoop1(node *head){
node *cur1,*cur2;
int pos1,pos2;
pos1=0;
cur1=head;
while(cur1){
pos2=0;
cur2=head;
while(cur2){
if(cur1==cur2){
if(pos1==pos2){
break;
}else{
printf("有环,环的位置在%d处\n",pos2);
return 1;
}
}
cur2=cur2->next;
pos2++;
}
cur1=cur1->next;
pos1++;
}
return 0;
}

//快慢指针
int HasLoop2(node *head){
node *p,*q;
p=q=head;

while(p!=NULL&&q!=NULL&&p->next!=NULL){
p=p->next;
if(p->next!=NULL){
q=q->next->next;
}
printf("p:%d,q:%d\n",p->data,q->data);
if(p==q){
printf("有环链表\n");
return 1;
}
}
return 0;
}

void Magic(){
node *p;
int i;

p=CreateLinkList();
Magician(p);
printf("按如下顺序排序:\n");
for(i = 0; i < CardNumber; i++){
printf("黑桃%d ",p->data);
p=p->next;
}

DestoryList(&p);
}

node *CreateLinkList(){
node *head=NULL;
node *s,*r;
int i;

r=head;

for(i=1; i <= CardNumber; i++){
s = (node *)malloc(sizeof(node));
s->data = 0;

if(head == NULL){
head = s;
}else{
r->next = s;
}
r = s;
}
r->next = head;

return head;
}

//发牌顺序计算
void Magician(node *head){
node *p;
int j;
int Countnumber = 2;

p = head;
p->data = 1; //第一张牌放1

while(1){
for(j = 0; j < Countnumber; j++){
p = p->next;
if(p->data != 0){
j--;
}
}

if(p->data == 0){
p->data = Countnumber++;

if(Countnumber == 14){
break;
}
}
}
}

//销毁工作
void DestoryList(node **list){
node *ptr=*list;
node *buff[CardNumber];

int i = 0;

while(i < CardNumber){
buff[i++] = ptr;
ptr = ptr->next;
}

for(i=0;i<CardNumber;++i){
free(buff[i]);
}
*list=NULL;
}

//拉丁方阵
void latin(){
node *p;
int num,i;
printf("请输入方阵大小:");
scanf("%d",&num);
fflush(stdin);

p=CreateLatinList(num);
for(i = 0; i< num; i++){
PrintLatin(p,i);
}
}

node *CreateLatinList(int num){
int i;
node *p,*q,*head;

head = (node *)malloc(sizeof(node));
q = p = head;
for(i=1;i<=num;i++){
p = (node *)malloc(sizeof(node));
p->data = i;
q->next = p;
q=p;
}
q->next = head->next;
free(head);
return q->next;
}

void PrintLatin(node *p,int i){
int j;
node *q,*head=p;

for(j = 1;j <= i; j++){
head=head->next;
}
q=head;
while(head->next!=q){
printf("%d  ",head->data);
head=head->next;
}
printf("%d  ",head->data);
printf("\n");
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: