good data structures linked lists
TRANSCRIPT
-
8/2/2019 GOOD Data Structures Linked Lists
1/21
Data Structures : Lists
Abhilash I
-
8/2/2019 GOOD Data Structures Linked Lists
2/21
Basics
In Cstruct node{
int data;struct node * next;
}
In C++struct node{
int data;node * next;
}
-
8/2/2019 GOOD Data Structures Linked Lists
3/21
Operations
Allocation of Memorystruct node *nn = (struct node *)malloc(sizeof(struct node)); //c
node *nn = new node; //c++ style
Insertion
Deletion
-
8/2/2019 GOOD Data Structures Linked Lists
4/21
Variants
Structural Variants
1. Single Linked List
2. Doubly Linked List
3. Circular LInked List
4. Circular Doubly Linked List
Ref:en.wikipedia.org/wiki/Linked
_list
http://en.wikipedia.org/wiki/Linked_listhttp://en.wikipedia.org/wiki/Linked_listhttp://en.wikipedia.org/wiki/Linked_listhttp://en.wikipedia.org/wiki/Linked_listhttp://en.wikipedia.org/wiki/Linked_listhttp://en.wikipedia.org/wiki/Linked_list -
8/2/2019 GOOD Data Structures Linked Lists
5/21
Variants : operational
Stack LIFO
Queue FIFO
-
8/2/2019 GOOD Data Structures Linked Lists
6/21
Problems / questions (in interviews too)
To most of the questions naive solutions are trivial Yes, optimized versions are needed
We will walk through a set of problems in next slides
-
8/2/2019 GOOD Data Structures Linked Lists
7/21
Problem #1 Find the maximum/mininum elements of linked list ?
Trivial isn't it. A way to write the solution.
node* findMaximum(node *head){node * returnValue=head;while(head!=NULL){
if (head->data > returnValue->data){returnValue=head;
}head=head->next;
}return reuturnValue;}
Variable Naming/ Style of code is important
-
8/2/2019 GOOD Data Structures Linked Lists
8/21
Problem #2
Find Union/Intersection of two linked lists ? Typically you would have to write a function that returns a
new list, with specified criteria Write it down
-
8/2/2019 GOOD Data Structures Linked Lists
9/21
Problem #3
Reverse a given linked list Hottest problem that is always asked
1. Iterative solution2. Recursive solution
Constraints (da routine ones :P )1. O(1) Space2. O(n) Time
-
8/2/2019 GOOD Data Structures Linked Lists
10/21
Iterative Solution
void reverse(struct node **x){struct node *q,*r,*temp;r=*x;q=NULL;
while(r!=NULL){temp=q;q=r;r=r->next;q->next=temp;
}*x=p;
}
-
8/2/2019 GOOD Data Structures Linked Lists
11/21
Recursive Solution
node* reverse(node** head , node* cur){if(cur->next == NULL) {
*head = cur;}
else{(reverse(head,cur->next))->next = cur;cur->next = NULL;
}return cur;
}
-
8/2/2019 GOOD Data Structures Linked Lists
12/21
Trick: Moving Two pointers cleverly
Depends on the problem at hand. Example: Moving one pointer as
slow as Tortoise, and moving
another as fast as Hare. Also known as Floyd's cycledetection algorithm
Next 4 problems can be solvedusing this trick
-
8/2/2019 GOOD Data Structures Linked Lists
13/21
Problem #4
Find the kth element from the endof linked list ? Constraints
1. O(1) space2. O(n) time
-
8/2/2019 GOOD Data Structures Linked Lists
14/21
Problem #5 : The "Y" shaped Linked List
Given pointers to two singly-linked lists, find out if they arejoined and also at which node they are joined.
ConstriaintsO(1) space
O(N) time
Ref:Go to the link
http://blogs.msdn.com/abhinaba/archive/2007/08/16/interesting-linked-list-problem.aspx -
8/2/2019 GOOD Data Structures Linked Lists
15/21
Problem #6
Find the centerof Linked List ? Constraint :
Without using integervariables
-
8/2/2019 GOOD Data Structures Linked Lists
16/21
Problem #7
Find whether a linked list has a cycle or not ? Possible approches
1. By storing the addresses all the elements already seen ?2. By destructing the linked list ?
What if we don't have auxilary storage and also do it withoutdestructing the list ?
Hint: Apply the intuition from previous slide Extension: Find the node at which the loop starts ?
-
8/2/2019 GOOD Data Structures Linked Lists
17/21
Problem #8:Not so easy deletion
Given a pointer to a node in a singly linked list delete thatparticular node.
-
8/2/2019 GOOD Data Structures Linked Lists
18/21
Problems for practice
1. Add two 100 digit numbers stored in a linked list
2. Check whether linked list is a palindrome. Each node storesa character
3. Swap two nodes of circular doubly linked list4. Sort linked list using quick, merge, insertion, selection sorts
5. Deleting Alternate nodes in the linked list6. XOR Linked List
-
8/2/2019 GOOD Data Structures Linked Lists
19/21
Stacks
Implement a stack using 2 queues efficiently Reverse a stack inplace Implement 3 stacks in an array You are given a black box datastructure which does
insertion and extract min in O(1) time. give an efficientimplementation for extract max using the operationssupported by it. What is the black box ? How to achieve this?
-
8/2/2019 GOOD Data Structures Linked Lists
20/21
cont
We are given with two arrays A and B..each of size N...elements of array contains either 1 or 0...
we have to find such an interval (p,q)(inclusive) such that
the sum of all the elements of A (between this interval) andsum of all elements of B (between this interval ) is equal...
i.e.
a[p]+a[p+1]....+a[q]= b[p]+b[p+1]....+b[q]
-
8/2/2019 GOOD Data Structures Linked Lists
21/21
Hope this presentation is of help :-)