473,788 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked List Question

beacon
579 Contributor
As you can probably tell from the title, I'm a little frustrated with linked lists. I'm working on a homework assignment and it just isn't making sense to me. I've read and read and read on the subject and I've checked out sample code, but I can't for the life of me get to methods in my class to work, let alone make sense. I've talked to my professor and to tutors at school, but I'm no better now than I was 2 weeks ago when the program was assigned.

Here's the gist:

1) I have to create a linked list that will sort in ascending or descending order based on the user's input and it has to be in a class. The list will read in data from a file that will be named at runtime(???). If it is not named it will get a generic filename.

2) I have to create an append method that will place a new node at the end of the list.

3) I have to create an insert method that will place a new node in the correct order (ascending or descending) in the list.

4) I have to create a print method to output the data.

First off, I have no idea what should go in my constructor. Should I prompt the user to enter 'd' or 'a' and then call the constructor based on their input?

Secondly, I can't get the insert method to work to save my life. I keep getting lost in an infinite loop and don't know why.

Anyway, here's my code so far if anyone would like to help.

Thanks...

Expand|Select|Wrap|Line Numbers
  1. //*** MAIN ***
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include "SortedList.h"
  6.  
  7. using namespace std;
  8.  
  9. void fileName();
  10.  
  11. void main(){
  12.  
  13.     fileName();
  14.  
  15.     /*string order;
  16.     cout<<"Enter 'A' for ascending or 'D' for descending ";
  17.     cin>>order;
  18.  
  19.     if(order == 'D'){
  20.         SortedList list("descending");
  21.     }
  22.     else
  23.         SortedList list("ascending");*/
  24.  
  25.     cin.clear();
  26.  
  27.     SortedList list("ascending");
  28.  
  29.     list.appendNode(23);
  30.  
  31.     list.printList();
  32. }
  33.  
  34. void fileName(){
  35.  
  36.     ifstream infile;
  37.  
  38.     cout<<"Please enter a name for your file: ";
  39.     string x;
  40.     getline(cin,x);
  41.  
  42.     if(x == "\0"){
  43.         cout<<"Your filename will be nums.in";
  44.         infile.open("nums.in");
  45.     }
  46.     else{
  47.         cout<<"Your filename will be "<<x.c_str()<<endl<<endl;
  48.         infile.open(x.c_str());
  49.     }    
  50. }
