Docsity

Linked List Data Structures and Operations, Cheat Sheet of Commercial Law

Partial preview of the text

Download Linked List Data Structures and Operations and more Cheat Sheet Commercial Law in PDF only on Docsity!

Adama Science and Technology University

Data structures and Algorithm

(week 5-6)

LIST DATA

STRUCTURES

  • (^) List ADT by array
  • (^) Dynamic memory
  • (^) Limitations of array
  • (^) Implementation of lists
  • (^) Linked list-singly linked list, doubly linked list, circular (singly and doubly) linked list
  • (^) Operations on linked list-creation, insertion, deletion, update search, adding new nodes

List ADT by array

  • (^) Using Arrays
  • (^) Storing objects in arrays is a common use for arrays
  • (^) Example: we could just as easily have chosen to store records for patients in a hospital or the names of players on a football team

ADT Lists

  • (^) Also called linear list.
  • (^) Group of items arranged in a linear order.
  • (^) Operations supported are : insertion, deletion and read an item.
  • (^) List is defined by its interface; the specific methods used to interact with it.
  • (^) This can be implemented using arrays or linked lists.

Dynamic Allocation of Matrices

  • (^) If the dimensions of a two-dimensional array are not known in advance, it is necessary to allocate the array dynamically. int M = new int[n]; // allocate an array of row pointers* for (int i = 0; i < n; i++) M[i] = new int[m]; // allocate the i-th row This example is allocating storage for a matrix as an array of arrays.
  • (^) For example, suppose that we wish to allocate an integer matrix with n rows and m columns.
  • (^) Each row of the matrix is an array of integers of length m.
  • (^) Recall that a dynamic array is represented as a pointer to its first element, so each row would be declared to be of type int*.
  • (^) How do we group the individual rows together to form the matrix? The matrix is an array of row pointers.

Allocating storage for a matrix as an array of arrays int** M = new int*[n]; // allocate an array of row pointers for (int i = 0; i < n; i++) M[i] = new int[m]; // allocate the i-th row

  • (^) Once allocated, we can access its elements just as before, for example, as “M[i][ j].”
  • (^) As shown in Code, deallocating the matrix involves reversing these steps.
  • (^) First, we deallocate each of the rows, one by one.
  • (^) We then deallocate the array of row pointers. Since we are deleting an array, we use the command “delete[].”

Array Limitations

  • (^) What are the limitations of an array, as a data structure?
    • (^) Fixed size
    • (^) Physically stored in consecutive memory locations
    • (^) To insert or delete items, may need to shift data

Linked Lists

  • (^) A linked list is similar. It is a series of connected "nodes" that contains the "address" of the next node. Each node can store a data point which may be a number, a string or any other type of data.
  • (^) Each data item is embedded in a link.
  • (^) Each Link object contains a reference to the next link in the list of items.
  • (^) In an array items have a particular position, identified by its index.
  • (^) In a list the only way to access an item is to traverse the list

How another node is referenced?

  • (^) Some pointer magic is involved. Let's think about what each node contains:
  • (^) A data item
  • (^) An address of another node
  • (^) We wrap both the data item and the next node reference in a struct as:

Continued..

  • (^) struct node
  • (^) {
  • (^) int data;
  • (^) struct node *next;
  • (^) };
  • (^) Understanding the structure of a linked list node is the key to having a grasp on it.

Continued.. /* Allocate memory / one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); / Assign data values */ one->data = 1; two->data = 2; three->data=3;

Continued..

  • (^) /* Connect nodes */
  • (^) one->next = two;
  • (^) two->next = three;
  • (^) three->next = NULL;
  • (^) /* Save address of first node in head */
  • (^) head = one;