Dowemo
0 0 0 0

Table stack queue

The author:

Contact: sunxiangguodut @ qq. Com.

Copyright protection

A normal class of contents:

1. Table 50.

1. 1 line table 5.

Note: the line table is a logical structure and its corresponding storage structure is sequential table and list.

When an element of a linear table is ordered, it's called an ordered list, and the ordered table is a logical structure.

Among them, linked list is divided into single linked list/loop list.

图片1.1

图片1.2

linked list

图片1.3.1

图片1.3.2

static list

Slightly

The features of the list:

The storage capacity of sequential tables is higher than that of sequential tables in storage space than order. (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, etc.

The list doesn't have the characteristics of random access in order tables, but it's only necessary to modify the key pointer fields when inserting or deleting.

In 2-1-4 line table, the cell address ( ) of each element is stored by linked list.

A must be continuous.

B isn't continuous.

C part continuous

D continuous or not.

The 2-1-8 set line table has n elements, and in the following operation, ( ) is implemented in order to achieve higher efficiency than on linked lists.

A output the first element value.

B exchange value for the first element and elements

C sequence outputs the value of the n element

D outputs the ordinal number of an element with a given value x in the line table

For a linear table, it requires that the data can be inserted and deleted quickly, and the relationship between the data elements and the data elements should be

A sequence

B chain

C hash

D index

There are n elements in the 2-1-10 table, and in the following operation, ( ) the implementation is more efficient than in the order table.

A deletes the following element of the specified location element

B inserts a new element after the first element

C elements before the output of c

D exchange the value of the first element and n I + 1 elements.

Algorithm 10 for 1. 2 sequential tables.

Note: the element in the line table. aiThe sequence number is called logical ordinal, which is stored in the order table. data[i1] In, i1 Called physical ordinal. The array subscript in c + + starts with 0, and the logical sequence number starts with 1.

#define MaxSize 50typedef struct{
 ElemType data[MaxSize];
 int length;
}sqlist;

Find by element value

int locate_elem(sqlist L,ElemType e)
{
 intwhere=0;
 while(where<L.length && L.data[where]!=e)
 where++;
 if (where>=L.length)
 return -1;
 elsereturnwhere+1; # or return where}

( back )

int insert_where(sqlist &L, ElemType e, intwhere)
{
 int j;
 for(j=L.length;j>where;j--)
 L.data[j]=L.data[j-1];
 L.data[where]=e;
 L.length++;
 return1;
}

int delete_where(sqlist &L, int i)
{
 int j;
 for (j=i;j<L.length-1;j++)
 L.data[j]=L.data[j+1];
 L.length--;
 return1}

Merging of ordered tables

void merge(sqlist L1,sqlist L2, sqlist &L3)
{
 int i=0,j=0,k=0;
 while(i<L1.length && j<L2.length)
 {
 if (L1.data[i]<L2.data[j]) {
 L3.data[k]=L1.data[i]; i++;k++;
 }
 else {
 L3.data[k]=L2.data[j]; i++;k++;
 } 
 }
 while (i<L1.length)
 {
 L3.data[k]=L1.data[i]; i++;k++;
 }
 while(j<L2.length)
 {
 L3.data[k]=L2.data[j]; j++;k++;
 }
 L3.length=k;
}

Homework: build a sequential table class

Didn't you say that the book is over again. Go ahead and do the following.

It's a template problem.

2-2-13 ( n> 1 ) integer to array R. Try to design a time and space as a algorithm, and move the integer sequence of R to p ( 0 ).

voidreverse(sqlist R[], int left, int right) //1 function{
 int k=left,j=right,temp;
 while(k<j)
 { //2 code block temp=R[k];
 R[k]=R[j]; //3 code line R[j]=temp; // k++;
 j--; 
 }
}void shift_left(sqlist R[],int n,int p)
{
 if (p<=0 || p>=n)
 return0;
 reverse(R,0,n-1);
 reverse(R,0,n-p-1);
 reverse(R,n-p,n-1);
 return1;
}

The time complexity of the algorithm is O(n) Space complexity is O(1)

In 2-2-25, two line tables represented by order table a and b, the number of elements is m and n, if the data in the table is ordered, and the data isn't repeated.

( 1 ) design an algorithm that divides both sequential tables into a sequential table c, and c elements are still ascending ordered.

( 2 ) if the size of the order table b is m + n units, don't use the sequential table c to store the merged table in order table b.

( 3 ) the ordered table a is ordered to be ordered, and then n elements are ordered and an algorithm is designed to make the whole sequential table ordered and the space complexity is O(1) .

( 4 ) if the space complexity isn't limited, it's possible to design a time complexity. O(m+n) Algorithm.

Algorithm 30 for 1. 3 single list.

typedefstruct LNode
{
 ElemType data;
 struct LNode *next; 
}linklist;

Note: if there are no special instructions, single list is a single linked list.

Head interpolation and interpolation

void creat_list_front(linklist *&head, ElemType a[],int n)
{
 linklist *s;int i;
 #head = (linklist *)malloc(sizeof(linklist));
 linklist* head=new(linklist);
 head->next =NULL;
 for (i = 0; i<n;i++)
 {
 linklist* s=new(linklist);
 s->data = a[i];
 s->next = head->next;
 head->next = s;
 } 
}
void creat_list_rear(linklist *&head,ElemType a[], int n)
{
 linklist *s,*r;int i;
 linklist* head = new(linklist);
 r=L;
 for (i =0;i<n;i++)
 {
 linklist* s=new(linklist);
 s->data = a[i];
 r->next = s;
 r=s;
 }
 r->next =NULL;
}

Find by element value

