DS blog work 01- linear table

0.PTA score Screenshot

1.1 linear table learning summary

1. The basic concept of the linear form

Linear list is a finite sequence of n having the same characteristics, all the elements belong to the same data type. The logical structure of a simple linear form, and facilitate the operation, a wide range of use. A storage can be divided into: the sequence table and the linked list.

2. order table

The data is sequentially stored in a contiguous block of physical space, each memory cell addresses between nodes is continuous.

Structure is defined

typedef struct node{
    ElemType data[MAX];
    int length;//保存线性表的长度,或者也可以定义int型变量last用于保存线性表中最后一个数据所在的位置下标。
}Node,*NodeList;

Basic Operations

  • Insertion sequence table : essentially operate the mobile array traverse from scratch, locate the insert in position, the array starts moving or while moving from the end side comparison array, to find the insertion position, directly inserted;

    • Fake code:
    void InserList(NODE & list,int x)
    {
      判断顺序表是否满了,如果满了就return
      for i=list.length to 0  //从末尾开始一个一个把数据右移,直到找到x的插入位置停止。
          if  x>list.data[i-1]  //找到插入位置,退出循环;
              break;
          else
              list.data[i]=list.data[i-1];//数据进行右移;
          end if
      end for
      list.length++;//一定要记得修改顺序表的长度!
      list.data[i]=x;//插入数据;
    }
    • Specific code and achieve the results:
  • Deletion order table : Mobile data array is substantially in, a method generally used is reconstructed sequence table.

    • Fake code:
    void DeleteData(NODE &list, int x)
    {
     定义变量i来遍历顺序表;
     定义变量j并初始化为0;
     定义变量flag用于判断删除是否成功,初始化为0;
    
     for i=0 to list.length
        if(list.data[i]!=x)//不是删除的数据x就存入顺序表中;
            list.data[j++]=list.data[i];
        else 
            flag=1;
        end if
     end for
     list.length=j;
     判断是否删除成功。
    }
    • Specific code and its results:

3. singly linked list

Dispersing data stored in a physical space, the address of the memory cell between the respective nodes is not necessarily continuous, which is a logical relationship pointer increases next, by establishing a chain pointer to save the data.

Structure is defined

typedef struct node
{
    Elemtype data;
    struct node* next;
}Node,*ListNode;

Basic Operations

  • The first interpolation method to create a single chain : node list order opposite to the logical order.
    • Fake code:
    void GreatList(ListNode&head,int n)
    {
      head=new Node;//为list申请空间;
      head->next=NULL;
      定义结构体指针ptr来保存每一个结点;
    
      for i=0 to n
          ptr=new Node;//为ptr申请空间;  
          输入数据赋给ptr->data;
          ptr->next保存头结点head的下一个结点;
          head->next保存ptr;
      end for
    }
    • Specific code and achieve the results:
  • Interpolation tail established chain : junction sequence and the same sequence as the logical linked list.

    • Fake code:
    void GreatList(ListNode& head,int n)//尾插法
    {
     定义结构体指针ptr保存每个结点;
     定义结构体指针tail指向链表尾部;
     /*初始化链表*/
     head=new Node;
     head->next=NULL;
     tail=head;//尾指针先指向头结点;
    
     for i=0 to n
        ptr = new Node;
        输入数据赋给ptr->data;
        ptr->next=NULL;
        tail->next=ptr;//先连接;
        tail=ptr;//再移动tail;
      end for
    }
    • Specific code and achieve the results:
  • Chain is inserted
    when inserting data, when we find the insertion position, we need to modify the predecessor and successor pointers of the pointer position, then we need to define a precursor of the precursor to hold pre pointer inserting position; we modified so convenient

    • Fake code:
    void InserData(ListNode& list)
    {
     定义结构体指针pre用来保存前驱指针;
     定义结构体指针ptr保存插入的数据;
     初始化ptr;
    
     输入数据赋给ptr;
     while(pre->next && 未满足插入条件)
           pre = pre->next;
     end while
     ptr->next=pre->next;//先保存后继指针;
     pre->next=ptr;//再修改前驱指针;
    }
    • Specific code and achieve the results:
  • List delete
    and insert as a linked list, also need to modify the predecessor and successor delete position, it also set a precursor here refers to pre positive precursor to save.
    • Fake code:
    void DeletData(ListNode& list)
    {
     定义结构体指针pre保存删除位置的前驱;
     定义结构体指针ptr保存删除的结点;
     输入需要删除的数据赋给x;
    
     while(pre->next && 未找到删除的数据)
        pre=pre->next;
     end while
     if  pre->next == NULL //说明没有找到删除的数据
        输出删除失败;
     else //找到了删除的数据;
        ptr=pre->next;//ptr保存需要删除的结点;
        pre->next=ptr->next;//连接删除结点的前驱和后继;
     end if       
    }
    • Specific code and achieve the results:
  • Single chain reversal
    will lead a single linked list node reversal, in essence, is the use of the first interpolation method, can be modified directly on the original single linked list, where the need to store a successor pointer latter back unmodified data;
    • Fake code:
void ReverseList(ListNode& L)
{
   定义结构体指针ptr保存当前结点;
   定义结构体指针latter保存原链表中ptr的后继结点;
   latter=L->next;
   L->next=NULL;//重构链表;
   while(latter!=NULL)//遍历原链表;
        ptr=latter;
        latter=latter->next;//保存ptr的后继结点;
        /*头插法*/
        ptr->next=L->next;
        L->next=ptr;
   end while   
}
  • Specific code and achieve the results:

  • List segmentation : the essence is the application to delete nodes and interpolation of the head.

    • Fake code:
    void SplitList(ListNode& L, ListNode& L1, ListNode& L2)
    {
     定义结构体指针ptr1来遍历链表L1;
     定义结构体指针ptr2来保存属于链表L2的结点;
     /*初始化链表L2*/
     L2-next=NULL;
     L1=L;//L1和L共享结点;
     ptr1=L1->next;
    
     while(ptr1!=NULL && ptr1->next!=NULL)//判断是否遍历结束
          ptr2=ptr1->next;//ptr2保存要插入L2的结点;
          /*L1做删除结点操作*/
          ptr1->next=ptr2->next;
          ptr1=ptr2->next;
          /*ptr2保存的结点用头插法编入L2中*/
          ptr2->next=L2->next;
          L2->next=ptr2;
      end while
    }
    • Specific operations and achieve results:

4. ordered list

It is a special linear table, all of the elements in ascending or descending ordered.
Basic Operations

  • Create an ordered list : The structure pointer ptr to save the number entered by the user, and then traverse the list to find the pluggable position.
    • Fake code:
    void SortList(ListNode& list,int n)
    {
      定义结构体指针ptr保存新结点;
      定义结构体指针listPtr遍历链表list;
      /*初始化链表list*/
      list = new Node;
      list->next=NULL;
    
       for i=0 to n
            ptr=new Node;
            输入数据赋给ptr->data;
            listPtr=list;//开始遍历链表list;
            while(listPtr->next && 未找到插入位置)
                 listPtr = listPtr->next;
            end while
        ptr->next=list->next;//开始进行插入;
        listPtr->next=ptr;
        end for
    }
    • Specific code and achieve the results:
  • Ordered insertion of single chain : see above is inserted into a single list
  • Ordered a single linked list to delete data - Delete interval data
    6-3 jmu-ds- list section deleted
    • Fake code:
    void DelList(ListNode& L, int min, int max)
    {
    定义结构体指针pre,用pre->next来遍历链表;
    pre=L;
    while(pre->next!=NULL)
        if(pre->next->data <= max && pre->next->data >= min)
              p = pre->next;
              pre->next = p->next;//改变pre的后继结点;   
              delete p;
        else if(pre->next->data>max)//因为是有序链表,所以如果数据大于删除区间的最大值就已经完成了删除。
              break;
        else //pre->next->data < min的情景。
              pre=pre->next;//要继续往下遍历;
        end if
     end while  
    }
    • Specific code and achieve the results:
  • The combined sorted linked list : each L2 essentially the node is inserted in a position corresponding to L1, L1 is inserted with the result that the data remains ordered;

    • Fake code:
void MergeList(ListNode& L1, ListNode L2)//合并链表,使结果呈递增排序
{
   定义结构体指针ptr1和ptr2分别用来遍历L1和L2;
   定义结构体指针temp;
   ptr1=L1;
   ptr2=L2->next;
   
   while(ptr2!=NULL)
        if (ptr1->next->data > ptr2->data)//找到L1中可插入的位置
              temp = ptr2;//用于保存L2中需要插入的结点;
              ptr2 = ptr2->next;//继续遍历L2;
              /*把temp插入L1中*/
              temp->next=ptr1->next;
              ptr1->next=temp;
        else if (ptr1->next->data==ptr->data)//用于删除重复的数据
               ptr2=ptr2->next;//跳过重复数据;
        end if
        ptr1=ptr1->next;
        if(ptr1->next==NULL && ptr2!=NULL)//链表L1遍历完毕L2没有遍历完
               ptr1->next=ptr2;//直接L2中剩余的结点连接到当前ptr1指向结点的后面   
               break;
        end if
}
  • Specific code and achieve the results:

The doubly linked list


Structure is defined

struct Node
{
    ElemType data;
    struct Node *prior;//指向前驱结点;
    struct Node *next;//指向后继结点;
};

