Data structure Tutorial Nineth lesson cyclic link list and bidirectional linked list

Source: Internet
Author: User
Tags empty

the subject of this lesson: Circular chain list and bidirectional linked list

Teaching Purpose: grasping the concept of circular chain list, grasping the representation and realization of the bidirectional linked list

Teaching emphases: the expression and realization of bidirectional linked list

Teaching Difficulty: The storage representation of bidirectional linked list

Teaching Content:

First, review the storage structure of the linear list

Second, the storage structure of the cyclic chain list

A cyclic list is a chain-type storage structure that adds a form. Its characteristic is that the pointer field of the last node in the table points to the head node.

The operation of the cyclic chain list is basically consistent with the linear list, except that the cyclic conditions in the algorithm are not p or p->next, but whether they are equal to the head pointer.

Three, bidirectional linked list of storage structure

Question: What is the disadvantage of one-way list?

Hint: How to find the direct forward trend of the node.

Bidirectional linked list can overcome the one-way fault of single linked list.

There are two pointer fields in the node of the bidirectional linked list, one pointing to the direct successor and the other pointing directly forward.

1. Two-way linked list storage structure of linear table

typedef struct dulnode{

struct Dulnode *prior;

Elemtype data;

struct Dulnode *next;

}dulnode,*dulinklist;

For Pointer D, which points to any node in a doubly linked list, there are the following relationships:

D->next->priou=d->priou->next=d

That is, the successor of the current node is itself, and the successor of the current nodal point is itself.

2, two-way linked list delete operation

Status Listdelete_dul (dulinklist &l,int i,elemtype &e) {

if (!) ( P=getelemp_dul (L,i))

return ERROR;

e=p->data;

p->prior->next=p->next;

p->next->prior=p->pror;

Free (p);

return OK;

}//listdelete_dul

3, bidirectional linked list of insert operation

Status Listinsert_dul (dulinklist &l,int i,elemtype &e) {

if (!) ( P=getelemp_dul (L,i))

return ERROR;

if (!) ( S= (dulinklist) malloc (sizeof (Dulnode))) return ERROR;

s->data=e;

s->prior=p->prior;

p->prior->next=s;

s->next=p;

p->prior=s;

return OK;

}//listinsert_dul

四、一个 The linear side table type definition of the complete lead node:

typedef struct lnode{

Elemtype data;

struct Lnode *next;

}*link,*position;

typedef struct{

Link Head,tail;

int Len;

}linklist;

Status Makenode (Link &p,elemtype e);

Assign a node with a value of e that is pointed to by P and return OK if the allocation fails

void Freenode (Link &p);

Release P-point node

Status initlinst (linklist &l);

Construct an empty linear linked list L

Status destroylinst (linklist &l);

Destroy linear list l,l no longer exists

Status clearlist (linklist &l);

Reset the linear chain table L to an empty table and release the node space of the original list

Status Insfirst (Link h,link s);

H is known to point to the head node of the linear list, and the node of S is inserted before the first node.

Status Delfirst (Link h,link &q);

H is known to point to the head node of the linear list, delete the first node in the list and return with Q.

Status Append (linklist &l,link s);

Link the last node of the linear list L with a string of nodes that the pointer s refers to (each other as a chain of pointers)

And then change the tail pointer of the list L to point to the new tail node.

Status Remove (linklist &l,link &q);

Delete the tail node in the linear list L and return with Q, change the tail pointer of the list L to point to the new tail node

Status Insbefore (linklist &l,link &p,link s);

It is known that p points to a node in the linear chain table L and that the node of S is inserted before the node indicated by P.

and modify the pointer p to point to the newly inserted node

Status insafter (linklist &l,link &p,link s);

It is known that p points to a node in the linear chain table L, and that the node of S is inserted after the point of P.

and modify the pointer p to point to the newly inserted node

Status Setcurelem (Link &p,elemtype e);

P is known to point to a node in a linear list, and the value of the data element in the node of P is updated with E.

Elemtype Getcurelem (Link p);

It is known that p points to a node in a linear list and returns the value of the data element in the node that P refers to

Status listempty (linklist L);

Returns true if the linear list L is an empty table, otherwise returns false

int Listlength (linklist L);

Returns the number of elements in a linear list L

Position GetHead (linklist L);

Returns the position of the first node in the linear chain table L

Position getlast (linklist L);

Returns the position of the last node in the linear list L

Position priorpos (linklist l,link p);

It is known that p points to a node in the linear chain table L and returns the direct forward value of the node referred to P

If not, return null

Position nextpos (linklist l,link p);

It is known that p points to a node in the linear chain table L and returns the direct subsequent value of the node referred to P

If no successor, return NULL

Status Locatepos (linklist l,int i,link &p);

Returns the error when p indicates the position of the first node in the linear list L and returns the Ok,i value when it is not valid.

Position Locateelem (linklist l,elemtype E,
Status (*compare) (Elemtype,elemtype));

Returns the position of the 1th element in the linear list l that satisfies the compare () decision of the function.

If such an element exists, NULL is returned

Status Listtraverse (linklist l,status (*visit));

Call function visit () for each element of L in turn. Once visit () fails, the operation fails.

Summary of the contents of this lesson

The storage structure of cyclic linked list

The storage structure of bidirectional linked list

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.