Computer Science Worksheet

ADVERTISEMENT

King Abdullah II School for Information Technology – Data Structures
University of Jordan
Mid term exam
24/3/2009
‫اﺳﻢ اﻟﻤﺪرس‬
‫اﺳﻢ اﻟﻄﺎﻟﺐ‬
...................................:
.........................................................:
Student . #:_ _
Section #:______
Seat #:_
Given the following definition of a Singly Linked List, use the functions whose prototype
Q1)
appear in the public or Private section (if needed) to answer questions A-C :
class SLList
{
private:
SLLNode * head;
// points to the first node in a list
void copyList(const SLList & otherList); //copy from one list to another
public:
int size( );
//return number of nodes
void deleteNode(int el);
//delete node from linked list
bool Palindrome(int el);
//check number Palindrome(if you read it
//from left or right it's the same) or not
};
A. Write the Copy Constructor for the above class. (3 marks)
SLList::SLList(const SLList & otherList)
{
è
1 Mark
head = NULL;
è
2 Mark
copyList(otherList);
}
B. Define a member function sort that will sort the elements in the above list in ascending
( ‫ ) ﺗﺼﺎﻋﺪي‬order.
(7 marks)
void SLList::sort()
{
SLLNode *tmp;
1 Mark
int data;
è1 Mark
for(int i=0;i<size ()-1;i++)
{
è1 Mark
tmp=head;
è1 Mark
for(int j=0;j<size ()-1-i;j++)
{
è 1 Mark
if(tmp->data>tmp->next->data)
{
data=tmp->data;
1 Mark
tmp->data=tmp->next->data;
tmp->next->data=data;
}
è1 Mark
tmp=tmp->next;
}
}
}
1

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3