Expand|Select|Wrap|Line Numbers
  1. // SortedList.cpp: implementation of the SortedList class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include "SortedList.h"
  9.  
  10. using namespace std;
  11.  
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15.  
  16. SortedList::SortedList(string order)
  17. {
  18.  
  19.     if(order == "descending") 
  20.         cout<<"so you want a descending list"<<endl; 
  21.     else 
  22.         cout<<"so you want an ascending list"<<endl; 
  23.     head = NULL; 
  24.  
  25. }
  26.  
  27. SortedList::~SortedList()
  28. {
  29.  
  30. }
  31.  
  32. /*void SortedList::appendNode(int inData){
  33.  
  34.     node *Ptr = head; // combining the steps; create a pointer and assign it to head
  35.                              //Ptr = head;
  36.     node *nodePtr;
  37.     nodePtr = new node;
  38.  
  39.     nodePtr->data = inData;
  40.     nodePtr->next = NULL;
  41.  
  42.     while(Ptr != NULL){
  43.         if(Ptr->next == NULL){
  44.             Ptr->next = nodePtr;
  45.  
  46.         }
  47.         else//(nodePtr->next != NULL)
  48.             Ptr = Ptr->next;
  49.     }
  50. }*/
  51.  
  52. void SortedList::appendNode(int inData){
  53.     node *Ptr; 
  54.  
  55.     node *nodePtr;             
  56.     nodePtr = new node; 
  57.     nodePtr->data=inData; 
  58.     nodePtr->next=NULL; 
  59.  
  60.     if(head==NULL) 
  61.         head = nodePtr; 
  62.     else{
  63.         Ptr= head;
  64.         while(Ptr->next!=NULL){
  65.             Ptr=Ptr->next;
  66.         }
  67.         Ptr->next=nodePtr;
  68.     }
  69.     /*else{ 
  70.         Ptr = head; 
  71.         while(Ptr->next){ 
  72.             Ptr = Ptr->next; 
  73.         } 
  74.         Ptr->next = nodePtr; 
  75.     } */
  76. }
  77.  
  78. void SortedList::insertNode(int inData){
  79.  
  80.     node *Ptr = head;
  81.     node *tempPtr = head->next;
  82.  
  83.     node *nodePtr;
  84.     nodePtr = new node;
  85.     head->next = nodePtr;
  86.     nodePtr->data = inData;
  87.     nodePtr->next = NULL;
  88.  
  89.     while(Ptr != NULL){
  90.         if(Ptr->data > tempPtr->data)
  91.         {
  92.             Ptr->next = nodePtr;
  93.             nodePtr->next = tempPtr;
  94.             tempPtr = tempPtr->next;
  95.         }
  96.         else //(Ptr->data < tempPtr->data)
  97.         {
  98.             Ptr = Ptr->next;
  99.             tempPtr = tempPtr->next;
  100.         }
  101.     }
  102. }
  103.  
  104. void SortedList::printList(){
  105.     node *Ptr;
  106.     node *nodePtr;
  107.     Ptr=head;
  108.     nodePtr = head;
  109.     while(nodePtr!=NULL){
  110.         cout<<nodePtr->data<<endl;
  111.         nodePtr = nodePtr->next;
  112.     }
  113. }
  114.  
  115. void SortedList::revprintList(){
  116.  
  117. }
  118.  
  119. void SortedList::graphvizOut(){
  120.  
  121. }
  122.  
  123. /*void IntList::AppendNode(int data) 
  124.  
  125.     node *Temp; 
  126.  
  127.     node *NewNodePtr;             
  128.     NewNodePtr = new node; 
  129.     NewNodePtr->data=data; 
  130.     NewNodePtr->next=NULL; 
  131.  
  132.     if(head==NULL) 
  133.         head = NewNodePtr; 
  134.     else{ 
  135.         Temp = head; 
  136.         while(Temp->next){ 
  137.             Temp = Temp->next; 
  138.         } 
  139.         Temp->next = NewNodePtr; 
  140.     } 
  141. }*/
  142.  
Expand|Select|Wrap|Line Numbers
  1. // SortedList.h: interface for the SortedList class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. #if !defined(AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_)
  12. #define AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_
  13.  
  14. #if _MSC_VER > 1000
  15. #pragma once
  16. #endif // _MSC_VER > 1000
  17.  
  18. typedef int nodetype;
  19.  
  20. struct node{
  21.     nodetype data;
  22.     node*next;
  23.     node*prev;
  24. };
  25.  
  26. class SortedList  
  27. {
  28. private:
  29.     node *head;
  30. public:
  31.     SortedList(string);
  32.     ~SortedList();
  33.     void inputFile();
  34.     void appendNode(int);
  35.     void insertNode(int);
  36.     void printList();
  37.     void revprintList();
  38.     void graphvizOut();
  39.  
  40. };
  41.  
  42. #endif // !defined(AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_)
  43.  
Oct 9 '07 #1
19 2534
weaknessforcats
9,208 Recognized Expert Moderator Expert
I believe your design is at fault.

Since this is a linked list, your class should be appropriately named:
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.  
  4. };
  5.  
  6.  
A linked list is a series Nodes connected by addresses. The Node contains the data.
Expand|Select|Wrap|Line Numbers
  1. class Node
  2. {
  3.      Datatype* the data;
  4.      Node* next;
  5.      Node* prev;
  6. };
  7.  
The LinkedList just need info on the start, end and current position (in the cas eof a traverse) of the list:
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.  
  7. };
  8.  
