Linked List Sheet
Linked List Sheet
Linked List Sheet
1. Add member functions to the List class with the following specifications:
A public function . Recursive_List_Size( ) to call a recursive private
function to return the size of the list.
Answer:
int List_Size2(NodePointer h)
{if (h == NULL) return 0;
else return 1 + List_Size2(h->next);}
Public:
template <class keyType, class dataType>
int List<keyType,dataType> :: Recursive_List_Size( )
{ return List_Size2(head); }
2. Write a function Reverse() that receive a list and return it in reverse order
Answer:
void List:: Reverse()
{NodePointer res=NULL;
NodePointer current= head;
NodePointer nextptr;
while(current!=NULL)
{nextptr=current->next;
current->next=res;
res=current;
current=nextptr;
}
head=res;
}
3. Write a function Arrange ( ) that take a list to arrange its nodes so they are
stored in ascending order based on key value.
Answer:
void arrange(List &L)
{List L1;
int k; char d;
L.toFirst();
L1.makeListEmpty();
while(!L.curIsEmpty())
{
L.retrieveData(k,d);
L1.orderInsert(k,d);
L.deleteNode();
}
while (!L1.curIsEmpty())
{
L1.retrieveData(k,d);
L.insertEnd(k,d);
L1.deleteNode();
}
}
4. Write a class Student which contains private members "char* Stud-name, int
Stud-ID, float Total , float Perc". Perc will be calculated using this formula
for each student " Perc = Total / 500 * 100. The class has only two public
methods , "GetStudDetails, PutStudDetails". After creating the class, use it
to read and print multiple student details using linked list of objects.
Answer:
class Student{
string studname;
int studid;
float total,perc;
public:
void GetStudDetails(string &name,int &id,float &t,float &p)
{
name = studname ;
id = studid;
t = total;
p = perc;
}
void PutStudDetails(string name,int id,float t)
{
studname = name;
studid=id;
total=t;
perc=(t/500)*100;
}
};
#include"LinkedList.cpp"
int main() {
LinkedList<Student> list;
Student st;
st.PutStudDetails('a', 1, 400);
list.InsertAfter(st);
st.PutStudDetails('b', 2, 300);
list.InsertAfter(st);
st.PutStudDetails('c', 3, 450);
list.InsertAfter(st);
return 0;
}
5. Given a Linked List of integers, write a function to modify the linked list
such that all even numbers appear before all the odd numbers in the
modified linked list. Also, keep the order of even and odd numbers same.
Answer:
void List::EvenOdd(List &L)
{List lEven,lOdd;
int d;
L.toFirst();
while(!L.curIsEmpty())
{L.retrieveData(d);
if(d%2==0)
lEven.insertEnd(d);
else
lOdd.insertEnd(d);
L.advance();
}
L.makeListEmpty();
lEven.toFirst();
while (! lEven.curIsEmpty() )
{ lEven.retrieveData(d); L.insertEnd (d);
lEven.advance ( );
}
lOdd.toFirst();
while(! lOdd.curIsEmpty() )
{ lOdd.retrieveData (d); L.insertEnd (d);
lOdd.advance ( );
}
}
More exercises:
1. Write a function Count () to count how many times a given number occurs in
the given list. The function takes two arguments; the list and the number you
want to count.
2. Write a function Append () to received two lists ( L1, L2 ) and append L2 to
the end of L1 returning the result in a new list ( L ) without changing the
original lists.
3. Receive a list (L) and to split it at the approximate middle into two separate
lists, returning two new lists (L1) and (L2). After the split, (L) will become
empty.
4. Receive two lists and return true if they are identical, ordered in the same
way. Assume that the two lists are not empty.
5. A public function .Recursive_Reverse( ) to call a recursive private function
to return the list in reverse order.