int find_ele(linklist *L,ElemType e)
{
 linklist *p=L->next;
 int n=1;
 while(p!=NULL && p->data!=e)
 {
 p=p->next;
 n++;
 }
 if (p==NULL)return -1;
 elsereturn n;
}

Insert node

s->next=p->next;
p->next=s;

图片4

Think: can you reverse the two sentences.

Delete node

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

Note: the pointer indicates the pointer on the left side of the pointer, and the address on the right.

图片5

Single list algorithm induction:

  • algorithms: This kind of algorithm directly or indirectly converted to the process of building the table, if the order of the new table is the same as the original order, If the order of the new table is opposite to the original order, the head interpolation is used.
  • Algorithm based on find/insert or delete; This kind of algorithm is based on the basic operation of a single list, including the find/insert or delete operation.

Test part one 10 test:

A decision condition for the single list l of 2-3-1 head node is ( )

A l = null

B l-> next = = null

C l-> next = = l

D l! = null

In a single linked list, the purpose of increasing the header node is to ( ).

A single list has at least one node.

B identifies the location of important nodes in a list

Implementation of c convenient operation

D description single list is the chain storage structure of.

2-3-6 inserts a new node in an ordered list with n nodes and still maintains an ordered time complexity ( ).

A O(1)

B O(n)

C O(n2)

D O(nlgn)

In 2-3-7, the algorithm time complexity is ( ) after the length of a single linked list linked with length m.

All of the nodes in 2-3-8 known a single list of length n are ordered to be ordered, and the following is correct.

A is inserted into a node to order the algorithm time complexity. O(1)

B deletes a node to order the algorithm time complexity. O(1)

The time complexity of the algorithm to find the minimum node of c O(1)

I don't have more than that.

In a 2-3-9 with a length n ( n> 1 ), is provided with a tail pointer R ( pointing to the tail node ), which is related to the list length

A delete the first element in a single list.

B delete the tail node in single list

C inserts a new node before the first element of a single list

D inserts a new node after the last element of a single list

A 2-3-20 in a single list, to delete a specified node, must find the node ( ) node.

In 2-3-15 single list, p points to a node of a node, and the node is exchanged with the node. The process of switching the node of p to its subsequent node is:

A ----------;

A ----------;

A ----------;

In a single linked list, you know that each node has only one data field data and a pointer domain, which can be done when a s node is inserted before the p

( 1 ) s-> next =

( 2 ) p-> next = s;

( 3 ) t = p-> data;

( 4 ) p-> data =

( 5 ) s-> data =

In a single linked list, you know that each node has only one data field data and a pointer domain, and when you delete a p point node, you should do the following:

( 1 ) q = p-> next;

( 2 ) p-> data = q-> data;

( 3 ) p-> next =

( 4 ) free ( q );

Big questions part two 2.

Two integer sequences 2-3-48 A=(a1,a2,,am) And and B=(b1,b2,,bn) In addition to two single lists, design an algorithm that determines whether the sequence b is a subsequence of sequence a.

In 2-3-47, the nodes in are known to be ordered by an ordered array of elements. ( two nodes with the same values may exist ), and an algorithm is designed for the following: Leave the list of the data elements that are contained in the table a in the operation, and don't have a node with the same value, and release all useless nodes.

Algorithm 5 for 1. 4 double linked lists.

typedefstruct dlinklist
{
 ElemType data;
 struct dlinklist *prior;
 struct dlinklist *next;
}dlinklist;

I & ert a node:

Insert node s after p

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

When a new node is inserted before a single linked node p, we need to find the precursor node of the p node. When a node p is inserted in a list, only the p node can be found to implement the insertion operation.

Delete node

Delete the subsequent nodes of a p node

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

When removing a p node in a single list, we need to find the precursor node of the p node, while removing the node p in the double list, we only need to find the p.

In a list, the operation of the * q node after the * p node is 2-4-1.

A. Q-> prior = p; p-> next = q; p-> next-> front = q; q-> = p- next.

B. Q-> next = p-> next; p-> next-> face = q; p- next = q; q- min = p.

C. P-> next = q; q-> prior = p; q-> next = p-> next; p- next-> = q.

D. P-> next-> prior = q; q-> next = p-> next; q- prior = p; trace> = 1.

In a list, the operation of the q node before the * p node is ( )

A. P-> prior = q; q-> next = p; p-> prior-> next = q; q- min = p- prior to.

B. Q-> prior = p_prior; p-> prior-> next = q; q-> nu = p; p- prior = q-.

C. Q-> next = p; p-> next = q; q-> prior-> next = q; q- next = p.

D. P-> prior-> next = q; q-> next = p; q-> prior = p->; p- prior = q.

1. 5 circular list.

In the loop list, the condition of> is determined by next = = l ( l head node ).

Other operations are similar to previous lists

2. Stack 20.

Basic concepts of 2. 1 stack.

With n elements, their number is 1 ~ n, the sequential entry into a stack, and the possible stack sequence ( ) is possible.

The following three types of problems are equivalent:

  • Number of elements of the n element
  • The number of binary trees formed by n different elements
  • The first order sequence of n different elements form the number of binary trees in different forms

A stack sequence of 3-1-8 known a stack is 1, 2, 3. N, the first element of the output sequence is I ( 1 <= I <= n ), and the ( 1 <= j <= n ) element is ( ).

A i

B n-i.

C j-i + 1

D isn't sure.

2. 2 sequential stack.

2. 3 chain stack.

2. 4 exercise tests.

15, 10 minutes

3. Queue 15.

3. 1 queue basic terms.

3. 2 sequential queue.

3. 3 chain queue.

3. 4 exercise tests.

15 questions, 10 minutes

4, array and matrix 15

4. 1 arrays and matrices.

4. 2 exercise tests.

5. Total data structure review 40.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs