c语言新建双循环链表/遍历

Node *create()
{
	DoubLink *list;
	Node *p, *pNew, *pHead;

	pHead = (Node *)malloc(sizeof(Node));
	pHead->next = NULL;
	pHead->prior = NULL;//第一个节点

	p = pHead; //指针p指向第一个节点

	for(int i = 0; i < 20; i++)
	{
		pNew = (Node *)malloc(sizeof(Node));
		pNew->data = i;

		if(p){
			p->next = pNew;
			pNew->prior = p;
			pNew->next = pHead; //新添加的节点的后驱指向第一个节点
			pHead->prior = pNew; //第一个节点的前驱指向新添加的节点
			p=pNew;	//把p指针指向新添加的节点
		}
	}

	return p;
}

遍历:

void display(Node *linkList)
{
	Node *p = linkList->next->next;

	while(p != linkList->next)
	{
		printf("%d\n", p->data);
		p = p->next;
	}
}


你可能感兴趣的:(c,链表,malloc,指针,sizeof)