Re: HELP for doubly linked list
From: Onega (www.fruitfruit.com)
Date: 03/02/04
- Next message: zhang allen: "About winzip API"
- Previous message: drewy2k12: "HELP for doubly linked list"
- In reply to: drewy2k12: "HELP for doubly linked list"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 2 Mar 2004 15:57:47 +0800
I dont write double linked list myself, I am happy with std::deque.
"drewy2k12" <drewy2k12@hotmail-dot-com.no-spam.invalid> wrote in message
news:40442785$3_2@Usenet.com...
> Heres the story, I have to create a doubly linked list for
> class, and i have no clue on how to do it, i can barely create a
> single linked list. It has to have both a head and a tail pointer,
> and each node in the list must contain two pointers, one pointing
> forward and one pointing backwards.
> Each node in the list will contain 3 data values: an item ID
> (string), a quantity (integer) and a price (float). The ID will
> contain only letters and digits ?no spaces or other characters (for
> example, P123CD). The list must have the following capabilities:
> ?You must be able to insert a new node at the head of the
> list.
> ?You must be able to insert a new node at the tail of the
> list.
> ?You must be able to output all data in the list to an
> arbitrary file stream, one line per node, forward (from head to
> tail), followed by a blank line.
> ?You must be able to output all data in the list to an
> arbitrary file stream, one line per node, backward (from tail to
> head), followed by a blank line.
>
> Input for the program will come from a file whose name is "Prog-
> 3.txt". Each line of the file will begin with an op-code that
> consists of a single character: H ?insert at head, T ?insert at
> tail, F ?display contents forward, B ?display contents backwards,
> Q ?quit. Output will be to a file whose name is the first 4 letters
> of your last name, followed by "-3.txt". For example, Meye-3.txt
>
> For example, the data file might contain the following lines:
>
> H P123CD 100 4.95
> H T449RS 200 9.95
> F
> T Q987AB 50 3.00
> B
> Q
>
> And the output to file would look like:
>
> T449RS 200 9.95
> P123CD 100 4.95
>
> Q987AB 50 3.00
> P123CD 100 4.95
> T449RS 200 9.95
>
> The code i have is this but its not complete and has alot of errors:
>
> #include<iostream>
> using namespace std;
>
> class node { // node structure for simple linked list
> private:
> int data; // data is an integer value
> node* link; // pointer to next node in list
> string itemid;
> int quant;
> float price;
> public:
> node(); //default constructor - makes link null
> node(int, int, string, float); //initialization constructor - makes
> link null
> void setData(int); //assign data value to an existing node
> void setLink(node*);//assign link value to an existing node
> int getData(); //returns value of data field of node
> node* getLink(); //returns value of link field of node
> void showData(); // output data value of node (to console)
> };
>
> class list {
> private:
> node* head;
> node* tail;
> public:
> list(); //create an empty list
> ~list(); //destructor
> bool isEmpty(); //returns true if list is empty, false otherwise
> void insertHead(node*); //insert referenced node at head of list
> void insertTail(node*);
> void outputList(); //output all data from list
> int sizeOf(); //returns number of nodes in list
> };
>
>
> void buildList(list L); // function to build a list for demo
> purposes
>
>
//**************************************************************************
*******************
>
> void main() { // build, output and destroy 3 lists
> /* buildList(8);
> buildList(5);
> buildList(7);
> */
> }
>
>
//**************************************************************************
*******************
> /*
> void buildList(list L) {
> list L;
> node *p;//define a list
> for(int i=1; i<n+1; i++) { //create and insert 4 nodes with data
> 4, 8, 12, 16
> p = new node(4*i);
> L.insertHead(p);
> }
> cout << "The list L contains " << L.sizeOf() << "
> nodes." << endl;
> L.outputList(); //output data values in list
> cout << "***************" << endl;
> }
> */
>
//**************************************************************************
*******************
>
> node::node(){ //default constructor - makes link null
> link = 0;
> string itemid = 0;
> int quant = 0;
> float price = 0;
> }
>
> node::node(int d, int q, string id, float p){ //initialization
> constructor - makes link null
> data = d;
> link = 0;
> itemid = id;
> quant = q;
> price = p;
> }
>
> void node::setData(int d){ //assign data value to an existing node
> data = d;
> }
>
> void node::setLink(node* p){//assign link value to an existing node
> link = p;
> }
>
> int node::getData(){ //returns value of data field of node
> return data;
> }
>
> node* node::getLink(){ //returns value of link field of node
> return link;
> }
>
> void node::showData(ostream& out){ // output data value of node
> (to console)
> out << data << '\t';
> }
>
>
//**************************************************************************
********************
>
> list::list(){ //create an empty list
> head = 0;
> tail =0;
> }
>
> list::~list() {
> node* p = head;
> while(p!=0) {
> head = p->getLink();
> delete p;
> p = head;
> }
> }
>
> bool list::isEmpty(){ //returns true if list is empty, false
> otherwise
> return (head==0);
> }
>
> void list::insertHead(node* p){ //insert referenced node at head of
> list
> if (head == 0){
> p->setLink(head);
> head = p;
> tail = p;
> }
> else {
> p->setLink(head);
> head = p;
>
> }
> }
>
> void list::insertTail(node* p){
> if (tail == 0){
> p->setLink(tail);
> head = p;
> tail = p;
> }
> else{
> p->setLink(tail);
> tail =p;
> }
> }
>
> void list::outputList(){ //output all data from list
> node *p=head;
> while(p!=0) {
> p->showData(); // output data in node
> p = p->getLink();
> }
> cout << endl; // end line of output
> }
>
> int list::sizeOf(){ //returns number of nodes in list
> node *p=head;
> int n=0;
> while(p!=0) {
> n++;
> p = p->getLink();
> }
> return n;
> }
>
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
- Next message: zhang allen: "About winzip API"
- Previous message: drewy2k12: "HELP for doubly linked list"
- In reply to: drewy2k12: "HELP for doubly linked list"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|