Table Stack Queue
Author: Xiangguo
Contact: sunxiangguodut@qq.com
All rights reserved, no reprint
Table stack queue a normal course contents Table 50 1 Linear table 5 2 sequential table algorithm 10 3 single-linked list algorithm 30 4 double-linked list algorithm 5 5 loop link Table stack 20 1 stack basic concept 2 sequence stack 3 link stack 4 exercise test Queue 15 1 queue basic Term 2 sequential queue 3 chain queue 4 Exercises test arrays and matrices 15 1 arrays and matrices 2 exercise test data structure Total review 40
I. Normal teaching content: 1. Table 1.1 Linear table 5
Note: A linear table is a logical structure that corresponds to a storage structure that is a sequential table and a linked list.
When the elements of a linear table are ordered, they are called ordered linear tables, which are referred to as ordered tables, and ordered tables are logical structures.
Among them, the linked list is divided into single linked list/double linked list/circular link list/static linked list
single linked list
Double Linked list
Circular link List
static Linked list
Slightly
List of features :
Storage space is more expensive than sequential tables, so the storage density of sequential tables is higher. (intentionally wrong so that the linear table has a higher storage density)
Linked lists do not have random access to sequential tables, but when inserting or deleting in a linked list, you only need to modify the relevant node pointer fields, and you do not need to move the nodes.
2-1-4 linear table is stored in a linked list, it holds the cell address of each element ()
A must be continuous.
B is discontinuous.
Part C Continuous
D continuous or not all can
2-1-8 the linear table has n elements, the following operations, () on the sequential table is more efficient than on the linked list
A output value of element I
B swaps the values of the 1th and 2nd elements
C sequentially outputs the values of these n elements
D output ordinal of an element equal to the given value x in a linear table
2-1-9 for a linear table that requires the ability to quickly insert and delete operations, and requires the storage structure to reflect the logical relationship between data elements, the () storage structure should be used
A Order
B-Chain
C Hash
D Index
2-1-10 the linear table has n elements, the following operations, () on a single-linked list implementation is more efficient than on the sequential table
A deletes the next element of the specified position element
B Insert a new element after the first element
C sequential output before k elements
D Interchange the value of the first element and the N-i+1 element 1.2 algorithm of the sequential table
Note: In a linear table, the sequence number of the element AI A_i is called the logical ordinal, which is stored in the order table in data[i−1] data[i-1], and i−1 i-1 is called the physical ordinal. The array subscript in C + + starts with 0, and the logical sequence number starts from 1, which differs by 1.
#define MAXSIZE
typedef struct
{
elemtype data[maxsize];
int length;
} SqList;
Find by Element value
int Locate_elem (sqlist l,elemtype e)
{
int where=0;
while (Where<l.length && l.data[where]!=e)
where++;
if (where>=l.length)
return-1;
else
return where+1; # or return to where
}
Bitwise insertion (back)
int Insert_where (sqlist &l, elemtype e, int where)
{
int J;
for (j=l.length;j>where;j--)
l.data[j]=l.data[j-1];
l.data[where]=e;
l.length++;
return 1;
}
Bitwise DELETE
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--;
Return 1
}
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: Building a sequential table class
You did not say that the King's books have gone through it. Come and do the following question.
This is a template exercise.
2-2-13 sets N (n>1) integers into one-dimensional array R. Try to design a time and space two aspects as far as the university's algorithm, the R in the integer sequence of the loop left P (0
void reverse (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)
return 0;
Reverse (r,0,n-1);
Reverse (r,0,n-p-1);
Reverse (r,n-p,n-1);
return 1;
}
The time complexity of the algorithm is O (n) o (n) and the space complexity is O (1) O (1)
2-2-25 the two linear tables represented by the order table A and B, the number of elements is M and n respectively, if the data in the table is ascending and orderly, and this m+n data is not duplicated.
(1) design an algorithm to merge these two sequential tables into a sequential table C, the elements in C are still ascending and orderly.
(2) If the size of the order table B is m+n, do not use the sequential table C and the merged linear table is stored in order table B.
(3) Set the order table A before the M elements increment order, the latter n elements increment order, design an algorithm, so that the entire order table orderly, the space complexity is required O (1) O (1).
(4) for (3) If space complexity is not limited, can design the time complexity of O (m+n) O (m+n) algorithm. 1.3 Single-linked list algorithm
typedef struct LNODE
{
elemtype data;
struct Lnode *next;
} linklist;
Note: A single linked list is a single linked list of lead nodes if not specifically stated
Head interpolation method and tail interpolation method
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;
else return n;
}
Inserting nodes
s->next=p->next;
p->next=s;
Think: Can you turn these two words upside down?
Delete a node
p->next=p->next->next;
Note: The pointer represents the pointer to the left of the equals sign, and the address on the right
Single-linked list algorithm induction: Based on the algorithm: This type of algorithm is directly or indirectly converted into the process of building a table, if the order of the new table is the same as the original order, then the use of the tail interpolation method; If the order of the new table is the reverse of the original order, the head interpolation method is used. Algorithm based on Find/INSERT or delete; This algorithm is based on the basic operation of a single linked list, and includes a Find/INSERT or delete operation with nodes.
Part One 10-way quiz:
2-3-1 the single-linked list L of the lead node is empty, the criterion is ()
A L=null
B L->next==null
C l->next==l
D L!=null
2-3-5 in a single-linked list, the purpose of adding a head node is to ()
A make a single-linked list have at least one node
B identifies the location of important nodes in the linked list
C the realization of convenient operation
D Description Single-linked list is a chain storage structure of linear table
2-3-6 the time complexity of inserting a new node in an ordered single-linked list with n nodes and still maintaining an orderly ()
A o (1) O (1)
B o (n) o (n)
C O (n2) O (n^2)
D o (NLGN) O (NLGN)
2-3-7 The algorithm time complexity of a single-link table of length n after a single-linked list of length m is ()
2-3-8 is known that all nodes in a single-linked list of length n are ascending and orderly, and the following description is correct ()
A insert a node to make it an orderly algorithm time complexity O (1) O (1)
B Delete a node order the algorithm time complexity is O (1) O (1)
C the time complexity of the algorithm for finding the least-valued node is O (1) O (1)
None of the above is right.
2-3-9 on a single-linked table H with a leader length of N (n>1) and a tail pointer R (pointing to the tail node), the execution () operation is related to the chain table length.
A delete the first element in a single-linked list
B Delete the tail node in the single-linked list
C Insert a new node in front of the first element of the single-linked list
D insert a new node after the last element in the single-linked list
2-3-20 in a single-linked list, to delete a specified node, you must locate the node's () node
2-3-15 a node in a single-linked list that points to a non-tailed node, and the pre points to its predecessor, the process of exchanging P-referred node with its successor is:
——————————;
——————————;
——————————;
2-3-16 in a single-linked list, it is known that each node has only one data field and one pointer field next, when inserting an S-referred node before the P-point node, perform the operation:
(1) s->next=
(2) p->next=s;
(3) t=p->data;
(4) P->data=
(5) S->data=
2-3-17 in a single-linked list, it is known that each node has only one data field and one pointer field next, when you delete the node referred to by P, you should do the following:
(1) q=p->next;
(2) p->data=q->data;
(3) p->next=
(4) free (q);