473,320 Members | 2,158 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,320 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
1 1769
I dont write double linked list myself, I am happy with std::deque.
"drewy2k12" <dr*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@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

Nov 17 '05 #2

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
0
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.