Esempio n. 1
0
int main(int argc, const char *argv[])
{
	int i = 0;
	DLinkList *list = DLinkList_Create();
	struct Value *pv = NULL;

	struct Value v1;
	struct Value v2;
	struct Value v3;
	struct Value v4;
	struct Value v5;

	v1.v = 1;
	v2.v = 2;
	v3.v = 3;
	v4.v = 4;
	v5.v = 5;

	DLinkList_Insert(list,(DLinkListNode *)&v1,DLinkList_Length(list));
	DLinkList_Insert(list,(DLinkListNode *)&v2,DLinkList_Length(list));
	DLinkList_Insert(list,(DLinkListNode *)&v3,DLinkList_Length(list));
	DLinkList_Insert(list,(DLinkListNode *)&v4,DLinkList_Length(list));
	DLinkList_Insert(list,(DLinkListNode *)&v5,DLinkList_Length(list));

	for(i = 0; i < DLinkList_Length(list); i++)
	{
		pv = (struct Value *)DLinkList_Get(list, i);

		printf("%d \n", pv->v);
	}

	printf("\n");

	DLinkList_Delete(list, DLinkList_Length(list)-1);
	DLinkList_Delete(list, 0);

	for(i = 0; i < DLinkList_Length(list); i++)
	{
		pv = (struct Value *)DLinkList_Next(list);

		printf("%d \n", pv->v);
	}

	printf("\n");

	DLinkList_Reset(list);

	DLinkList_Next(list);
	pv = (struct Value *)DLinkList_Current(list);
	printf("%d \n", pv->v);

	DLinkList_DeleteNode(list, (DLinkListNode *)pv);
	pv = (struct Value *)DLinkList_Current(list);
	printf("%d \n", pv->v);

	DLinkList_Pre(list);
	pv = (struct Value *)DLinkList_Current(list);
	printf("%d \n", pv->v);

	printf("Length: %d \n", DLinkList_Length(list));

	DLinkList_Destroy(list);
	
	return 0;
}
Esempio n. 2
0
void play()
{
	Student s[maxn];
	for (int i = 0; i < maxn; ++i) {
		s[i].age = i + 21;
	}

	DLinkList *list = NULL;
	list = DLinkList_Create(); // 创建链表

	// 插入结点
	for (int i = 0; i < maxn; ++i) {
		int ret = DLinkList_Insert(list, (DLinkListNode *)&s[i], DLinkList_Length(list));
		if (ret < 0) {
			return;
			printf("function DLinkList_Insert err.\n");
		}
	}

	// 遍历链表
	for (int i = 0; i < DLinkList_Length(list); ++i) {
		Student *tmp = (Student *)DLinkList_Get(list, i);
		if (tmp == NULL) {
			printf("function DLinkList_Get err.\n");
			return;
		}
		printf("age: %d\n", tmp->age);
	}

	DLinkList_Delete(list, DLinkList_Length(list) - 1); // 删除尾结点
	DLinkList_Delete(list, 0); // 删除头结点

	// 用游标遍历链表
	for (int i = 0; i < DLinkList_Length(list); ++i) {
		Student *tmp = (Student *)DLinkList_Next(list);
		if (tmp == NULL) {
			printf("function DLinkList_Next err.\n");
			return;
		}
		printf("age: %d\n", tmp->age);
	}

	printf("\n");

	DLinkList_Reset(list);
	DLinkList_Next(list);

	Student *tmp = (Student *)DLinkList_Current(list);
	if (tmp == NULL) {
		printf("function DLinkList_Current err.\n");
		return;
	}
	printf("age: %d\n", tmp->age);

	DLinkList_DeleteNode(list, (DLinkListNode*)tmp);
	tmp = (Student *)DLinkList_Current(list);
	if (tmp == NULL) {
		printf("function DLinkList_Current err.\n");
		return;
	}
	printf("age: %d\n", tmp->age);
	printf("length: %d\n", DLinkList_Length(list));

	DLinkList_Pre(list);
	tmp = (Student *)DLinkList_Current(list);
	if (tmp == NULL) {
		printf("function DLinkList_Current err.\n");
		return;
	}
	printf("age: %d\n", tmp->age);

	printf("length: %d\n", DLinkList_Length(list));
	DLinkList_Destroy(list);

	return;
}