Feature

  • Starting from any one node can find the node's predecessor and successor node;
  • Regardless of the starting point from which node, you can access to other nodes, the nodes can only access a single list behind the node.
  • When performing insertions and deletions do not need to define a pointer precursor, the precursor double linked list pointer to the list can be inserted and deleted;

6. circular list

  • Single chain loop: node type and single chain acyclic same, except that the single-chain circular table pointer in the end node domain directed into the header node, the entire list to form a ring. Thus any one node in the list can be found starting at any point.
  • Circular doubly linked list: node type and a non-circular doubly linked list, except that the circular doubly-linked list in Table successor pointer tail node into points header precursor pointer, the precursor header pointer points to a subsequent pointer to the end node.

1.2 pairs of linear form understanding and learning experience

Point linear list of a lot of knowledge, and very flexible. Beginning to learn a little bit jerky, it will not use. After two weeks of learning the linear table already we have a rough outline. But when the subject is still usually do miss some of the details, causing the program to go wrong. For example, when building the list, I often forget to get my tail junction point to NULL, causing the program to crash into an infinite loop output. Even when I have understood, or there will be many small details may be missing, I think it should be I'm not skilled enough, or not enough of the code brush, brush next to the code, and more sum up experience summary the problems and solutions. Learning about the linear form has only just begun, little contact with the next chapter of a stack of school feeling now seems to have just scratched the surface of the linear form (perhaps even fur are not really), there are a lot of future we need a lot we can learn a lot a lot of.

2.PTA lab assignments

List the number of reciprocal 2.1 m

2.1.1 Code Screenshot

Method One:

Method Two:

2.1.2 The title PTA submit a list of instructions


This question I tried the above two methods, different errors encountered when different test methods.
method one:

Q:运行时错误
A:起初不知道运行时错误是什么意思,以为是网页有问题于是就多试了几下。后来发现是因为我没有让链表的尾结点指向NULL,因为在自己写代码的时候没有实现销毁链表这一函数,所以我的程序能运行成功并输出正确的答案。但是在过测试的时候,由于需要执行销毁链表,找不到尾结点,程序就无法结束导致运行错误;

Method Two:

Q:部分正确
A:刚开移动q时,用的是for循环,循环变量i是从1开始,当q指向第m个数退出循环时,i等于m+1,不等于m,而我下面用i==m时来判定位置合法,i!=m时位置无效,显然无论我输入的m是否合法,其结果都是无效;
Q:部分错误
A:我没有发现上面那个错误,修改了判断位置是否有效的条件,导致原本位置应该无效的变成有效的,最后对空指针进行取值导致位置无效的测试点段错误。
Q:部分错误
A:判断位置是否有效的条件少了一个等号;
Q:编译错误
A:复制代码时没有把最后的大括号复制进来;

2.2 univariate polynomial multiplication and addition operations

2.2.1 Code Screenshot










2.2.2 The title PTA submit a list of instructions

Q:部分错误
A:没有考虑到同项需要再进行相加减;
Q:部分错误
A:同项相消时为0,输出时,要舍去不输出,我全部都输出了。
Q:部分错误
A:当结果为零多项式时,我没有输出,因为为0的全部都跳过输出了。于是我设置了变量flag,只要有输出就改变flag的值,如果flag的值没有改变,则表结果为零多项式,需要输出0 0

2.3 Operation sequence table

2.3.1 Code Screenshot



2.3.2 The title PTA submit a list of instructions

Q:编译错误
A:我编程时用的是C++的语法,而题目规定用c语言
Q:答案错误
A:题目理解错误,我以为读顺序表的插入只能插入数组的最后一位数后面。
Q:答案错误
A:删除数据的函数中,判断位置是否合法的条件错误,非法的应该是小于0或者大于last,我写的是大于MaxSize或者小于last。
Q:部分错误
A:不小心将自己为了方便在vs中调试而设置的输出代码一起复制进来。

3. Read Code

3.1 adds two numbers

  • topic
  • Code
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode sum = l1;
        while(l1.next!=null&&l2.next!=null){
            l1.val += l2.val;
            l1 = l1.next;
            l2 = l2.next;
        }
        l1.val += l2.val;
        if(l1.next==null&&l2.next!=null){
            l1.next = l2.next;
        }
        l1 = sum;
        while(l1.next!=null){
            if(l1.val>9){
                l1.val -= 10;
                l1.next.val++;
            }
            l1 = l1.next;
        }
        if(l1.val>9){
            l1.val -= 10;
            l1.next = new ListNode(1);
        }
        return sum;
    }
}

作者:lhpyxjn75x
链接:https://leetcode-cn.com/problems/add-two-numbers/solution/2liang-shu-xiang-jia-jian-dan-bao-li-2ms-by-lhpyxj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3.1.1 design ideas

