언젠가는 되어있겠지
데이터 공부 노트
언젠가는 되어있겠지
전체 방문자
오늘
어제
  • 분류 전체보기 (410)
    • Programming (14)
      • Python (6)
      • Algorithm (8)
    • OS (9)
      • Linux (9)
    • DBMS (2)
      • MySQL (2)
    • IoT Apps (6)
      • AppInventor (5)
      • Arduino (1)
    • LeetCode (151)
      • Easy (136)
      • Medium (13)
      • Hard (2)
    • Course (207)
      • [IBM] Data Science (9)
      • [IBM] Data Engineering (5)
      • [Inflearn] Pandas (12)
      • [Inflearn] Public Data Anal.. (8)
      • [Kaggle] Data Science (49)
      • [Youtube] Informations (2)
      • [Coursera] Machine Learning (40)
      • [Progrmiz] Data Structure A.. (15)
      • [DataQuest] Data Engineerin.. (67)
    • Data Engineering (20)
      • [Data Quest] Handling Datas.. (16)
      • [Data Quest] Data Pipeline (4)
    • Certificate (0)
      • 빅데이터 분석기사 (0)
    • SQLD (0)
    • 하고 싶은 이야기 (0)
    • 소설 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • Kaggle Course
  • Data Science

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
언젠가는 되어있겠지

데이터 공부 노트

[DS2] Linked List
Course/[Progrmiz] Data Structure Algorithm

[DS2] Linked List

2022. 8. 22. 17:38

1. What is Linked List?

1.1 Definition

A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores the data and the address of the next node.

 

Starting point of linked list have a special name called HEAD. Also, the last node in the linked list can be identified because its next portion points to NULL.

 

Linked lists can be of multiple types : singly, doubly, and circular linked list.

 

1.2 Representation

Each node consists :

  • A data item
  • An address of another node

Each struct node has a data item and a pointer to another struct node.

1.3 Implementation Linked List

class Node: 
    def __init__(self, item):
        self.item = item
        self.next = None 
        
class LinkedList: 
    def __init__(self):
        self.head = None

 

2. Linked List Operations

  • Traversal : access each element of the linked list
  • Insertion : adds a new element to the linked list
  • Deletion : removes the existing elements
  • Search : find a node in the linked list
  • Sort : sort the node of the linked list

 

2.1 Traversal a Linked List

Displaying the contents of a linked list is moving the temp node to the next one and display its contents.

 

2.2 Insert elements to a Linked List

  1. Insert at the beginning
    1. Allocate memory for new node
    2. Store data
    3. Change next of new node to point to head
    4. Change head to point to recently created node
  2. Insert at the End
    1. Allocate memory for new node
    2. Store data
    3. Traerse to last node
    4. Change next of last node to recently created node
  3. Insert at the Middle
    1. Allocate memory and store data for new node
    2. Traverse to node just for the required position of new node
    3. Change next pointers to include new node in between

 

2.3 Delete from a Linked List

  1. Delete from beginning
    1. Point head to the second node
  2. Delete from end
    1. Traverse to second last element
    2. Change its next pointer to null
  3. Delete from middle
    1. Traverse to element before the element to be deleted
    2. Change next pointers to exclude the node from the chain

 

2.4 Search an Element on a Linked List

  1. Make head as the current node
  2. Run a loop until the current node is None because the last element points to None.
  3. In each iteration, check if the key of the node is equal to item. If it the key matches the item, return Ture otherwise return False.

 

2.5 Sort Elements of a Linked List

  1. Make the head as the current node and create another node node index for later use.
  2. If head is null, return.
  3. Else, run a loop till the last node.
  4. In each iteration, follow the following step 5 - 6.
  5. Store the next node of current in index.
  6. Check if the data of the current node is greater than the next node. If it is greater, swap current and index.

 

2.6 Operation implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None 
        
class LinkedList: 
    def __init__(self): 
        self.head = None 
        
    def insertAtBeginning(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node 
        
    def insertAfter(self, prev_node, new_data):
        if prev_node is None : 
            print("The given previous node must inLinkedList.")
            return
            
        new_node = Node(new_data)
        new_node.next = prev_node.next
        prev_node.next = new_node
        
    def insertAtEnd(self, new_data): 
        new_node = Node(new_data)
        
        if self.head is None : 
            self.head = new_node 
            return 
            
        last = self.head
        while (last.next):
            last = last.next
        last.next = new_node 
        
    def deleteNode(self, position):
        if self.head is None:
            return
        temp = self.head 
        if position == 0 : 
            self.head = temp.next 
            temp = None 
            return 
        
        # Find the key to be deleted
        for i in range(position - 1):
            temp = temp.next
            if temp is None: 
                break 
                
        if temp is None : return
        if temp.next is None : return 
        next = temp.next.next 
        temp.next = None 
        temp.next = next 
        
     def search(self, key):
        current = self.head

        while current is not None:
            if current.data == key:
                return True
            current = current.next
            
        return False
        
    def sortLinkedList(self, head): 
        current = head 
        index = Node(None) 
        
        if head is None : return 
        else : 
            while current is not None : 
                index = current.next 
                while index is not None : 
                    if current.data > index.data: 
                        current.data, index.data = index.data, current.data
                    index = index.next 
                current = current.next 
        
     def printList(self):
        temp = self.head
        while (temp):
            print(str(temp.data) + " ", end="")
            temp = temp.next

3. Source

https://www.programiz.com/dsa/linked-list-operations

 

Linked List Operations: Traverse, Insert and Delete

Linked List Operations: Traverse, Insert and Delete In this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java. There are various linked list operations tha

www.programiz.com

 

'Course > [Progrmiz] Data Structure Algorithm' 카테고리의 다른 글

[DS2] Hash Table  (0) 2022.08.22
[DS2] Types of Linked List  (0) 2022.08.22
[DS1] Types of Queue  (0) 2022.08.16
[DS1] Queue  (0) 2022.08.10
[DS1] Stack  (0) 2022.08.10
    'Course/[Progrmiz] Data Structure Algorithm' 카테고리의 다른 글
    • [DS2] Hash Table
    • [DS2] Types of Linked List
    • [DS1] Types of Queue
    • [DS1] Queue
    언젠가는 되어있겠지
    언젠가는 되어있겠지

    티스토리툴바