The only mention of Node next/prev pointers will be in the Node member functions.

The Node class does not know there is a list. All it knows is that there are Nodes with data. That means you can write twomethods on the Node to insert the this object before or after the Node used on the call.

Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.    public:
  7.      void InsertAfter(Node* first);
  8.      void InsertBefore(Node* first);
  9.  
  10. };
  11.  
To sort the list, you just need a method on the LinkedList class
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.  
  7.    public:
  8.      enum Sequence {ASCENDING, DESCENDING};
  9.      void Sort(enum Sequence);
  10.  
  11. };
  12.  
The Datatype for the data in the Node is a second level of development. Initially, I would just use an int until the linked list code is working. Then I would create a template class for the Datatype.
Oct 9 '07 #2
beacon
579 Contributor
I believe your design is at fault.

Since this is a linked list, your class should be appropriately named:
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.  
  4. };
  5.  
  6.  
A linked list is a series Nodes connected by addresses. The Node contains the data.
Expand|Select|Wrap|Line Numbers
  1. class Node
  2. {
  3.      Datatype* the data;
  4.      Node* next;
  5.      Node* prev;
  6. };
  7.  
The LinkedList just need info on the start, end and current position (in the cas eof a traverse) of the list:
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.  
  7. };
  8.  
The only mention of Node next/prev pointers will be in the Node member functions.

The Node class does not know there is a list. All it knows is that there are Nodes with data. That means you can write twomethods on the Node to insert the this object before or after the Node used on the call.

Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.    public:
  7.      void InsertAfter(Node* first);
  8.      void InsertBefore(Node* first);
  9.  
  10. };
  11.  
To sort the list, you just need a method on the LinkedList class
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.     Node* start;
  4.     Node* end;
  5.     Node* current;
  6.  
  7.    public:
  8.      enum Sequence {ASCENDING, DESCENDING};
  9.      void Sort(enum Sequence);
  10.  
  11. };
  12.  
The Datatype for the data in the Node is a second level of development. Initially, I would just use an int until the linked list code is working. Then I would create a template class for the Datatype.
I appreciate your help, but we haven't learned enum, templates, or the 'this' object yet. I'm sure that knowing these things would make my program much easier, but I'm pretty much confined to what is contained herein.
Oct 9 '07 #3
arnaudk
424 Contributor
Use enums, they're very simple and make your code more readable, it'll take you two minutes to learn how they work.

enum Sequence {ASCENDING, DESCENDING};
void Sort(enum Sequence);

is just pretty way of saying

int Sequence;
void Sort (int Sequence);

where you decide that Sequence=0 means ascending, Sequence=1 means descending.

You don't need to use templates at this stage if you don't want to. Just replace DataType by int or whatever, or you can typedef DataType as an alias to some other type. You can implement the suggestions above without using the this pointer.

Finally, there's no point in quoting the entire previous message in your answer, is there?
Oct 10 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
I appreciate your help, but we haven't learned enum, templates, or the 'this' object yet. I'm sure that knowing these things would make my program much easier, but I'm pretty much confined to what is contained herein.
I can understand not knowing templates.

However, enums are prehistoric C and you should know those by now.

But you can't use classes and write member functions without knowing what the this pointer is. The this pointer always contains the address of the object that contains the member data that the member function uses.
Oct 10 '07 #5
beacon
579 Contributor
Sorry about the quote...it automatically came up like that when I hit reply and I wasn't paying attention. I'm going to read over your suggestions and try them out again. I'll post again if I stumble across something else confusing.\

Thanks...
Oct 25 '07 #6
beacon
579 Contributor
I can understand not knowing templates.

However, enums are prehistoric C and you should know those by now.

But you can't use classes and write member functions without knowing what the this pointer is. The this pointer always contains the address of the object that contains the member data that the member function uses.
I wish you'd tell that to my professor. I hadn't programmed in any language but HTML before taking this class. I took a first level computer science class last semester, but we didn't cover anything like this then either. We've only focused on C++...no C whatsoever besides the fact that you can use using namespace std to avoid having to type std::cout or what have you.

