Sie sind auf Seite 1von 16

Andrew Petrik & Anh Nguyen

EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Lab Assignment 2
Linked Lists and the gdb Debugger
Andrew Petrik & Anh Nguyen
petrik.an@husky.neu.edu
nguyen.anh3@husky.neu.edu

Submit Date: 10/3/16


Due Date: 10/3/16

Assignment 1

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

The print person command prints out the memory location of where the data is stored. The print
*person command prints out all of the data stored where the pointer points to. In other words, all
of the data that makes up this person. The print personname command prints out the data that
is stored in the name field of the person structure. The print personage command prints out the
data that is stored in the age field of the person structure for this person.
Assignment 2
#include <iostream>
#include <string>
using namespace std;
// Linked List Management Code
struct Person
{
// Unique identifier for the person
int id;
// Information about person
string name;
int age;
// Pointer to next person in list
Person *next;
};
struct List
{
// First person in the list. A value equal to NULL indicates that the
// list is empty.
Person *head;
// Current person in the list. A value equal to NULL indicates a
// past-the-end position.
Person *current;
// Pointer to the element appearing before 'current'. It can be NULL if
// 'current' is NULL, or if 'current' is the first element in the list.
Person *previous;

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1



// Number of persons in the list
int count;
};
// function prototypes
void DisplayMenu ();
void ListInitialize(List *list);
void ListNext(List *list);
void ListHead(List *list);
Person *ListGet(List *list);
void ListFind(List *list, int id);
void ListInsert(List *list, Person *person);
void ListRemove(List *list);
void PrintPerson(Person *person);
// main function: Will create and process a linked list
int main() {
List list;
// Create the main list
ListInitialize(&list);
// Initialize the list
DisplayMenu();
//declare variables
string new_name;
int new_age, new_id;
int idCount =1;
//wait for user input
int option;
cout << "Select an option: ";
cin >> option;

//display error if invalid input


while (1==1) {
switch (option) {
case 1:{
cout << "You selected \"Add a person\"" << endl;
//user input
cout << "Please enter the name: ";
cin >> new_name;
cout << "Please enter the age: ";
cin >> new_age;
//temp person
Person * temp = new Person;
//assign values
temp->name = new_name;
temp->age = new_age;
list.count++;
temp->id = idCount;
idCount++;
//insert person
ListInsert (&list, temp);
PrintPerson (list.current);
break;}
case 2:{
cout << "You selected \"Find a person\"" << endl;
//user input
cout << "Please enter the ID: ";
cin >> new_id;

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1



Person *temp_current = list.current;
//find person with id & return it
ListFind(&list, new_id);
ListGet(&list);
//conditional for possible error input
if (!list.current) {
cout << "Your input is invalid." << endl;
} else {
PrintPerson (list.current); //print the person
}
list.current = temp_current;
break;}
case 3:{
cout << "You selected \"Remove a person\"" << endl;
//user input
cout << "Please enter the ID: ";
cin >> new_id;
Person *temp_current = list.current;
//find person with id & return it
ListFind(&list, new_id);
ListGet(&list);
//conditional for possible error input
if (!list.current) {
cout << "Your input is invalid" << endl;
} else {
ListRemove(&list);
list.count--;
}
list.current = temp_current;
break;}
case 4:{
cout << "You selected \"Print the list\"" << endl;
ListGet(&list);
Person* temp_current = list.current;
ListHead(&list);
//move pointer to the start of list
//verify whether list is empty
if (!list.current) {
cout << "The list is empty" << endl;
} else {
for (int i=0; i<list.count; i++) {
PrintPerson(list.current);
//print
the current person
ListNext(&list);
//move
to the next person
ListGet(&list);
//retrieve the current element
}
}
list.current = temp_current;
break;}
case 5:{
cout << "You selected \"Exit\"" << endl;
return 0;}
default:{
cout << "Your input is invalid." << endl;
break;}
}
DisplayMenu();
cout << "Select an option: ";
cin >> option;
}

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1



//end
return 0;
} //end main
void DisplayMenu () {
//display the menu
cout << "\nMain Menu:" << endl;
cout << "1. Add a person" << endl;
cout << "2. Find a person" << endl;
cout << "3. Remove a person" << endl;
cout << "4. Print the list" << endl;
cout << "5. Exit" << endl;
}
// Give an initial value to all the fields in the list.
void ListInitialize(List *list)
{
list->head = NULL;
list->current = NULL;
list->previous = NULL;
list->count = 0;
}
// Move the current position in the list one element forward. If last element
// is exceeded, the current position is set to a special past-the-end value.
void ListNext(List *list)
{
if (list->current)
{
list->previous = list->current;
list->current = list->current->next;
}
}
// Move the current position to the first element in the list.
void ListHead(List *list)
{
list->previous = NULL;
list->current = list->head;
}
// Get the element at the current position, or NULL if the current position is
// past-the-end.
Person *ListGet(List *list)
{
return list->current;
}
// Set the current position to the person with the given id. If no person
// exists with that id, the current position is set to past-the-end.
void ListFind(List *list, int id)
{
ListHead(list);
while (list->current && list->current->id != id)
ListNext(list);
}
// Insert a person before the element at the current position in the list. If
// the current position is past-the-end, the person is inserted at the end of
// the list. The new person is made the new current element in the list.
void ListInsert(List *list, Person *person)
{
// Set 'next' pointer of current element
person->next = list->current;
// Set 'next' pointer of previous element. Treat the special case where
// the current element was the head of the list.

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1



if (list->current == list->head)
list->head = person;
else
list->previous->next = person;
// Set the current element to the new person
list->current = person;
}
// Remove the current element in the list. The new current element will be the
// element that appeared right after the removed element.
void ListRemove(List *list)
{
// Ignore if current element is past-the-end
if (!list->current)
return;
// Remove element. Consider special case where the current element is
// in the head of the list.
if (list->current == list->head)
list->head = list->current->next;
else
list->previous->next = list->current->next;
// Free element, but save pointer to next element first.
Person *next = list->current->next;
delete list->current;
// Set new current element
list->current = next;
}
void PrintPerson(Person *person)
{
cout << "Person with ID: " << person->id << endl;
cout << "\tName: " << person->name << endl;
cout << "\tAge: " << person->age << endl << endl;;
}

