CE242-Spring’09-Homework#1
CE242 Data Structures and Algorithms Course @KHas – Spring 2009 – Homework #1 questions and solutions.
CE242 Data Structures and Algorithms course description is available here.
Download questions and solutions
Note: “ce242-spring09-hw1-q1.cpp” needs “List.cpp” module, so link them together while building the code.
Source files:
[List.h] | show
// ==================================================
// - File______: List.h _____________________________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// __________________________________________________
// - Copyright (c) 2009, Some Rights Reserved _______
// ==================================================
#ifndef LIST_H_
#define LIST_H_
/* type definitions */
struct Node
{
void *pData;
Node *pNext;
};
struct List
{
Node *pHead;
Node *pTail;
int size;
};
#define NodeNext(pNode) (pNode)->pNext
#define NodeData(pNode) (pNode)->pData
/* Function declarations */
List *ListCreate();
Node *ListPrepend(List *pList, const void *pData);
Node *ListAppend(List *pList, const void *pData);
void *ListDeleteHead(List *pList);
void *ListDeleteNth(List *pList, int idx);
Node *ListFindNode(List *pList, bool (*compare)(const void *, const void *), const void *pCmpData);
void *ListDeleteNode(List *pList, bool (*compare)(const void *, const void *), const void *pCmpData);
void ListForeach(List *pList, void (*callback)(void *, void *), void *pCallbackData);
void ListTruncate(List *pList);
int ListGetSize(List *pList);
bool ListIsEmpty(List *pList);
void ListDestroy(List *pList);
#endif /* LIST_H_ */
[List.cpp] | show
// ==================================================
// - File______: List.cpp ___________________________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// __________________________________________________
// - Copyright (c) 2009, Some Rights Reserved _______
// ==================================================
#include <cstdlib>
#include "List.h"
List *ListCreate()
{
List *pList;
pList = new List;
pList->pHead = NULL;
pList->pTail = NULL;
pList->size = 0;
return pList;
}
Node *ListPrepend(List *pList, const void *pData)
{
Node *pNode;
pNode = new Node;
pNode->pData = (void *) pData;
pNode->pNext = pList->pHead;
pList->pHead = pNode;
if (pList->size == 0)
{
pList->pTail = pNode;
}
++pList->size;
return pNode;
}
Node *ListAppend(List *pList, const void *pData)
{
Node *pNode;
pNode = new Node;
pNode->pData = (void *) pData;
pNode->pNext = NULL;
if (pList->size == 0)
{
pList->pHead = pNode;
}
else
{
pList->pTail->pNext = pNode;
}
pList->pTail = pNode;
++pList->size;
return pNode;
}
void *ListDeleteHead(List *pList)
{
Node *pNode;
void *pData;
if (pList->size == 0)
{
return NULL;
}
pNode = pList->pHead;
pData = pNode->pData;
pList->pHead = pNode->pNext;
delete pNode;
--pList->size;
if (pList->size <= 1)
{
pList->pTail = pList->pHead;
}
return pData;
}
void *ListDeleteNth(List *pList, int idx)
{
if ((idx < 0) || (idx >= pList->size))
{
return NULL;
}
if (idx == 0)
{
return ListDeleteHead(pList);
}
int i;
void *pData;
Node *pCur, *pPrev;
for (
pPrev = pList->pHead, pCur = pPrev->pNext, i = 1;
pCur != NULL;
pPrev = pCur, pCur = pCur->pNext, ++i
)
{
if (i == idx)
{
pPrev->pNext = pCur->pNext;
pData = pCur->pData;
delete pCur;
--pList->size;
return pData;
}
}
return NULL;
}
Node *ListFindNode(List *pList, bool (*compare)(const void *, const void *), const void *pCmpData)
{
for (Node *pNode = pList->pHead; pNode != NULL; pNode = pNode->pNext)
{
if (compare(pNode->pData, pCmpData))
{
return pNode;
}
}
return NULL;
}
void *ListDeleteNode(List *pList, bool (*compare)(const void *, const void *), const void *pCmpData)
{
Node *pCur, *pPrev;
void *pData;
/* handle first item outside of the loop */
if ((pPrev = pList->pHead) == NULL)
{
return NULL;
}
if (compare(pPrev->pData, pCmpData))
{
pList->pHead = pPrev->pNext;
pData = pPrev->pData;
delete pPrev;
--pList->size;
return pData;
}
/* first item is not what we are looking for, so traverse the entire list */
for (
pCur = pPrev->pNext;
pCur != NULL;
pPrev = pCur, pCur = pCur->pNext
)
{
if (compare(pCur->pData, pCmpData))
{
pPrev->pNext = pCur->pNext;
pData = pCur->pData;
delete pCur;
--pList->size;
return pData;
}
}
return NULL;
}
void ListForeach(List *pList, void (*callback)(void *, void *), void *pCallbackData)
{
for (Node *pNode = pList->pHead; pNode != NULL; pNode = pNode->pNext)
{
callback(pNode->pData, pCallbackData);
}
}
void ListTruncate(List *pList)
{
Node *pNode, *pNodeTmp;
for (pNode = pList->pHead; pNode != NULL; pNode = pNodeTmp)
{
pNodeTmp = pNode->pNext;
delete pNode;
}
pList->pHead = NULL;
pList->pTail = NULL;
pList->size = 0;
}
int ListGetSize(List *pList)
{
return pList->size;
}
bool ListIsEmpty(List *pList)
{
return (pList->size == 0);
}
void ListDestroy(List *pList)
{
ListTruncate(pList);
delete pList;
}
[ce242-spring09-hw1-q1.cpp] | show
// ==================================================
// - File______: ce242-spring09-hw1-q1.cpp __________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// __________________________________________________
// - Copyright (c) 2009, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include "List.h"
using namespace std;
struct Grade
{
int studId;
int grade;
};
int main()
{
List *pListGrades = ListCreate();
int id, grade;
Grade *pGrade;
// read input
while (true)
{
cout << "id: ";
cin >> id;
if (id < 0)
{
break;
}
cout << "grade: ";
cin >> grade;
pGrade = new Grade;
pGrade->studId = id;
pGrade->grade = grade;
ListAppend(pListGrades, pGrade);
}
// create failed and passed lists
List *pListPassed = ListCreate();
List *pListFailed = ListCreate();
// filter main list
for (Node *pNode = pListGrades->pHead; pNode != NULL; pNode = pNode->pNext)
{
Grade *pGrade = (Grade *) pNode->pData;
if (pGrade->grade < 30)
{
ListAppend(pListFailed, pGrade);
}
else
{
ListAppend(pListPassed, pGrade);
}
}
// print lists
cout << "--- all grades ------------------" << endl;
for (Node *pNode = pListGrades->pHead; pNode != NULL; pNode = pNode->pNext)
{
Grade *pGrade = (Grade *) pNode->pData;
cout << "id: " << pGrade->studId << ", grade: " << pGrade->grade << endl;
}
cout << "--- passed grades ---------------" << endl;
for (Node *pNode = pListPassed->pHead; pNode != NULL; pNode = pNode->pNext)
{
Grade *pGrade = (Grade *) pNode->pData;
cout << "id: " << pGrade->studId << ", grade: " << pGrade->grade << endl;
}
cout << "--- failed grades ---------------" << endl;
for (Node *pNode = pListFailed->pHead; pNode != NULL; pNode = pNode->pNext)
{
Grade *pGrade = (Grade *) pNode->pData;
cout << "id: " << pGrade->studId << ", grade: " << pGrade->grade << endl;
}
// destroy passed and failed lists
ListDestroy(pListPassed);
ListDestroy(pListFailed);
// delete all Grade objects pListGrades contains and destroy pListGrades
for (Node *pNode = pListGrades->pHead; pNode != NULL; pNode = pNode->pNext)
{
delete (Grade *) pNode->pData;
}
ListDestroy(pListGrades);
return 0;
}