473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HELP for doubly linked list

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
Nov 17 '05 #1
0 1793

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: surrealtrauma | last post by:
I want to ask what's the differences between doubly liked list and linear liked list, and also the circular doubly liked list in terms of implementation. THX
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
1
by: drewy2k12 | last post by:
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...
8
by: tonywinslow1986 | last post by:
I'm reading MIT's book "Introduction to Algorithms". The following is one of the excercises from it: < 10.2-8 Explain how to implement doubly linked lists using only one pointer value np per...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
4
kim6987
by: kim6987 | last post by:
can you please spend a little time evaluating this code. I can not run it successfully thanks :) #include<stdio.h> #include<conio.h> #include<stdlib.h> #define SIZE 10 typedef struct dlist
10
by: kalar | last post by:
Hello. we have this struct and we must to make a linked list struct node { char name; char phone; struct node *prevName; // previous node alphabetically struct node *prevNumber; //...
1
by: Mahesh | last post by:
Hi Coders, I was asked to write a program to interchange numbers using doubly linked list @ Amazon. Here is the details with Code that i wrote. i/p: 1 2 3 4 5 6 7 8 .....n,n-1. o/p: 2 1 4...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.