数据结构-创建双向链表

双向链表其实和单向链表是差不多的,只需要我们多注意一下prior就可以了。

#include 
#include 
#include 
typedef int ElemType ;
typedef struct Dnode{
	ElemType data ;
	struct Dnode *prior ;
	struct Dnode *next ;
}Dnode;

Dnode *createDouble(int n)
{
	Dnode *head , *p , *q ;
	int i ;
	head = (Dnode *)malloc(sizeof(Dnode)) ;
	head->prior = NULL ;
	head->next = NULL ;
	q = head ;
	for(i = 0 ; i < n ; i ++)
	{
		p = (Dnode *)malloc(sizeof(Dnode)) ;
		
		scanf("%d",&p->data) ;
		q->next = p ;
		p->prior = q ;
		p->next = NULL ;
		q = p ;
	}
	q->next = NULL ;
	
	return head ;
}

void displayDouble(Dnode *L)
{
	Dnode *p ;
	p = L->next ;
	while(p != NULL)
	{
		printf("%d\t",p->data) ;
		p = p->next ;
	}
	puts("") ;
	return ; 
}

void main()
{
	Dnode *L ;
	int num ;
	printf("Please input the num of elements\n") ;
	scanf("%d",&num) ;
	printf("Please input the elements to establish a double linked list\n") ;
	L = createDouble(num) ;
	displayDouble(L) ;
	return ;
}

你可能感兴趣的:(数据结构,链表)