Data Structure Tutorial Eighth lesson the chain representation and realization of linear table

Source: Internet
Author: User

The subject of this lesson: chain representation and implementation of linear table

Teaching Objective: To master the concept, representation and implementation of linear linked list, single linked list and static linked list

Teaching emphases: the expression and realization method of the single linked list of the linear list.

Teaching Difficulty: The concept of linear linked list.

Teaching Content:

First, review the definition of the sequence table.

Second, the concept of linear linked list:

A linear table stored in a chain structure is called a linear chain list.

The characteristic is that the data elements in the linear table can be stored in any storage unit. The storage space of two elements that are logically adjacent to a linear table can be discontinuous. To represent a logically sequential relationship, each data element of a table, in addition to storing its own information, needs to store a message indicating its immediate inheritance. These two parts of information constitute the storage image of the data element, called the node.

2000:1000
2000:1010
2000:1020
2000:1030
2000:1040
2000:1050
2000:1060
...
2000:4,000
Head pointer 2000:1006 2000:1030
A3 2000:1040
A6 Null
A1 2000:1060
A4 2000:1050
A5 2000:1020
A2 2000:1010
Data fields Pointer field
<-data field + pointer field

Example: The following picture is a number of drawers, each drawer has a data element and a pointer to the successor, a drawer with the first element of the linear table, and the next to the second element is labeled 5, which is placed in the 5th drawer, and the third in drawer 2nd. The third element is the last one, and its next element has a null pointer, which is represented by 0.

When a linear table is represented by a linear list, the logical relationship between the data elements is indicated by the pointers in the nodes.

Second, the realization of the storage of linear list

struct lnode{

Elemtype data;

struct Lnode *next;

};

typedef struct LNODE Lnode;

typedef struct LNODE * linklist;

The difference between a head pointer and a head node:

The head pointer is only the pointer field of the node, the first node is the whole linear list, its data field can put the data elements, and can put the length of the linear table and other information, can not store any information.

Operation Realization of linear table (Class C language)

1 initialization operation

Status init_l (linklist L) {

if (l= (linklist *) malloc (sizeof (Lnode))

{L->next=null;return 1;}

else return 0;

}

2 Insert operation

Status listinsert_l (linklist &l,int i,elemtype e) {

p=l,j=0;

while (p&&j<i-1) {p=p->next;++j}

if (!p| | J>I-1) return ERROR;

S= (linklist) malloc (sizeof (Lnode));

s->data=e;s->next=p->next;

p->next=s;

return OK;

}//listinsert_l

3 delete operation

Status listdelete_l (linklist &l,int i,elemtype &e) {

p=l,j=0;

while (p&&j<i-1) {p=p->next;++j}

if (!p->next| | J>I-1) return ERROR;

q=p->next;p->next=q->next;

E=q->data;free (q);

return OK;

}//listdelete_l

4 An operation that takes an ordinal element

Status getelem_l (linklist &l,int i,elemtype &e) {

p=l->next,j=1;

while (p&&j<i) {p=p->next;++j}

if (!p| | J>i) return ERROR;

e=p->data;

return OK;

}//getelem_l

5 algorithm for merging two single linked lists

void mergelist_l (linklist &la,linklist &lb,linklist &lc) {

Elements of the single chain linear table LA and LB are known to be in a non descending order of value

After merging, the new single chain linear table LC is obtained, and the elements are arranged by value not descending

pa=la->next;pb=lb->next;

Lc=pc=la;

while (PA&&PB) {

if (pa->data<=pb->data) {

pc->next=pa;pc=pa;pa=pa->next;

}else{pc->next=pb;pc=pb;pb=pb->next;}

}

pc->next=pa?pa:pb;

Free (LB);

}//mergelist_l

The example of C language implementation.

Iv. Summary

1, the concept of linear linked list.

2, the linear list of storage

3, the linear list of 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.