Вы находитесь на странице: 1из 4

Sheet 2 Solution(cont.

)
Problem 2
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "list.h"

using namespace std;

int main()
{
string s;
List l;

ifstream f; //open a stream to read from a file


f.open("test.txt"); //open the file 'test.txt' to read
f >> s; //like 'cin >> s', read a string from file 'f' and put it in string 's'
while(f.good()) //read till the file ends
{
l.insert(s); //insert the string s in the list
f >> s;
}
f.close(); //close the file after finishing

//retrieve the stored string elements from the inorder list


string temp;
int i = 1;
bool notEmpty;

notEmpty = l.first(s);
while(notEmpty)
{
temp = s;
notEmpty = l.next(s);
if(s == temp && notEmpty) i++; //compare last string with the current string to count number of its
occurence
else {
//if the string is read for the first time, print the last string and its number of occurence and
reset the counter

cout << temp << " " << i << endl;


i = 1;
}
}

system("PAUSE");
return EXIT_SUCCESS;
1
}

Problem 4

void List::reverse()
{
Link predN, currentN, nextN;

//at the begining, currentN is the head and its predessor is NUll as it will be the new tail
predN = NULL;
currentN = head;

//The reverse procedure is executed recursively till it reach tail where currentN = Null
while(currentN != NULL)
{
//preserve the next node before breaking the link between currentN and its next
nextN = currentN -> next;
//link between currentN and its predecessor predN
currentN->next = predN;
//move predN and currentN for the next iteration
predN = currentN;
currentN = nextN;
}

//after reversing the list, the new tail is the old head and vice versa
Link temp = head;
head = tail;
tail = temp;

Problem 5

void List::merge(List l)
{
//we want to merge 'list l' into 'this' list cosidering order of the nodes
bool notEmpty;
ListElemType e;

notEmpty = l.first(e);
while(notEmpty) //loop till the end of the list
{
insert(e); //insert e in this list
notEmpty = l.next(e); //get next item in the list
}
}

2
Problem 6
void List::sort()
{
Link N1, N2; //two pointers used to loop within the list

for(N1 = head; N1 != tail; N1 = N1->next) //first loop from head till the node before tail
for(N2 = N1->next; N2 != NULL; N2 = N2->next) //second loop from the next node of N1 till the
tail
if(N1->elem > N2->elem) //to sort the list ascending, compare then swap
{
ListElemType temp = N1->elem;
N1->elem = N2->elem;
N2->elem = temp;
}
}

Problem 7

In this solution, the remove is based on the position of the node in the list:

bool List::remove(int position)


{
//the function returns false if the list is empty of the position is not found in the list

Link pred, deleted; //two pointers used for deletion precedure


int k = 1; //counter used to search for 'position'

if(head == NULL) //check if the list is empty


return false;

if(position == 1) //this means that head will be removed


{
deleted = head;
if(head == tail) //check if there is only one node in the list
head = tail = current = NULL;
else
{
head = head->next; //move the head pointer to the folllowing node in the list
if(current == deleted) current = current->next; //if current points to the head node, move
it to the following node
}
delete deleted;
return true;
}
else{ //we need to set the pointer 'deleted' to the node in the 'position' and 'pred' to its predecessor
pred = head;
while(k != position - 1 && pred != tail) //loop till reaching the predecssor node or reaching the
end of the list

3
{
pred = pred->next;
k++;
}

deleted = pred->next;

if(deleted == NULL) //check if end of the list is reached, so position not found
return false;

if(deleted == tail) //check if the deleted node is the tail


{
tail = pred;
pred->next = NULL;
if(current == tail) current = pred; //if current points to the tail, move it to the predecssor
node
delete deleted;
return true;
}
else{ //the node to delete is in somewhere between head and tail
pred->next = deleted->next;
if(current == deleted) current = current->next;
delete deleted;
return true;
}

Вам также может понравиться