Those are just a couple of reasons why I'm so frustrated with computer science at the moment. I love working with computers, but I feel like my professors aren't covering things that should be covered after speaking with people that have been in the game for a while.

Thanks for getting on to me by the way considering that you didn't even offer a lick of helpful advice this time around. What the hell is this forum for if users, nay moderators, like you are going to degrade other people that are trying to and are eager to learn a language. Instead of teaching me, you belittle me. That's not very becoming of someone that claims they've taught C++ for 15 years. Just because you've had more training and time spent with this doesn't mean that you are better than me. Why don't you go find a meat bathing suit and take your happy ass swimming with some sharks?
Oct 25 '07 #7
Studlyami
464 Recognized Expert Contributor
I think you misinterpreted the intention of that post I didn't see it as a personal attack, but a person who is just surprised that you didn't know some of the more basic topics before you were taking on a couple more of the complex ones and from the sound of it, the class structure at your school didn't cover what was needed.

As for not offering help he helped you on your design and he gave you several reference points in which you could expand your knowledge and help the program at hand (looking up enums for example). This site is one of the few sites where people here will actually help you with your program and understand what it is doing rather than just giving out an answer.
Oct 26 '07 #8
beacon
579 Contributor
I think you misinterpreted the intention of that post I didn't see it as a personal attack, but a person who is just surprised that you didn't know some of the more basic topics before you were taking on a couple more of the complex ones and from the sound of it, the class structure at your school didn't cover what was needed.

As for not offering help he helped you on your design and he gave you several reference points in which you could expand your knowledge and help the program at hand (looking up enums for example). This site is one of the few sites where people here will actually help you with your program and understand what it is doing rather than just giving out an answer.
True, he did help me previously and I'm not knocking that, but my response was to his last post. Besides, if he was surprised that I hadn't learned those things it still baffles me why he would harp on it and not try to figure out why we hadn't covered those items in class.

Additionally, he assumed that I had experience in C, which is not the case and probably the thing that bothered me the most. I actually agreed with him that I should have learned those things by now, but for some reason my professor haven't felt like they were important pieces of the curriculum.

I absolutely love this forum and all of the help I've received. Plus, I enjoy being able to help other people with any problems they might have when I have the knowledge to do so.

Anyway, I apologize to weakness for the whole meat bathing suit crack. It was out of line, but I would like to request that everyone be careful about the response they give because they can very easily be misconstrued, as was the case in this instance if he, in fact, had good intentions. This is especially true with users, like myself, who are just starting out. I'd like to believe that no one on any of these forums would like to be the reason that an individual lost interest in programming altogether.
Oct 26 '07 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
To beacon:

No need to apologize.

One of my first instructors told me that C++ cannot be learned until you weep the necessary tears. He explained that the necessary tears occur late, late at night after you have been working on one problem for hours and finally, you slam you keyboard into your monitor, with tears in your eyes shouting: "Why won't this &^(^$* thing compile!!!". At that point, he said, you will reach the "teachable moment" and in that moment you will learn.

Stick with me, beacon, we have all been in your position and have great empathy for you.
Oct 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
15134
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
19
13576
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
7
2614
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
12
15100
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition should be kept for references to previous element of list. Here is my solution below: struct node {
57
4306
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
1495
by: Paminu | last post by:
I have a Linked-List and would like to create pointers to elements in this list. e.g I would like two pointers that point to each of their elements in the Linked-List. But there should always be exactly 5 nodes between these pointers. Does this make any sense or are there some more efficient way to access certain elements in a Linked-List?
12
3954
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
9
2846
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node * nPtr; //the next node's pointer }
2
1700
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of all, I am using C++, however it's managed C++ or visual C++, or whatever microsoft calls it. I'm using MSVS2005, and working on a Windows Forms Application project from the C++ projects tab. I'm pointing this out because apparently the syntax used in...
11
2554
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.