Andrew Petrik & Anh Nguyen


EECE 2160

Assignment 3

Option 1 is adding a person to the list in the manner as expected.

Embedded Design: Enabling Robotics


Lab Assignment 1

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Option 2 is finding a person as intended. If an ID is given that does not exist the input is stated to
be invalid and the main menu is shown again.

Andrew Petrik & Anh Nguyen


EECE 2160

Option 4 prints the list in order.

Embedded Design: Enabling Robotics


Lab Assignment 1

Andrew Petrik & Anh Nguyen


EECE 2160

Option 3 removes the person with the ID given.

Option 5 ends the program.

Embedded Design: Enabling Robotics


Lab Assignment 1

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Assignment 4

Printing out list printed the fields of list, with the pointers holding what memory locations they
are pointing to and with the count being 1 (since there is only one person in the list). Printing
list.head just printed out the head field. Printing list.headnext prints out the location where the
pointer next in the person James is pointing to. Since it has not been assigned anything it is
pointing at NULL.

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Assignment 5
The code in case 3 within the switch operator originally intended to remove a person from the
list and rearrange the people on the list accordingly. An intermediate version of this code was
which without a decremented value of the list.count variables, as listed below:
case 3:{
cout << "You selected \"Remove a person\"" << endl;
//user input
cout << "Please enter the ID: ";
cin >> new_id;
Person *temp_current = list.current;
//find person with id & return it
ListFind(&list, new_id);
ListGet(&list);
//conditional for possible error input
if (!list.current) {
cout << "Your input is invalid" << endl;
} else {
ListRemove(&list);
}
list.current = temp_current;
break;}

The program is run to add 3 people as similar to above, and then remove the person with the
number ID of 2, and finally print out the list of the people again on the screen, which crashed
after printing.

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Gdb was prompted in order to debug this crash as followed. Two person was added to the list
using the option 1, and one with ID number 1 was removed using option 3.

(gdb) start and next were used to follow the step of the program into the loop and into the switch
to notify whether there is any mistakes in the looping of the option selections. Next commands
were used continuously until encountered with a bug after using the option 4 to print the person
list, which is shown as followed

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

As apparently, the program crash after it enters the printing loop within the option 4, where it
prompts a counting number i, and increment it every time it prints a person, until it reaches the
count of people within the list. The program did successfully printed the first person where i=0;
but crashes from i=1 onwards. Thus, steps were used to step inside of the loop to investigate the
bug, where it was shown as below to crash when prompt to print the current person where the
list.count is 2, i=1, but there was no data of the person there.

Andrew Petrik & Anh Nguyen


EECE 2160

Embedded Design: Enabling Robotics


Lab Assignment 1

Das könnte Ihnen auch gefallen