Bidirectional cyclic linked list operations

Source: Internet
Author: User

Double Linked list:
1 2 3 4 5 6 7 8 9 10 11 12

1 3 5 7 9 11 12 10 8 6 4 2
1. Design node
typedef int DATATYPED
TYPEDDEF struct node
{
Typedata data;
struct node * NEXT;
struct node *prev;

}listnode, *linklist;


2. Initializing an empty bidirectional loop list
Linklist init_list ()
{
Linklist l=malloc (sizeof (ListNode))
if (l!=null)
{

l->next=l;
l->prev=l;
}
return l;

}

3. Insert
BOOL Insert_prev (linklist new,linklist L)
{
New node-to-front relationship
New->n=l
new->p=l-p;


Back relationship of the previous node of a new node
l->p->n=new;
The front relationship of the next node of the new node
l->p=new;

return true;

}

4. Display
Show ()
{

}
Remove_node (linklist PL)
{
pl->p->n=pl->n;
pl->n->p=pl->p;
pl->p=pl->n=null;


}
Move_p (linklist pl,linklist L)
{
Remove_node (PL);
Insert_prev (pl,l);

}

5.
Ran (linklist L)
{
Linklist pl=l->p,q;

int i=0;
while (PL!=L)
{
if ((pl->data)%2==0 && i==1)
{
Delete and insert operations
Move_p (pl,l);
pl=q;
}
Else
Q=PL;

pl=pl->p;

I=1;
}


}


int main ()
{
Linlist l=init_list ();

for (i=1;i<=n;i++)
{
Linklist new =malloc ();
new->data=i;

Insert_prev (new,l);

}


Ran (L)

Show (L)

}

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int datatype;
typedef struct NODE
{
datatype data;
struct node *next;
struct node *prve;
}listnode,*linklist;

Linklist init_list ()
{
linklist L = malloc (sizeof (ListNode));
if (l!=null)
{
L->next = L;
L->prve = L;
}
return L;
}

BOOL Insert_prve (linklist new,linklist L)//Insert node in the precursor of L
{
New->next = L;
New->prve = l->prve;
L->prve->next = new;
L->prve = new;
return true;

}
/*while (Q->prve! = q)
{

}
*/
BOOL Cir_insert (linklist L)//display odd-even display 1 3 5 7 9 10 8 6 4 2
{
Linklist p=l->prve,q;
s = l->next;
while (P! = L)//Pay attention to the loop condition
{

if (p->data%2==0)
{q=p->prve;//records the precursor of P so that the P-pointer moves forward
P->prve->next = p->next;
P->next->prve = p->prve;
P->prve = l->prve;
L->prve->next = p;
P->next = L;
L->prve = p;
A pointer that was previously recorded by p=q;//.

}
p=p->prve;

}
return true;
}


void Show (Linklist L)//print cycle linked list data
{linklist P =l;
while (P->NEXT!=L)
{p = p->next;
printf ("%d\t", p->data);
}
printf ("\ n");

}
int main ()
{
Linklist L;
L = Init_list ();
int i;
for (i=1;i<=10;i++)
{
linklist new = malloc (sizeof (ListNode));
New->data = i;
Insert_prve (new,l);
}
Cir_insert (L);
Show (L);
return 0;

}

Bidirectional cyclic linked list operations

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.