Data Structures and Algorithms Problem 1

 

Problem 1.

Array Q [n] is used to represent a circular queue, front previous location of the head elements, rear location of the tail element, calculating the number of elements in the queue for the formula   (REAR-n-Front +) n-%   .

Circular queue, the result may be a rear-front-negative integer, while a negative integers modulo the results may vary in different compiler environment.

 

 

Problem 2.

Stacks and queues are common feature  only allows endpoints insert and delete elements   .

 

 

Problem 3. 

A symmetric matrix of n × n, column-priority compressed storage, the storage capacity of  n-* (n-+. 1) / 2  .

For symmetric matrices, the row and column-priority, the storage capacity is the same, 1 + 2 + ... + n = n (n + 1) / 2.

 

Problem 4.

A linked list represents the advantages of linear form is convenient to insert and remove , represented by circular list the main advantages is the linear form   from the table any node can access the entire list .  

 

 

Problem 5.

For the linear form of n elements, the establishment of a single list the time complexity is O (n) .

 

Problem 6.

In a single linked list, known q refer immediate predecessor node is the indicated node p, when inserted between the node pointed to by s p and q, it is necessary to do the following:  Q-> Next = s; S-> Next = P; .

 

Problem 7.

The stack into the stack a known sequence 1,2,3, ..., n, which is the output sequence p1, p2, ..., pn, if p1 = n, the value of pi is  n--I +. 1  .

When p1 = n, the output sequence is unique, i.e. n, n-1, ..., 2,1, then p2 = n-1, ..., pn = 1, inferred pi = n-i + 1.

 

Problem 8.

To set target string s = "abcabababaab", the pattern string is p = "babab", the KMP pattern matching algorithm for the next array {-1,0,0,1,2}  .

next pattern string array with only about p, the calculation process is as follows:

j

0

1

2

3

4

p

b

a

b

a

b

next[j]

-1

0

0

1

2

 

Problem 9.

In a single linked list, the first node increases purpose is   to facilitate the realization of operation  .

 

 

Problem 10.

The purpose of the analysis algorithm is  analyzed in order to improve the efficiency of the algorithm .

 

 

Problem 11.

Given a list, the list is determined whether there is a ring list.

If the list has a ring, so when traversing the list will be caught in an endless loop, take advantage of this feature, we can design such algorithms.

1. Use a slow pointer, a pointer fast.

2. slow pointer back again traversed to a node, a next pointer FAST traverse two nodes, such an operation has been done. If there is a ring, it is certainly fast prior to or simultaneously slow to enter the ring, after entering as fast and slow, but not out of the loop, so there are two rings must meet. No ring fast-> next = End NULL.

struct Link 
{ 
    int Data; 
    Link * Next; 
} 

BOOL isLoop (Link * head) 
{ 
    // SLOW, FAST for traversing the list 
    Link head * = SLOW, FAST * = head; 
    
    // if the chain length of <3, the ring is not chain type 
    IF (head == NULL || head-> Next == NULL) 
    { 
        return  to false ; 
    } 
    
    // else chain length> = 3, traversing the list 
    do {
         // SLOW time through a node 
        SLOW = slow-> Next
         // FAST time through two nodes 
        FAST = FaST - PAK> next-> Next;
     //After traversing the list or slow pointer and fast pointer exits the loop when meet 
    } the while (fast && FaST - PAK> Next && slow =! Fast) 
    
    // if slow pointer and fast pointers meet, there is a ring 
    IF (slow == fast)
         return  to true ;
     // otherwise described went to the end node, loop does not exist 
    the else  
        return  to false ; 
}

 

12. The single list of elements known to each node is an integer and is incremented ordered, algorithm design "DeleteBetween" delete list mink greater than and less than all of the elements maxk, deleted nodes and release storage space.

Since operating on a single ordered list, therefore, to take full advantage of its ordering.

Find the first node is greater than a mink and a node maxk less than in a single linked list, then all the nodes between the two deleted.

DeleteBetween (the Node <T> * First, int mink, int maxK) 
{ 
    P = First;
     // traverse to the last node mink less than 
    the while (p-> Next = NULL && p-> next-> Data <=! Mink ) 
        P = p-> Next;
     // q points to the first node is greater than mink 
    IF ! (p-> Next = NULL) 
        q = p-> Next;
     // if q-> data is smaller than the mask, to be off q chain of     
    the while (Q = NULL && Q-> Data <! maxK) 
    { 
        U = Q-> Next; 
        P -> Next = U; 
        Delete Q; 
        Q= U; 
    } 
}

Guess you like

Origin www.cnblogs.com/antarctic/p/12182623.html