Create a doubly linked list or two-way circular linked list

Source: Internet
Author: User
#include <stdio.h>
#include <stdlib.h>
#define Len sizeof (struct list)
struct list
{
	int x;
	struct list *pre,*next;
};
struct list * Create ()//Create a linked list and return a pointer to the chain header
{
	struct list *p,*p1,*head;
	head=p= (struct list *) malloc (len);
	P->PRE=NULL;//1. The first element does not have a direct precursor
	scanf ("%d", &p->x);
	int n=0;
	while (P->x!=-1)
	{
		if (n==0)
			n++;
		else//understand clearly. Suggest drawing on the paper
			p1->next=p,p->pre=p1;
		p1=p;
		p= (struct list *) malloc (len);
		scanf ("%d", &p->x);
	}
	P1->next=null;//2. The last element has no direct successor
//	p1->next=head,head->pre=p1;//If 1 2 lines of code is deleted so that's a two-way loop list.
	return head;
}
int main ()
{
	struct list *l1;
	printf ("Please enter the list A, end with-1:");
	L1=create ();
	printf ("\ n");
	while (L1!=null)
	{
		printf ("%d", l1->x);
		l1=l1->next;
		if (l1!=null)
		printf ("%d", l1->pre->x);
	}
	printf ("\ n");
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.