Professional Documents
Culture Documents
Pointers
Lists
Conclusion
Abhilash I
abhilashi@students.iiit.ac.in
Table of contents
1 Problem on loops
Solution
2 Pointers
Problem 1
Problem 2
Never forget these
3 Lists
Decleration
Operations
Problems
4 Conclusion
Problem on loops
for(i=0; i<n;i++)
for(i=0;i<p;i++)
printf("yes");
Problem statement
On executing the above code how many times will yes be printed
for different values of p=n, n-1, n-2;
value of p answer
n ?
n-1 ?
n-2 ?
Solution
value of p answer
n n
n-1 ∞ n-1
n-2 ∞
Solution
value of p answer
n n
n-1 ∞ n-1
n-2 ∞
Problem1
Question
What is the output of the above program ?
Abhilash I abhilashi@students.iiit.ac.in Loops, Pointers and Linked Lists
Problem on loops
Problem 1
Pointers
Problem 2
Lists
Never forget these
Conclusion
Problem 2
Question
What is the output of the above program ?
Abhilash I abhilashi@students.iiit.ac.in Loops, Pointers and Linked Lists
Problem on loops
Problem 1
Pointers
Problem 2
Lists
Never forget these
Conclusion
Pointer is a variable.
In the decleration char *p; What is the data type of p and *p ?
Pointer is a variable.
In the decleration char *p; What is the data type of p and *p ?
ANSI C standard defines string literals to be constant.
char *p="tutorial";
p[0]=’z’; //results in a segmentation fault
You cannot modify the base address of an array.
char buff[100];
char *p="pointer"
buff=p; //raises a compilation error;
Basics: Decleration
In C
struct node{
int data;
struct node * next;
}
Basics: Decleration
In C++
struct node{
int data;
node * next;
}
Basics: Decleration
In C
struct node{
int data;
struct node * next;
}
In C++
struct node{
int data;
node * next;
}
Basics: Operations
Allocation of Memory
struct node *nn = (struct node *)malloc(sizeof(struct node))); //c
node *nn = new node; //c++ style
Basics: Operations
Allocation of Memory
struct node *nn = (struct node *)malloc(sizeof(struct node))); //c
node *nn = new node; //c++ style
Insertion
Deletion
Nature of problems
Nature of problems
Nature of problems
Problem Statement
Find the maximum/mininum elements of a linked list ?
Problem Statement
Find the maximum/mininum elements of a linked list ?
Problem Statement
Find union/intersection of two linked lists ?
Problem Statement
Reverse a linked list.
Problem Statement
Reverse a linked list.
Constraints
O(1) space complexity
O(N) time complexity
Problem Statement
Reverse a linked list.
Constraints
O(1) space complexity
O(N) time complexity
Iterative solution
Recursive solution
Problem Statement
Given a pointer to a node in a linked list, how do you delete that
particular node ?
Problem Statement
Find the k th element from the end of a linked list ?
Problem Statement
Find the k th element from the end of a linked list ?
Constraints
Without using integer variable to store length of list.
Problem Statement
Find the center of the linked list ?
Problem Statement
Find the center of the linked list ?
Constraints
You cannot use integer variables in your solution.
Problem Statement
Given pointers to two singly-linked lists, find out if they are joined
and also at which node they are joined.
Problem Statement
Find whether the linked list has a cycle or not ?
Problem Statement
Find whether the linked list has a cycle or not ?
Solution strategies
By storing the addresses of the elements already seen ?
Problem Statement
Find whether the linked list has a cycle or not ?
Solution strategies
By storing the addresses of the elements already seen ?
By destructing the linked list ?
Problem Statement
Find whether the linked list has a cycle or not ?
Solution strategies
By storing the addresses of the elements already seen ?
By destructing the linked list ?
By reversing the linked list ?
Problem Statement
Find whether the linked list has a cycle or not ?
Solution strategies
By storing the addresses of the elements already seen ?
By destructing the linked list ?
By reversing the linked list ?
By cleverly moving two pointers
Abhilash I abhilashi@students.iiit.ac.in Loops, Pointers and Linked Lists
Problem on loops
Pointers
Lists
Conclusion
Conclusion