preview

Assignment

Decent Essays

#include (-- removed HTML --)

#include (-- removed HTML --)

#include (-- removed HTML --)

using namespace std;

class Node {

int data;

class Node * next;

public:

//Default constructor for Node

Node() { data = 0; next = NULL; }

Node(int data) { this->data = data; next = NULL; }

Node * getNext() { return next; }

void setNext(Node * next) { this->next = next; }

int getData() { return data; }

};

class List {

class Node *head;

unsigned int size;

public:

List() { head = NULL; size = 0; }

//append to end of the list

void append(int data) {

//create a new node and set data

class Node * temp = new Node(data);

//traverse till end of the list

class Node * ptr = head;

while (ptr != NULL && ptr->getNext() != NULL) {

ptr = …show more content…

d int count = 0;

//check if position entered is valid

if (pos > size) {

cout << "Invalid position entered" << endl;

return NULL;

}

class Node * ptr = head;

while (count != pos) {

ptr = ptr->getNext();

count++;

}

return ptr;

}

//print all the current keys to command line in one line

void printAll() {

unsigned int i;

class Node *ptr;

for (i = 0; i < size; i++) {

ptr = get(i);

cout (-- removed HTML --) getData() << " ";

}

cout << endl;

}

};

int main() {

List *list = new List();

string line;

//replacing std input with content of data.txt

freopen("data.txt", "r", stdin);

//read first line

getline(cin, line);

char data[100] = "\0";

char ps[100] = "\0";

int num, i = 0, j = 0;

//append elements to list

while (line[i] != '\0') {

if (line[i] >= '0' && line[i] <= '9') {

j = 0;

//extract a number

while (line[i + j] != '\0' && line[i + j] != ',') {

data[j] = line[i + j];

j++;

}

data[j] = '\0';

num = atoi(data);

list->append(num);

if (line[i + j] == '\0')

break;

i = i + j;

}

i++;

}

//read second line

getline(cin, line);

i = 0;

j = 0;

//delete elements from list

while (line[i] != '\0') {

if (line[i] >= '0' && line[i] <= '9') {

j = 0;

//extract a number

while (line[i + j] != '\0' && line[i + j] != ',') {

data[j] = line[i + j];

j++;

}

data[j] = '\0';

num = atoi(data);

list->del(num);

if (line[i + j] == '\0')

break;

i = i + j;

}

i++;

}

//read third line

getline(cin, line);

i = 0;

j = 0;

int pos = 0;

//inserting

Get Access