예제 #1
0
int Joseph(int n, int m ){//总人数和报数的上限
    LinkList L;
    initLinkList(&L);
    autoCreateLinkList(&L, n);

    ListNode *p, *q;
    p = L;
    while(p->next){
        p = p->next;
    }
    p->next = L->next;
    p = L;

    while(p->next != p){
        for(int i = 1; i < m; ++i){
            p = p->next;
        }
        q = p->next;
        p->next = q->next;
        printf("%d -> ", q->content);
    }
    printf("%d", p->content);

    printf("\n");
    return 1;
}
예제 #2
0
// 主函数-用于测试
int main() {
    // 初始化链表
    linkList list = initLinkList();
    // 插入三个元素
    inserts(&list, 333);
    inserts(&list, 222);
    inserts(&list, 2222);

    // 遍历链表
    lists(&list);

    // 求列表中元素个数
    printf("size = %d\n", list.size);


    // 获取列表中的元素
    printf("value = %d\n", getElement(&list, 0));
    printf("value = %d\n", getElement(&list, 1));
    printf("value = %d\n", getElement(&list, 2));

    // 删除列表中的元素
    deletes(&list, 2);

    printf("value = %d\n", getElement(&list, 0));
    printf("value = %d\n", getElement(&list, 1));
    // 查看最终列表中元素个数
    printf("size = %d\n", list.size);
}
예제 #3
0
int main(int argc, const char *argv[])
{
    LinkList La;
    LinkList Lb;
    LinkList L;

    int lenA, lenB, positionAtA;
    printf("Input the length of A:\n");
    scanf("%d", &lenA);
    printf("\nInput the length of B:\n");
    scanf("%d", &lenB);
    printf("\nInput the position at A to insert B: ");
    scanf("%d", &positionAtA);



    autoCreateLinkList(&La, lenA);
    autoCreateLinkList(&Lb, lenB);

    crossTwoLinkList(La, Lb, positionAtA);


    initLinkList(&L);
    L->next = checkIfCross(La, Lb);

    if(L != NULL){
        printLinkList(L);
    }else{
        printf("None\n");
    }

    return 0;
}
예제 #4
0
main()
{
	
	/* variable: studentList    -> curr list
	 *           student        -> as a tmp variable to save curr info
	 *           commands       -> combine command you want to exe
	 *           id             -> analyze id of student in cmd
	 *           name           -> analyze name of student in cmd
	 *           cmd            -> analyze sub command in commande,split with  
	 *           flags          -> analyze command
	 */
	tLinkList studentList;
	tStudent student;
	char commands[1000],id[10],name[20], *cmd,flag;
	int pos;

	/* init list with my self define function then print program UI*/
	initLinkList(&studentList,dataCmp,handle);
	printUI();

	/* loop to deal with commands*/
	while(TRUE)
	{
		scanf("%s",commands);
		cmd=strtok(commands," ");
		/* loop to deal with command, split with  */
		do
		{
			/*analyze curr command to get id and name*/
			flag=cmd[0];
			if(strchr(cmd,',') !=NULL)
			{
					pos=strchr(cmd,',')-cmd;
					strncpy(id,cmd+2,pos-1);
					id[pos-2]='\0';
					strcpy(name,cmd+pos+1);
					name[strlen(name)-1]='\0';
					student.id = atoi(id);
					strcpy(student.name,name);
			}
			else
			{
					strcpy(id,cmd+2);
					id[strlen(id)-1]='\0';
					student.id = atoi(id);				
			}

			/* both upper and lower can be executed*/
			switch(toupper(flag))
			{
				case 'I' :
					insertData(&studentList,&student,sizeof(tStudent));
					break;
				case 'D' :
					if(deleteData(&studentList,&student)==SUCCESS)
						printf("%s deletes successfully!\n",id);
					else
						printf("%s not exists in the list!\n",id);
					break;				
				case 'U' :
					updateData(&studentList,&student,&student.id,sizeof(tStudent));
					break;				
				case 'Q' :
					/* query and print the data*/
					handle(queryData(&studentList,&student.id));
					break;				
				case 'P' :
					iterativeLinkList(&studentList);
					break;
				case 'C' :
					system("cls");
					printUI();
					break;
				case 'E' :
					exit(0);
					break;
				default:
					printf("command error ,please try again !\n");
			}
			cmd=strtok(NULL," ");
		}while(cmd != NULL);
	}
	system("pause");
}