level 1
请问,如何用C语言来实现一个循环链表?请各位指教,最好能给我一个例子.我的 QQ是410521729谢谢!
2005年04月18日 02点04分
1
level 1
#include
#define LEN sizeof(struct shu)#define NULL 0struct shu{int num; struct shu *next; }; main() {struct shu *p,*q,*head; int i,a,b; head=NULL; p=q=(struct shu *)malloc(LEN); scanf("%d",&p->num); while(p->num!=NULL) {if(head==NULL)head=p; else {q->next=p;q=p;} p=(struct shu *)malloc(LEN); scanf("%d",&p->num); } q->next=head; scanf("\n%d%d\n",&a,&b); while(q->num!=a) q=q->next; p=q->next; while(p!=q) {for(i=1;i<=b-2;i++) {q=p;p=p->next;} q->next=p->next; printf("%d,",p->num); q=q->next; p=q->next; } printf("%d",q->num);}
2005年04月18日 04点04分
2
level 1
不用,只是我最近也刚好再做这个题目,就拿来班门弄斧啦!!!! 有什么问题还请指教哦!! 有空长联系,我的qq是“283263249” 加的时候附加 :c朋友
2005年04月18日 13点04分
4
level 0
可以呀很简单的问题
# define NULL 0#
define LEN sizeof(struct Londe)main(){ struct Londe { int number; int password; struct Londe *next; }; int n=0,m,j=1; struct Londe *p1,*p2,*head; p1=p2=(struct Londe*)malloc(sizeof(LEN)); printf("input the first number:"); scanf("%d",&p1->number); printf("input the first password:"); scanf("%d",&p1->password); head=NULL; while(p1->number!=0 && p1->password!=0) {n++; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Londe *)malloc(sizeof(LEN)); printf("input the %dth number:",n+1); scanf("%d",&p1->number); printf("input the password of the %dth number:",n+1); scanf("%d",&p1->password); } p1=p2->next=head; printf("input m:"); scanf("%d",&m); while(n!=1) {while(m!=j) { j++; p1=p1->next; p2=p2->next; } printf("%d,",p1->number); m=p1->password; p2->next=p1->next; p1=p2->next; j=1; n--; } printf("%d\n",p1->number);}欢迎和对c语言有兴趣的朋友交流,qq:253968497 验证时注明c语言交流
2005年09月15日 09点09分
9
level 0
以7个人为例,用链表实现,每个人都有自己的密码,当他出列时,以他的密码作为下一个的个数,例这七个人的密码分别是3,1,7,2,4,8,4。第一个执行的个数是20
2005年09月27日 03点09分
10
level 0
#include
#include
/* to test something about LIST data structure2005.9.26 */struct list{ int data; int key; struct list* nextPtr;};typedef struct list List;typedef List* ListPtr;ListPtr insert(ListPtr*,int);void constructList(ListPtr*,int);void print(ListPtr,int);void distroy(ListPtr);void maintain(ListPtr,int);int main(){ int n; ListPtr sPtr; sPtr=NULL; printf("\ninput n:"); scanf("%d",&n); constructList(&sPtr,n); print(sPtr,n);
#if 1 maintain(sPtr,n); #
endif getch(); return 0;}ListPtr insert(ListPtr* sPtr,int value){ ListPtr currentPtr,previousPtr,newPtr; static int i=1; newPtr=(ListPtr)malloc(sizeof(ListPtr)); currentPtr->nextPtr=NULL; if(newPtr!=NULL) {newPtr->data=i;newPtr->key=value;newPtr->nextPtr=NULL;currentPtr=*sPtr; previousPtr=NULL; while(currentPtr!=NULL) { previousPtr=currentPtr; currentPtr=currentPtr->nextPtr; } if(previousPtr==NULL){ *sPtr=newPtr; i++; return *sPtr;}else{ previousPtr->nextPtr=newPtr; i++; return newPtr; } }else{ printf("can't malloc \n"); }}void constructList(ListPtr* sPtr,int n){ int i; int key; ListPtr currentPtr; currentPtr=NULL; for(i=0;i
nextPtr=*sPtr;}void print(ListPtr sPtr,int n){ int i; printf("List is:"); for(i=0;i
data); printf(" %d ->",sPtr->key); sPtr=sPtr->nextPtr; } printf("\n");}void distroy(ListPtr sPtr){ ListPtr tempPtr; while(sPtr!=NULL) { tempPtr=sPtr; sPtr=sPtr->nextPtr; free(tempPtr); } printf("free success!\n");}void maintain(ListPtr sPtr,int n){ int m,length,posit; ListPtr currentPtr,previousPtr,tempPtr; length=n; currentPtr=previousPtr=sPtr; printf("input the first KEY:"); scanf("%d",&m); while(length>=1) { posit=m-2; if(posit==-1) { m=currentPtr->key; printf("%d--",currentPtr->data); currentPtr=previousPtr=currentPtr->nextPtr; }else{ while(posit) { currentPtr=currentPtr->nextPtr; posit--; } previousPtr=currentPtr; tempPtr=currentPtr->nextPtr; m=tempPtr->key; printf("%d--",tempPtr->data); currentPtr=tempPtr->nextPtr; previousPtr->nextPtr=currentPtr; length--; } } printf("\n");}
2005年10月07日 09点10分
12
level 1
我们老师是要求是用约瑟夫问题来解决八仙选首的问题,就是说报数M可以从一到8开始变化,最后出列的就是首领,八趟后就会出现八个首领(位置可能重复),把这些首领放在一个数组里跟原来的八个数相比,看哪个数是没现在首领的位置上,把没出现的数输出就行了.
2005年10月08日 03点10分
14
level 1
请教两个SCANF函数,第一个输入的是密码,第二个输入总人数和初始值吗 ?
2005年10月12日 12点10分
16
level 1
用一维数组实现约瑟夫问题#include"stdio.h"main(){int m,n,s,i,count,k,array[100];printf("\nPLease tell me how many children are there?\n");scanf("%3d",&n);printf("\nFrom which to count\n");scanf("%3d",&m);printf("\nHow shall I count\n");scanf("%3d",&s);for(i=1;i<=n;i++)array[i]=i;count=n;k=m;while(count!=0){k=k-1+s;while(k>count)k=k-count;printf("%7d",array[k]);if((n-count+1)%10==0)printf("\n");if(k!=count)for(i=k;i
2005年10月22日 14点10分
18
level 0
谁给我分析分析它的时间、空间复杂度,并给出进一步改进方案
2005年10月23日 07点10分
21
level 0
那个叫天地蒙胧龙的人,我很喜欢你写的程序,我想和你做朋友!可以吗?希望你能看到!!
2005年10月23日 13点10分
22