Third Lecture
Third Lecture
Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication
Third lecture
• Till now, we were using array data structure to organize the group of elements
that are to be stored individually in the memory. However, Array has several
advantages and disadvantages which must be known in order to decide the data
structure which will be used throughout the program.
• Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process.
3. All the elements in the array need to be contiguously stored in the memory.
• Linked list is the data structure which can overcome all the limitations of an array.
Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored
in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of declaration.
List grows as per the program's demand and limited to the available memory space.
• We can Allocate the space for the new node using malloc or new instruction.
• First method : ptr = (struct node *) malloc(sizeof(struct node *));
• Second method: node* newNode = new node();
• Many operations
• Insertion.
• Deletion.
• Traversing.
• Searching.
• Insertion
• The insertion into a singly linked list can be performed at different positions.
• Examples
• Insertion at the bigging of the linked list.
• Insertion at the end of the linked list.
• Insertion before specific node.
• Output
• Algorithm
• STEP 1: Start.
• STEP 2: Create NEW_NODE = PTR.
• STEP 3: IF PTR = NULL Write OVERFLOW, Go to Step 11.
• STEP 4: SET PTR -> DATA = Item.
• STEP 5: IF Head == NULL, HEAD = PTR, SET PTR - > NEXT = NUL, Go
to Step 11.
• STEP 6: SET Temp = HEAD.
• STEP 7: Repeat Step 8 while Temp - > NEXT != NULL
• STEP 8: SET Temp = Temp - > NEXT
• STEP 9: SET Temp - > NEXT = PTR
• STEP 10: SET PTR - > NEXT = NUL
• STEP 11: End.
• Output
return 0;
}
• Output
• Algorithm
• STEP 1: START
• STEP 2: IF HEAD = NULL, WRITE UNDERFLOW, GOTO STEP 12
• STEP 3: SET PTR = HEAD
• STEP 4: SET I = 0, ENTER LOC.
• STEP 5: REPEAT STEP 6 TO 9 WHILE I < LOC
• STEP 6: PTR1 = PTR
• STEP 7: PTR = PTR → NEXT
• STEP 8: IF PTR = NULL, WRITE "DESIRED NODE NOT PRESENT”,
GOTO STEP 12
• STEP 9: I = I+1
• STEP 10: PTR1 → NEXT = PTR → NEXT
• STEP 11: FREE PTR
• STEP 12: END
• Algorithm
• Step 1: START
• Step 2: SET PTR = HEAD
• Step 3: Set I = 0
• STEP 4: IF PTR = NULL, WRITE "EMPTY LIST”, GOTO STEP 9.
• STEP 5: REPEAT STEPS 6 TO 8 WHILE PTR != NULL
• STEP 6: IF PTR - > DATA = ITEM, RETURN I
• STEP 7: I = I + 1
• STEP 8: PTR = PTR - > NEXT
• STEP 9: END
• For the Traversal operation the worst case time complexity is O(n).
• For the Insertion operation in general (Not the first element) the worst
case time complexity is O(n).
• For the Deletion operation the worst case time complexity is O(n).
• For the Search operation the worst case time complexity is O(n).