【程序员面试宝典】数据结构基础二单链表循环链表的操作 建立|插入删除|打印|合并

#include
#include
typedef struct dlinks
{
	int data;
	struct dlinks *pre;
	struct dlinks *next;
}dnode;

dnode *Create()
{
	dnode *head=(dnode*)malloc(sizeof(dnode));
	head->pre=NULL;
	dnode *s,*p=head;
	int data,cycle=1;
	while(cycle)
	{
		scanf("%d",&data);
		if(data)
		{
			s=(dnode*)malloc(sizeof(dnode));
			s->data=data;
			p->next=s;
			s->pre=p;
			s->next=NULL;
			p=p->next;
		}
		else
			cycle=0;
	}
	return head;	
}
void PRINT(dnode *link)
{
	dnode *p=link->next;
	while(p)
	{
		printf(" %d ",p->data);
		p=p->next;
	}
	printf("\n");
}

dnode *insert(dnode *link,int data,int n)
{
	dnode *p=link;
	int i=0;
	while(inext;
		i++;
	}
	dnode *s=(dnode *)malloc(sizeof(dnode));
	s->data=data;
	s->next=p->next;
	s->pre=p;
	p->next=s;
	return link;
	
}
dnode *deletenode(dnode *link,int n)
{
	dnode *p=link;
	int i=0;
	while(inext;
	}
	dnode *s=p->next;
	s->next->pre=p;
	p->next=s->next;
	free(s);
	return link;
}
dnode* MergeLinks(dnode *Linka,dnode *Linkb)
{
	dnode *p=Linka->next;
	dnode *q=Linkb->next;
	dnode *Linkc=Linka;
	dnode *s=Linkc;
	s->pre=NULL;
	while(p&&q)
	{
		if(p->datadata)
		{
			s->next=p;
			p->pre=s;
			p=p->next;
		}
		else
		{
			s->next=q;
			q->pre=s;
			q=q->next;
		}
		s=s->next;
	}
	if(p)
	{
		s->next=p;p->pre=s;
	}
	if(q)
	{
		s->next=q;q->pre=s;
	}
	return Linkc;
}

int main()
{
// 	dnode *dlinklist;
// 	dlinklist=Create();
// 	dlinklist=insert(dlinklist,9,3);
// 	PRINT(dlinklist);
// 	dlinklist=deletenode(dlinklist,5);
// 	PRINT(dlinklist);
	dnode *link1=Create();
	PRINT(link1);
	dnode *link2=Create();
	PRINT(link2);
	dnode *link3=MergeLinks(link1,link2);
	PRINT(link3);
	return 0;
}


你可能感兴趣的:(C语言的世界遨游)