Docsity

Singley linked list and its examples, Slides of Data Structures and Algorithms

Partial preview of the text

Download Singley linked list and its examples and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

LECTURE 04 – LINKED LIST & SINGLY LINKED LIST

Why to Study?

  • To understand the manner in which data is organized and presented
  • To get familiarized with important, most often used and efficient data structuring

techniques

  • After completion, you will have a comprehensive understanding of data organization

mechanisms enabling you to write simple, clear, and efficient programs

Data Structures

  • A data structure is a collection of fundamental types (integers, floating point numbers,

characters etc. )

  • represent an identifiable entity
  • enables us to perform operations on them
  • For example, Time can be represented as a collection of hours, minutes and seconds

and the operations that can be performed on it include

  • increasing time every second
  • adding two time entities and etc.
  • C introduced struct for creating and manipulating data structures, C++ extended struct

to class

Arrays

  • A very useful data structures, used for homogeneous collection of elements
  • Limitations:
    • Size has to be known at the compilation time
    • Data in the array are separated in computer memory by the same distance, which means inserting an item in the array requires shifting other data

Linked List

  • Advantages:
    • A linked list is appropriate when the number of data elements to be stored in the list is unknown
    • Because linked lists are dynamic, their size can grow or shrink to accommodate the actual number of elements in the list
    • Easy Insertions and Deletions
  • Limitations:
    • We can perform efficient searches on arrays (e.g., binary search) but this is not practical with a linked list

Simple Linked List

  • Linked List:
    • A dynamic array of data, linked using pointers

Simple Linked List newLink->value = temp_value; newLink->next = HEAD; HEAD = newLink; cout << "Add another (y/n)? "; cin >> option; } Node* Temp = HEAD; while (Temp != NULL) //Display the List { cout << "Item’s value is: " << Temp->value << endl; Temp = Temp->next; }

Simple Linked List Temp = HEAD; while (Temp != NULL) //Free the Memory { HEAD = Temp->next; delete Temp; Temp = HEAD; } }

Singly Linked List

Data Structures

  • One of the attributes of a linked list is that the nodes are not stored contiguously in

memory (as in the case of array elements)

  • To determine the beginning of the list, and each additional element in the list, we need

to use pointers

  • It is convenient to create a structure that contains the head pointer of a list and also

information about the list itself (e.g., number of elements currently in the list, maximum

number of elements to be allowed in the list, etc).

  • This extra data is referred to as metadata.

Example

  • Please see the Handout, SinglyLinkedList.