Singly Linked Lists: Definition, Creation, and Basic Operations, Summaries of Data Structures and Algorithms

An overview of singly linked lists, their definition, creation process, and basic operations. It covers insertion and deletion at the beginning, end, and after a specified node, as well as displaying and searching for elements. It also includes code snippets for creating a node and inserting it into a list.

Typology: Summaries

2019/2020

Uploaded on 12/13/2021

jam-chester-lapuz
jam-chester-lapuz 🇵🇭

5 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lapuz, Jam Chester S. DSA LEC
BSCS 1-2 Mr. Regala, Richard
SUMMARY OF LEARNINGS
Singly linked list can be defined as the collection of ordered set of elements. The number of
elements may vary according to need of the program. A node in the singly linked list consist of
two parts: data part and link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address of its immediate
successor.
Creation of the node:
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
pf3
pf4
pf5

Partial preview of the text

Download Singly Linked Lists: Definition, Creation, and Basic Operations and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Lapuz, Jam Chester S. DSA – LEC BSCS 1- 2 Mr. Regala, Richard

SUMMARY OF LEARNINGS

Singly linked list can be defined as the collection of ordered set of elements. The number of elements may vary according to need of the program. A node in the singly linked list consist of two parts: data part and link part. Data part of the node stores actual information that is to be represented by the node while the link part of the node stores the address of its immediate successor. Creation of the node: Basic Operations Following are the basic operations supported by a list.  Insertion − Adds an element at the beginning of the list.  Deletion − Deletes an element at the beginning of the list.  Display − Displays the complete list.  Search − Searches an element using the given key.  Delete − Deletes an element using the given key.

  1. struct node
  2. {
  3. int data;
  4. struct node *next;
  5. };
  6. struct node *head, *ptr;
  7. ptr = (struct node *)malloc(sizeof(struct node *));

Insertion:  Insertion at beginning: Inserting any element at the front of the list. We just need to a few link adjustments to make the new node as the head of the list.  Insertion at end of the list: It involves insertion at the last of the linked list. The new node can be inserted as the only node in the list or it can be inserted as the last one.  Insertion after specified node: It involves insertion after the specified node of the linked list. We need to skip the desired number of nodes in order to reach the node after which the new node will be inserted. Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next to C – [NewNode.next −> RightNode;]

First, locate the target node to be removed, by using searching algorithms. The left (previous) node of the target node now should point to the next node of the target node [LeftNode.next −> TargetNode.next;] This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at. [TargetNode.next −> NULL;] We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe off the target node completely.