Tuesday, December 9, 2008
U.S. Capitals Lab
Write a program that maintains a linked list of U.S. capitals in alphabetical order. First of all, create a text file containing a few names of state capitals. Your program should get the names from the file, inserting them in the linked list and then call the sorting function and finally display the results.
Create a text file and name it as “stateCapital.txt”
Here is the content of your text file: State Capitals
Montgomery
Juneau
Phoenix
Little Rock
Sacramento
Denver
Hartford
Dover
Tallahassee
Atlanta
Honolulu
Boise
Springfield
Indianapolis
Des Moines
Topeka
Frankfort
Baton Rouge
Augusta
Annapolis
Boston
Lansing
St. Paul
Jackson
Jefferson City
Helena
Lincoln
Carson City
Concord
Trenton
Santa Fe
Albany
Raleigh
Bismarck
Columbus
Oklahoma City
Salem
Harrisburg
Providence
Columbia
Pierre
Nashville
Austin
Salt Lake City
Montpelier
Richmond
Olympia
Charleston
Madison
Cheyenne
Refer to http://www.lakesparadise.com/education/elearning/show_tutorial.php?id=25 for C++ file handling.
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html for C++ String class Examples and Tutorial
Tuesday, December 2, 2008
PRELIM EXAM DETAILS
Time --------------------- Location ----------------- Class
8:00 - 10:00 AM -------- L204 --------------------- IT2A
8:00 - 10:00 AM -------- L203 --------------------- CS2
10:00 - 12:00 AM -------- 302 --------------------- IT2B
10:00 - 12:00 AM -------- 303 --------------------- IT2C
Note:
- Irregular students from IT2A/CS2 may take the exam together with the IT2B/IT2C.
- Be on time. The exam is good for 90 minutes.
- Failure to attend to one of these schedules may cause failure for prelims.
Sunday, November 23, 2008
Sorting items in linked list
Please do not ask regarding the algorithm if your having problem translating it. Learn to read algo. If your sure something is wrong with it, call my attention.
Open your previous devC files and add this algorithm I'm about to give in Node.h.
step 1: Create a function named as Sort (lets assume you want to sort your nodes/items in ascending order) with void as its return type.
step 2: Inside your function, transform this algorithm into a C++ code.
1. Create a pointer (current) of type Node
2. Equate current to head
3. Iterate using while loop until current is null
a. Create another pointer (next) of type Node
b. Equate next to current->link
c. Iterate using a while loop until next is null
i. Test using an if statement whether current’s data value is greater compared to next’s data value (if true, proceed with the bullets list)
Ø Create a pointer (temp) of type Node
Ø Equate temp to a new Node
Ø Assign the values of current (data) to temp (data)
Ø Assign the values of next (data) to current (data)
Ø Assign values of temp (data) to next (data)
Ø Delete temp using the keyword temp
ii. move the next pointer to next->link
d. Move current pointer to current->link
Tuesday, November 18, 2008
LAB ACTIVITY
Advantage of Linked Lists.
Linked lists are used to store collections of data. But we've already seen a mechanism for doing this. i.e. A collection of data can be stored in array. However, an array is a sequential structure meaning that it is a group of memory elements located in contiguous locations (located next to one another in a group). You need to know how many elements there will be and you need to allocate a block of memory for them.When you don't know how many elements there will be, and if elements need to be inserted and deleted frequently, it is better to use a linked list. This is because each element for a linked list is created as needed and can be stored anywhere in the free memory space - the elements in the list do not have to be stored in contiguous locations.
Each element in a linked list is referred to as a node. In a simple singly linked list, each element has two fields:
- Data member - contains the data (in our implementation, this is called datum)
- Link member - contains the pointer to the next node (in our implementation, this is called next)
- Note: we are going to represent the "link" part as "next" variable
class Node
{
private:
int data;
Node* next;
public:
Node();
void insertItem (int);
void makeList ();
void appendItem (int);
void deleteItem (int);
void printList ();
};
A couple of notes on linked lists are:
- if you were implementing a doubly linked list, the above class would have an additional data member to define a pointer to the previous node.
- the last node in any linked list contains a null pointer in the link member.
- the null pointer is used to determine the end of the list.
- notice that there is a constructor in the above implementation that can be used to set the datum and next members.
The following diagram illustrates a simple singly linked list.
In this lab, we use a class Node to create a linked list.
Operations on Linked Lists.
What follows here, are very rough algorithms on the basic operations that you would perform on a linked list. In actually implementing list operations, you would have to consider conditions such as: Is the list empty? What happens if I get to the null pointer and haven't found a data member that is supposed to be in the list? The algorithms here are just to provide you with an overview of the concepts involved in simple operations.- Creating a List (makeList())
First, create a node initialized with a data value and a NULL pointer.
Set the "head" to point to the first node.
Set up a current-pointer to the first node (or "head").
Get a data value for the next node.
While more nodes to add
{
Create a new node initialized with the data value and a NULL pointer.
Set the current-pointer link member ("next") to the new node.
Set the current-pointer to point to the new node.
Get a data value for the next node.
}
- Adding a Node (appendItem(int item))
//Find the end of the list...
Set a current-pointer to the "head".
While current-pointer link member ("next") is not NULL
{
Set the current-pointer to the "next" node in the list.
}
//Now current-pointer points to the last node...
Create a new node initialized with the "item" and a NULL pointer.
Set the current-pointer link member ("next") to this new node.
- Printing Nodes (printList())
Set a current-pointer to the "head".
While current-pointer is not NULL
{
Print the data member ("datum") of the current node
Set the current-pointer to the "next" node in the list.
}
- Inserting Nodes (insertItem(int item))
This algorithm assumes that you want to insert a node into a list of ordered values. Let's say you have two values, 12 and 24. A new value of 14 should be inserted between these existing values so that the final list will contain the values 12, 14, and 24.Set a previous-pointer to NULL.
This is much easier to understand if you visualize the nodes and the pointers as they are changed. The following diagram attempts to illustrate this process.
Set a current-pointer to the "head".
//Find where your want to insert into the list...
While the new data member > current-pointer value
{
//Move both pointers along in the list...
Set the previous-pointer to the current-pointer
Set the current-pointer to the "next" node in the list.
}
Create a new node initialized with the "item" and current-pointer.
Set the link member in the previous_pointer to the new node.
- Deleting Nodes (deleteItem(int item))
This routine assumes that the item you are searching for is, in fact, in the list.
If "item" is in the first node
This is much easier to understand if you visualize the nodes and the pointers as they are changed. The following diagram attempts to illustrate this process.
{
Set a delete-pointer to the first node.
Change the "head" pointer to the second node.
}
Else
{
Set a current-pointer to the "head".
While current-pointer->next->datum is not equal to "item"
{
Set the current-pointer to the "next" node in the list.
}
Set a delete-pointer to the node to be deleted (Note: it is
the node after current-pointer).
Set the link member ("next") of the current-pointer to the
"next" node after delete-pointer.
}
Delete the node indicated by delete-pointer.
Thursday, November 6, 2008
GradeBook Assign
grade = cin.get();
3. 6 functions are included in the class:
- GradeBook(string input);
- void displayMessage();
- void setValue(string input);
- string getValue();
- void setGrades();
- void displayGrades();
- string courseName;
- int aCount;
- int bCount;
- int cCount;
- int dCount;
- int eCount;
- int fCount;
Output sample:
Sunday, October 26, 2008
Introduction to the subject
Quick Syllabus
To become data struct experts, you need to master the following topics:
1. Precondition/Postcondition specifications
2. Self-Referential Classes
3. Dynamic Memory Allocation and Data Structures
4. Linked List
5. Stacks
6. Queues
7. Trees
Grading System
Lec - 70% - Quizzes(40%), Attendance(5%), Class Recitation(5%), Long Tests(20%), Exam(30%)
Lab - 30% - Quizzes(50%), Attendance (5%), Exam(45%)
note: Only 4 absences are allowed for the whole duration of the semester.
- Late Homework
- Homework will be accepted up to 24 hours after the deadline with a 5% penalty. No homework will be accepted after that point, so please submit your work within 24 hours of the deadline even if the work is only partly completed.
- Missed Exams or Quizzes
- Makeup exams or quizzes can be arranged only if you inform me at least 24 hours ahead of time or if an unavoidable problem causes you to miss class. Other missed exams or quizzes will be recorded as a zero score. A medical certificate should be submitted to me to validate your reason for absence.
Book References: C++ How to Program, fifth edition by Deitel