August 2000
CA208 : 'C' PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 2

You are to implement a simple system which is used to store details of all the web pages accessed by a computer user. A web page is identified by the protocol type, which can be one of http, ftp, or cgi ; a URL (Uniform Resource Locator), which is a string of arbitrary length, and a server address, which is the composition of four numbers in the range 0 – 255 (i.e., four 8-bit numbers).
Consider the following definition of a list in C, which can be used to store details of web pages:
  typedef struct ListElement {
  WebPage Page;
  struct ListElement* NextPage;
} LE;

(a) Define a suitable enumerated type, called Protocol, which can take on any of the three protocol values http, ftp, or cgi. [1 mark]
(a) enum Protocol {http, ftp, cgi}; [1 mark]

(b) Define the data structure WebPage, such that it contains the protocol type, a pointer to a string containing the URL, and each of the four 8 bit numbers which make up the server address. You may assume that the data type char is an 8 bit wide type. [4 marks]
(b) A sample definition of WebPage follows:
typedef struct {
Protocol p;
char* url;
char a, b, c, d;
} WebPage;
And the following marking scheme should be used:
• A correctly defined struct; (1 mark)
• Including the Protocol member; (1 mark)
• Including a char* for the url; (1 mark)
• Including the four bytes a, b, c and d as char types. (1 mark)
[4 marks]

(c) Implement a function, called Find, the signature of which is given below, which takes a reference to a ListElement, and a reference to a WebPage and returns 1 if the WebPage is found in the list, or 0 otherwise. You may use the function Match, the signature of which is given below, which returns 1 if both parameters are identical, and 0 otherwise. You are not required to implement Match
int Match(Webpage a, WebPage b);
int Find(const ListElement* L, WebPage* W); [5 marks]
(c) A sample definition of Find follows:
int Find(const ListElement* L, WebPage* W)
{
  for(L; L->NextPage!= NULL; L= L->NextPage) {
    if( Match(L->page, *W)== 1)
    return 1;
  }
  return 0;
}
And the following marking scheme should be used:
• Declaring a suitable iterative structure which moves along the list; (1 mark)
• Bound by the end of the list; (1 mark)
• Using the Match function; (1 mark)
• With correctly typed parameters; (1 mark)
• Returning the appropriate result when a match is found, or at the end of the search;
(1 mark)

(d) Implement a function, called LastURL, which takes a reference to a ListElement and will return a reference to the URL of the last WebPage stored in the list The function should not be allowed to change the LastURL. [5 marks]
(d) A sample definition of LastURL follows:
char* LastURL(const ListElement* L) {
  if(L== NULL) return NULL;
  for(L; L!= NULL; L= L->NextPage) {}
  return L->page.URL;
}
And the following marking scheme should be used:
• Declaring a suitable function signature, with return type and parameter list; (1 mark)
• Testing for the case where L is null; (1 mark)
• Declaring a suitable iterative structure; (1 mark)
• Which increments the pointer on each iteration; (1 mark)
• Returning the correct reference when the end of the list is reached; (1 mark)
[5 marks]