From the first start adding the two lists, the results are saved directly to all l1, and then began to consider the carry issue to note here when the number of the last number in the list l1 and l2 the list of needs by adding a carry, l1 We need to develop new space to save the new number.

Provided m bits saved l1, l2 n bits saved.

  • The time complexity of O (m + n): the first of a while loop traverses min (m, n) times, while the second loop through the max (m, n) times, the time complexity of O (m + n ).
  • Space complexity O (1): Because when added together, the sum of each digit of the results are saved to l1, only the last digit of the sum Carry need to open up a space to save, so space complexity is O ( 1).

3.1.2 pseudocode

定义一个结构体指针sum来保存l1的头指针;
while(l1->next!=NULL && l2->next!=NULL)//把两个数每一位都先进行相加;
     l1->val += l2->val;
     l1 = l1->next;
     l2 = l2->next;
end while
l1->val += l2.val;//循环遍历到l1和l2中有一个后继结点为NULL时停止,退出循环时当前的位上的数还未相加
if(l1->next == NULL && l2->next!=NULL)//l2的位数比l1多直接把l2后面的数连接到l1后面
     l1->next=l2->next;//直接把l2后面的数连接到l1后面  
end if
l1=sum;//指向头指针;
while (l1->next!=NULL)//开始处理进位问题;
     if(l1->val>9)
         l1->val-=10;
         l1->next->val++; 
     end if
     l1=l1->next;
end while
if(l1->val>9)//遍历到最后一位数时,如果需要进位,必须要开辟新的空间
     l1->val-=10;
     l1->next=new ListNode(1);
end if
return sum;

3.1.3 operating results


3.1.4 Analysis of the advantages and difficulties in problem solving topics

Advantages: This question requires us to find two numbers together, are calculated from the minimum number two position in accordance with our usual thinking, questions were very friendly data input into a reverse order, and substantially reduce our write the burden of code, we only need to combine knowledge in accordance with the completion of primary school code on it.
Difficulty: For we are used to list containing the head node, the write test code when you need a bit of thought, not in accordance with the wording of the original has a head node to create lists or to determine whether out of the loop. And you can see from the author of the code, which appear to be two places merged into circulation inside the code, but yet to be singled out for execution, pay special attention to these two places, if all merge into the circulation, it will make l1 pointer points to a null value and a null pointer erroneous operation.

3.2 twenty-two switching node linked list

  • topic:
  • Code:
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode next = head.next;
        head.next = swapPairs(next.next);
        next.next = head;
        return next;
    }
}

作者:guanpengchn
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/hua-jie-suan-fa-24-liang-liang-jiao-huan-lian-biao/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3.2.1 design ideas

From the beginning to the first chain two two nodes as a whole, each of the two nodes exchange the entire, originally the first node is always connected to the next whole second node (exchange after variations as a whole at the first node), we provided a recursive algorithm, the first progressive or the last two numbers of a number, the latter starts to exchange data, returned after the exchange is complete to one. The first node of the current connection in the entire back after data has been exchanged, then the entire current node moves to the second before the first node.

The list provided there are n nodes:

  • Time complexity of O (n): number of times the recursive n / 2, the number of recursion need to operate each time was 4 times, the time complexity is T (N) = 4 * (n / 2) + 1 = O (N );
  • Space complexity O (n): number of times the recursive n / 2, each recursion occupies occupies a space, the space complexity is T (N) = n / 2 = O (N);

3.2.2 pseudocode

if head == NULL || head->next==NULL //结束标志,不管head为NULL还是为最后一个节点,都不影响,一位递归回去的指针总是当前整体中的第一个结点;
      return head;
end if
ListNode next=head->next;//next指向当前整体中的第二个结点;
head->next = swapPairs(next->next);//当前整体中的第一个结点先连接上已经交换过结点后的下一个整体中的第一个结点(就是原来该整体的第二个结点)
next->next=head;//将该整体的第一个结点移动到第二个结点后面;
return next;//原本的第二个结点现在变成这个整体中的第一个结点,返回到上一级函数。 

3.2.3 operating results

3.2.4 Analysis of the advantages and difficulties in problem solving topics

Advantages: This question is a non-recursive algorithm is not a problem, for example, open up a new list for the exchange of data stored, the idea is relatively simple, recursive algorithm time complexity and the same, but the complexity of space is 0 (1) than the recursive algorithm to be small, even can not think of a recursive algorithm, the use of non-recursive algorithm is also relatively easy to implement;
difficulty: I think the difficulty of this question is to use a recursive algorithm when the code when I saw this when I was ignorant of (magical ). Whole thinking recursive algorithm is very clever and flexible, just understand this code I spent a night time. Recursive algorithm still have to draw, illiterate really see what to see (dark circles warning!).

Guess you like

Origin www.cnblogs.com/xianerbian/p/12361091.html