"Data structure second week" Knowledge point collation of linear table

Source: Internet
Author: User

1. What is a linear table?

Linear table (Linear list): A linear structure consisting of an ordered sequence of elements of the same type.

The number of elements in a table is called the length of a linear table

When a linear table has no elements, it is called an empty table

Table start position is called Table header, table end position is known as footer

2. Abstract data type description of linear table

List Makeempty (): Initializes an empty linear table l;
ElementType findkth (int k, list L): returns the corresponding element according to the position order K; int find (ElementType X, List L): Finds the first occurrence of X in linear table L; void Insert (Elemen Ttype x, int i, List L): Inserts a new element X before the order I; void Delete (int i, list L): Deletes the element that refers to the position order I; int length (list L): Returns the length of the linear table L N.

3, the main implementation mode: Sequential storage implementation and chain storage implementation

4. Sequential storage implementation of linear tables

(1) How to store

How to store typedef struct {ElementType data[maxsize];int last;} List; List L,*ptrl;

To access the element labeled I: L.data[i] or ptrl->data[i]

Length of linear table: L.last+1 or Ptrl->last+1

(2) Initialize (Create empty order table)

List *makeempty () {list *ptrl; Ptrl = (list *) malloc (sizeof (list)); Ptrl->last = -1;return Ptrl;}

(3) Find

int Find (ElementType X, List *ptrl) {int i = 0;while (i <= ptrl->last && ptrl->data[i]!=x) i++;if (i > Pt Rl->last) {return-1;} Else{return i;}}

The average number of lookup successes is (n+1)/2 (the first comparison is found or the last comparison is found), and the average time performance is O (n).

(4) Insert (1<=I<=N+1) insert a new element with a value of X

Insert void Insert (ElementType X, int i,list *ptrl) {int j;if (ptrl->last==maxsize-1) {printf ("table full"); return;} if (I < 1 | | ptrl->last+2) {printf ("Location not valid"); return;} for (j = ptrl->last; J >= i-1;--j) {ptrl->data[j+1]=ptrl->data[j];//moves ai~an backwards}ptrl->data[i-1] = X;// Insert new element Ptrl->last++;//last still point to last element return;}

The average number of moves is N/2 and the average time performance is O (n).

(5) Delete

  

delete void Delete (int i,list *ptrl) {int j;if (i < 1 | | I >ptrl->last+1) {printf ("No%d elements", i); Return;}for (j = i; J <= ptrl->last;++j) {ptrl->data[j-1]=ptrl->data[j];//moves the ai+1~an sequence forward}ptrl->last--;return;}

The average number of moves is (n-1)/2, and the average time performance is O (n).

5, linear table of the chain-storage implementation

The logically adjacent two elements are not required to be physically contiguous, and the logical relationship between the data elements is established through the chain.

Inserting, deleting does not need to move data element, only need to modify "chain".

(1) How to store

How to store typedef struct NODE{ELEMENTTYPE data;struct Node *next;} List; List L,*ptrl;

(2) The length of the table

The table length int length (list *ptrl) {List *p = ptrl;//p points to the first node of the table int j = 0;while (p) {p = p->next;//the current p points to the J-node, j + +;} return J;}

(3) Find

Search by ordinal: findkth

List *findkth (int k,list *ptrl) {List *p = Ptrl;int i = 1;while (P! = Null&&i < K) {p = p->next;i++;} if (i==k) {return p;//find K, return pointer}else{return NULL;}}

Find by Value: Find

List *find (ElementType x,list *ptrl) {List *p = Ptrl;while (P! = NULL && P->data! = X) {p = P->next;} return p;}

(4) Insert

List *insert (ElementType X, int i, list *ptrl) {List *p,*s;if (i = = 1)//new node inserted in table header {s = (list *) malloc (sizeof (list)); S->dat A = X;s->next = Ptrl;return s;} p = findkth (I-1,ptrl), if (p = = NULL) {printf ("parameter I Error"),///The i-1 element does not exist, you cannot insert a return NULL;} Else{s = (list *) malloc (sizeof (list)); s->data = X;s->next = p->next;//The new node is inserted after the i-1 node P->next = S;return Ptrl;}}

(5) Delete (delete nodes in the list I (1<=i<=n))

List *delete (int i,list *ptrl) {List *p,*s;if (i = = 1)//deleted node is the first node {s = ptrl;//s points to the first node if (ptrl!=null) {Ptrl = Ptrl->next ;//Remove}else{return NULL from the list;} Free (s); return Ptrl;} P = findkth (i-1, Ptrl);//Find I-1 node if (p = = NULL) {printf ("%d nodes not present", i-1); return NULL;} else if (p->next==null) {printf ("%d nodes not present", I); return NULL;} Else{s = p->next;//s points to the i node P->next = s->next;//Remove free (s) return Ptrl from the list;}}

"Data structure second week" Knowledge point collation of linear table

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.