Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Data Structures & Algorithms

Spring 2014

Assignment 4
Singly Link List

DEADLINE for Question 1-5 Friday 2nd May


11:59 PM
And Question 6-11 4th May
11:59 PM

Follow the Deadline. No excuse will be entertained.

By NABEEL SABIR KHAN BLOCH


You can Submit your Assignment at

sdatastructures@gmail.com
Objective

●Link List
Question No. 1
Write the following Function:-
1. InsertAtend(int); This function takes an integer as a parameter
adds a new node at the end of the list.
2. InsertAtstart(int); This function take an integer as parameter add a
new node at the start of the list.
3. InsertAfter(int, int); This function take two integers as a parameter
and insert a new node after first integer value.
1->2->3->4->5
InsertAfter(3,6);
1->2->3->6->4->5
4. InsertBefor(int,int); this function take two integer as parameter
and insert new node after first integer value.
1->2->3->4->5
InsertBefor (3,6);
1->2->6->3->4->5
5. DeleteAtend(); this function delete the last node of the list.
6. DeleteAtstart(); this function delete the first node of the list.
7. DeleteAnyloction(int); this function take one integer as parameter
and delete this integerlocation node.
1->2->6->3->4->5
DeleteAnyloction(3);
1->2->3->4->5
8. DeleteBefore(int); this function take one integer as parameter and
delete before this integerlocation node.
1->2->6->3->4->5
DeleteBefore (3);
1->2->3->4->5
9. DeleteAfter(int); this function take one integer as parameter and
delete After this integerlocation node.
1->2->6->3->4->5
DeleteAfter (3);
1->2->6->3->5
10. SwapanyTwoNodeData(int,int); this function take two integer as
parameter and swap these node data.

1->2->6->3->4->5
SwapanyTwoNodeData (2,4);
1->3->6->2->4->5

11. PrintList(); Print all the nodes Data.


12. PrintRevers(); Print all the nodes Data in reverse order.
13. PrintforwardandRevers(); Print half nodes Data forward and half
nodes Data revers.
1->2->6->3->2->4
PrintforwardandRevers ();
1->2->6->4->2->3
14. Print_revers_withskip(int); Print all nodes of list but skip the node
start to given integer.
1->2->6->3->2->4
Print_revers_withskip(2)
4->2->3->6

15. DeleteanyNode(int);
1->2->6->3->2->4->5
If the integer is 2
1->6->3->4->5

Question No. 2
Write a function to find the 5th element from a singly link list from the end (not
from Head) in the one pass.

Question No. 3

Write a Function append the last n node of a link list to the beginning of the list.

e.g : 1->2->3->4->5->6

if n=2

5->6->1->2->3->4

Question No. 4

Write a function to swap the consecutive two elements of a list. Such as,
if list is “a->b->c->d->e->NULL” then after calling the function the list will be “b->a-
>d->c->e->NULL”.

Question No. 5

Write a RemoveDuplicates() function which takes a list sorted in increasing order


and
delete any duplicate nodes from the list. Ideally, the list should only be traversed
once.

Question No. 6

Write a function take two list as parameters and return a 3rd list as:

Suppose:

L1 = 1->2->3->4

L2 = 5->6->7
Make L3 as:

1->5->2->6->3->7->4

Question No. 7

Write a function take two integer link lists and Add these lists and store in the 3 rd
link list.

1->2->-3->4->5->6->7->8->9->0

1->2->-3->4->5->6->7->8->9->0

Result in 3rd list is

2->4->6->9->1->3->5->7->8->0

Question No. 8

Write a function take two link lists as a parameter and merge them in the 3 rd list.

L1: 2->4->6->8->10

L2: 1->3->5->7->9

List Merge(List L1, List L2);

L3: 1->2->3->4->5->6->7->8->9->10

Question No. 9

Write a function that takes two sorted lists and take the union of those lists in
another list, then return the new list from the function.

L1: 1->2->4->6->8->10

L2: 1->3->4->5->7->9
List Union(List L1, List L2);

L3: 1->2->3->4->5->6->7->8->9->10

Question No. 10

Write a function that takes two sorted lists and take their Intersection of those
lists in another list, then return the new list from the function.
L1: 1->2->4->5->6->8->10

L2: 1->3->4->5->7->9

List Intersection(List L1, List L2);

L3: 1->4->5

Question No. 11

Write a function to sort the link list.


Note: sort the node of the list, not Data.

1->2->5->6->4->11->10->3

Void Sort();

1->2->3->4->5->6->10->11

You might also like