Docsity

Data structure and algorithm, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Data structure and algorithm and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

DATA STRUCTURE AND

ALGORITHM (IT-209)

Instructor: Abdullah Javed ([email protected]) Lecturer, Govt. Graduate College, Jhelum Lecture 03. Fall 2021

AGENDA

Linked List Problems  (^) Nth Element  (^) Delete List  (^) Sorted Insert  (^) Insert Sort  (^) Move Node  (^) Reverse  (^) Remove Duplicates  (^) Append Lists  (^) Sorted Intersect  (^) Shuffle Merge

SORT

Sorted Insert

  • Write a SortedInsert() function which given a list that is sorted in increasing order, and a single node, inserts the node into the correct sorted position in the list.
  • Node SortedInsert(Node head, Node node)**

Insert Sort

  • Write an InsertSort() function which given a list, rearranges its nodes so they are sorted in increasing order. It should use SortedInsert().
  • Node InsertSort(Node head)**

MOVE

Move Node

  • MoveNode() takes two lists, removes the front node from the second list and pushes it onto the front of the first.
  • void MoveNode(Node head1, Node** head2)** Reverse
  • Write an iterative Reverse() function that reverses a list by rearranging all the .next pointers and the head pointer. Ideally, Reverse() should only need to make one pass of the list. The iterative solution is moderately complex. Hint: Use MoveNode()
  • Node Reverse(Node head)**

SORTED INTERSECT

Sorted Intersect

  • Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory —the original lists should not be changed.
  • Node SortedInsertsect(Node head1, Node2* head2)** Shuffle Merge
  • Given two lists, merge their nodes together to make one list, taking nodes alternately between the two lists. So ShuffleMerge() with {1, 2, 3} and {7, 13, 1} should yield {1, 7, 2, 13, 3, 1}. If either list runs out, all the nodes should be taken from the other list.
  • Node ShuffleMerge(Node head1, Node* head2)**