Arrays and linked lists of commonly used data structures

1 Overview

           The program running in the computer is actually the flow of data. Simply put, from one container to another, there are two operations to store and retrieve data corresponding to the data. So for the choice of data structure, our most basic requirement is that it can be installed, and then it is more convenient to access. For example, we need to use a basket to buy vegetables and a bucket to fetch water. It is equally important to choose the right container for our data. Data structure and algorithm can be regarded as the reason why programmers are programmers. I try to make this data structure easier to say, come on! Ollie gives.

2. Time complexity

           Time complexity is a simplified representation of the frequency of program execution, not strictly time, and usually the frequency under the worst condition. Big O notation is used here, and the basic steps are:

           1. Calculate the frequency function T(n)

           2. Keep the highest order items and remove the coefficients

           3. The constant is 1

           Commonly used time complexity are: constant order O(1), logarithmic order O(logn), linear order O(n), linear logarithmic order O(nlogn), square order O(n^2), cubic order O (n^3), k square order O(n^k), exponential order O(2^n)

1. Linear table

               Linear table is a concept of data storage. Its core idea is to string elements one by one, just like queuing. It can be a continuous memory space (sequential storage structure) or a discontinuous memory space (chain storage structure)

                

2. Array

          The array is a continuous space in the memory, once the size of the space is determined, it cannot be changed. As you can see, the array can be used to implement the sequential storage structure of the linear table. As shown in the figure below, if you want to take out an element from the array, the complexity is O(N)

                         

3. Linked list

     1. Single linked list

             The linked list is a discontinuous storage space, so its size can be dynamically changed. So how to string together the elements in the linked list? So each of our locations must contain two variables, one to store the value of the current element, and one to store the address of the next element. In this way, each node reaches for the next node, and all nodes can be connected as a whole. The linked list is highly flexible, and the complexity of accessing elements is O(N/2). The following uses python code to implement a one-way list, which implements the basic addition, deletion, and modification check.

class Node(object):
    def __init__(self,elem):
        self.elem = elem
        self.next = None
class SingleLinkList(object):
    def __init__(self,node=None):
        #双下划线开头表示私有成员,不可以直接访问,必须通过class进行访问
        self.__head = None
    #计算长度
    def length(self):
        cur = self.__head
        count = 0
        while cur.next != None:
            cur = cur.next
            count += 1
        return count
    #判断是否为空
    def is_empty(self):
        return self.__head == None
    #头部添加,将链表的头部设置为该元素,元素的next为原来的头部
    def add(self,item):
        node = Node(item)
        node.next = self.__head
        self.__head = node
    #遍历
    def travel(self):
        cur = self.__head
        while cur != None:
            print(cur.elem,end=' ')
            cur = cur.next  
    #尾部添加,直接将最后一个元素的next指向新的元素
    def append(self,item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next != None:
                cur = cur.next
            cur.next = node
    #固定位置添加,找到插入的前一个位置元素
    #将该元素next设置值为新元素 新元素的next设置为该元素原来的next
    def inert(self,pos,item):
        if pos<0:
            self.add(item)
        elif pos >  self.length()-1:
            self.append(item)
        else:
            per = self.__head
            count = 0
            while count <  pos-1:
                count += 1
                per = per.next
            node = Node(item)
            node.next = per.next
            per.next = node   
    #删除方法 
    #如果是头部,则删除,将头部指向下一个元素
    #如果不是头部,删除,然后将上一个元素的next指向该元素的next
    def remove(self,item):
        cur = self.__head
        per = None
        while cur != None:
            if cur.elem == item:
                if cur == self.__head:
                    self.__head = cur.next
                else:
                    per.next = cur.next
                break
            else:
                per = cur
                cur = cur.next
    #查找方法
    def search(self,item):
        cur = self.__head
        while cur:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        return  False
li = SingleLinkList()
li.append(3)
li.append(4)
li.append(5)
li.append(6)
li.append(7)
li.inert(2,100)
li.remove(5)
print(li.search(100))
li.travel()

2. Circular linked list

       The circular linked list is an extension of the one-way list. If we compare the one-way list to a group of children holding hands and queuing forward. Then the two-way linked list is the group of children playing games in a circle. The specific implementation is also very simple, we can point the next attribute of the tail element to the head element of the linked list.

       

3. Doubly linked list

       As the name implies, a doubly linked list has two directions, that is, for each element, there are two pointers. One element points to the front, and one element points to the back. In specific implementation, there are three variables in each element, one to record the previous element and one to record the back element.

               

 

4. Summary

          This article is my pioneering work on data structure. Data structure can be understood as a container for data, corresponding to various storage methods of data in memory. We introduced the concept of linear tables, using linear structures to store data. One is sequential linear storage, that is, data uses a continuous memory space. One is a chained linear storage method that uses discontinuous memory space. This storage method is more flexible and does not need to know the size of the occupied space in advance. Finally, we introduced linked lists, including singly linked lists, doubly linked lists, and circular linked lists. This linear storage method is relatively easy to understand and relatively simple. This is my first day, and I still dare not say that I have realized the truth. In terms of Taoism and Shushu, it is still at the academic stage. The current data structure to me is like blood flowing in a blood vessel, a pipe of a computer. They coordinated and dispatched, and they flowed beautifully like dancing, which made people excited!

5. Nonsense

          I like my friends very much, and I also like an uncertain life. Every kind of life has its charms, and perhaps the joy of driving a sports car I still can't imagine. But what I know is that everyone’s ability to feel happy is just so little. Everyone has to yearn for those that are not available. The less easily available, the happier we are. So instead of going into the endless loop of boring happiness, it is better to just throw them away. Some people say that not studying or making progress is a waste of time, but boring study really wastes our lives. You exhausted your entire life and stood at the top of the pyramid in a certain field. Looking back, I still feel worthless. Compared with our priceless youthful years, what are these broken things? The saddest thing in life is that we have the life, the source of energy, and the fountain of wisdom of the essence of all things in the world, but we don't know what to do with it. If decadence and laziness fall into one's own trap, then does self-discipline and hard work fall into the trap of heaven and earth? I firmly believe that one day I will find something worthy of my life. As long as I get one step closer to it, what I do is not wasting my life.

[MAD/ Ran Xiang] Naval battle scene Ran Xiang mixed cut Pirates of the Caribbean theme song World of Warships promotional video editing

 

Guess you like

Origin blog.csdn.net/gaobing1993/article/details/108666410