473,406 Members | 2,343 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,406 software developers and data experts.

Linked List (Node Class)

33
I want to write several functions so that the program below would work.
I am trying to find the functions for the main routine provided below and reveal a secret message. I dont wanne make any changes to the main routine. I just want to write all the functions, but I cant. is there any one here who could help me with that?

Thanks


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #include "list.cpp"
  5.  
  6.  
  7. int main() {
  8.     LinkedList<string> rebelList1(4," ");
  9.     Iterator<string> iter1 = rebelList1.end();
  10.     iter1.backward();
  11.     rebelList1.erase(iter1);
  12.     iter1.backward();
  13.     rebelList1.insert(iter1,"Co");
  14.     iter1.forward();
  15.     rebelList1.insert(iter1,"it");
  16.     rebelList1.insert(iter1, rebelList1[2]);
  17.     rebelList1[0] = "ldth\nha";
  18.     rebelList1.pop_back();
  19.     rebelList1.push_back("all");
  20.     rebelList1.push_front("oth\nb");
  21.  
  22.     LinkedList<string> rebelList2;
  23.     rebelList2.push_back("Tatooine");
  24.     rebelList2.push_front("Lando");
  25.     rebelList2.push_back(" sh");
  26.     rebelList2.pop_front();
  27.     rebelList2.pop_front();
  28.     rebelList2.push_front("hey");
  29.     rebelList2.reverse();
  30.     Iterator<string> iter2 = rebelList2.find(" sh");
  31.     rebelList2.insert(iter2,"ou");
  32.     rebelList2.reverse();
  33.     iter2 = rebelList2.find("hey");
  34.     rebelList2.insert(iter2,"oth\nb");
  35.     iter2.backward();
  36.     rebelList2.insert(iter2,"n H");
  37.     rebelList2.reverse();
  38.     rebelList2.insert(iter2,"ut t");
  39.     rebelList2.push_front("ld c");
  40.  
  41.     LinkedList<string> rebelList3;
  42.     rebelList3.push_front("bel b");
  43.     rebelList3.push_back("e");
  44.     Iterator<string> iter3 = rebelList3.find("e");
  45.     rebelList3.insert(iter3, "re");
  46.     rebelList3.reverse();
  47.     rebelList3.insert(iter3, "th");
  48.     rebelList3.push_back("a");
  49.     iter3.forward();
  50.     rebelList3.insert(iter3, " ");
  51.  
  52.     LinkedList<string> rebelList4 = rebelList3;
  53.     rebelList4[0] = "y, Da";
  54.     rebelList4[3] = "nes";
  55.     rebelList4.reverse();
  56.     rebelList4[1] = "lenti";
  57.     rebelList4.push_back("rt");
  58.     rebelList4.push_front("y v");
  59.     rebelList4.reverse();
  60.     rebelList4[2] = "da";
  61.  
  62.     LinkedList<string> rebelList = rebelList1 + rebelList2;
  63.     rebelList = rebelList4 + rebelList;
  64.     rebelList.push_front("h!\n");
  65.     LinkedList<string> oneWord(1,"s o");
  66.     rebelList = rebelList + oneWord;
  67.     rebelList[9] = "pp";
  68.     rebelList.reverse();
  69.     rebelList.push_front("se i");
  70.     rebelList = rebelList3 + rebelList;
  71.  
  72.     rebelList.print();
  73.  
  74.     return 0;
  75. }
Feb 12 '08 #1
6 3943
Laharl
849 Expert 512MB
You'll need to look up template classes and template programming. Here is a tutorial on them. Other than that, if you don't really know how linked lists work, go to Wikipedia or Google and get the information there and implement first without templates, then add them.
Feb 13 '08 #2
APEJMAN
33
THIS IS MY list.h file that I made, which is not complete
abd below that I have my list.cpp, which is the functions, but its not compete, is there any one here who can complete it?

thanks
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. #ifndef LIST_H
  4. #define LIST_H
  5.  
  6. using namespace std;
  7.  
  8.  
  9. //Node Class
  10. template <typename T>
  11. class Node {
  12. public:
  13.     Node(T newData); //Constructor
  14.     template <typename T>
  15.     friend class LinkedList;
  16.     template <typename T>
  17.     friend class Iterator;
  18. private:
  19.     T data;
  20.     Node* prev; //Pointer to previous node in list.
  21.     Node* next; //Pointer to next node in list.
  22. };
  23.  
  24.  
  25.  
  26.  
  27.  
  28. // Iterator Class
  29.  
  30. template <typename T>
  31. class Iterator {
  32. public:
  33.     Iterator();
  34.     T get() const;
  35.     void forward();
  36.     void backward();
  37.     template <typename T>
  38.     friend class LinkedList;
  39. private:
  40.     Node<T>* position;
  41. };
  42.  
  43.  
  44.  
  45. // LinkedList Class
  46.  
  47. template <typename T>
  48. class LinkedList {
  49. public:
  50.     LinkedList();
  51.     LinkedList(int numNodes, T defaultValue);
  52.     LinkedList(const LinkedList<T>& right);
  53.     ~LinkedList();
  54.     LinkedList<T>& operator= (const LinkedList<T>& right);
  55.     Iterator<T> begin() const;
  56.     Iterator<T> end() const;
  57.     void insert(Iterator<T> iter, T value);
  58.     void erase(Iterator<T>& iter);
  59.     T& operator[] (int index);
  60.     T operator[] (int index) const;
  61.     int size() const;
  62. private:
  63.     Node<T>* first;  //Pointer to first node in list.
  64.     Node<T>* last;   //Pointer to last node in list.
  65. };
  66.  
  67.  
  68. /*********************************************************/
  69.  
  70. list.cpp
  71.  
  72. #include <string>
  73. #include "list.h"
  74. #include <iostream>
  75.  
  76. //The Node Constructor
  77. template <typename T> 
  78. Node<T>::Node(T newData) {
  79. data = newData;
  80. prev = NULL;
  81. next = NULL;
  82. }
  83.  
  84.  
  85. /**********************************/
  86. //The Iterator Fuctions
  87. template <typename T>
  88. Iterator<T>::Iterator() {
  89.     position = NULL;
  90. }
  91.  
  92. template <typename T>
  93. T Iterator<T>::get() const {
  94.     return position->data;
  95. }
  96.  
  97. template <typename T>
  98. void Iterator<T>::forward() {
  99.     position = position->next;
  100. }
  101.  
  102. template <typename T>
  103. void Iterator<T>::backward() {
  104.     position = position->prev;
  105. }
  106. /*******************************/
  107.  
  108.  
  109.  
  110. /*******************************/
  111. //LinkedList Functions
  112. template <typename T>
  113. LinkedList<T>::LinkedList() {
  114.     first = NULL;
  115.     last = NULL;
  116. }
  117.  
  118. template <typename T>
  119. LinkedList<T>::LinkedList(int numNodes, T defaultValue) {
  120.     first=NULL;   last=NULL;  
  121.     for (int i=1; i<=numNodes; i++)
  122.         insert(begin(),defaultValue);
  123. }
  124.  
  125. template <typename T>
  126. LinkedList<T>::LinkedList(const LinkedList<T>& right) {
  127.     first = NULL;  
  128.     last = NULL;
  129.     Iterator<T> iter_right = right.end();
  130.     while (iter_right.position != NULL) {
  131.         insert(begin(), iter_right.get());
  132.         iter_right.backward();
  133.     }
  134. }
  135.  
  136. template <typename T>
  137. LinkedList<T>::~LinkedList() {
  138.     while (first != NULL) 
  139.         erase(begin());
  140. }
  141.  
  142. template <typename T>
  143. LinkedList<T>& LinkedList<T>::operator= (const LinkedList<T>& right) {
  144.     if (this != &right) {
  145.         Iterator<T> iter_left = begin();
  146.         while (iter_left.position != NULL) {
  147.             erase(iter_left);
  148.             iter_left = begin();
  149.         }
  150.         Iterator<T> iter_right = right.end();
  151.         while (iter_right.position != NULL) {
  152.             insert(begin(), iter_right.get());
  153.             iter_right.backward();
  154.         }
  155.     }
  156.     return *this;
  157. }
  158.  
  159. template <typename T>
  160. Iterator<T> LinkedList<T>::begin() const {
  161.     Iterator<T> iter;
  162.     iter.position = first;
  163.     return iter;
  164. }
  165.  
  166. template <typename T>
  167. Iterator<T> LinkedList<T>::end() const {
  168.     Iterator<T> iter;
  169.     iter.position = last;
  170.     return iter;
  171. }
  172.  
  173. template <typename T>
  174. int LinkedList<T>::size() const {
  175.     Iterator<T> iter = begin();
  176.     int length = 0;
  177.     while (iter.position != NULL) {
  178.         iter.forward();
  179.         length++;
  180.     }
  181.     return length;
  182. }
  183.  
  184. template <typename T>
  185. void LinkedList<T>::erase(Iterator<T>& iter) {
  186.     Node<T>* pos = iter.position;
  187.     iter.forward();
  188.     //Set pointer from node before.
  189.     if (pos == first)  //Check if first node.
  190.         first = pos->next;
  191.     else
  192.         (pos->prev)->next = pos->next;
  193.     //Set pointer from node after.
  194.     if (pos == last)   //Check if last node.
  195.         last = pos->prev;
  196.     else 
  197.         (pos->next)->prev = pos->prev;
  198.     delete pos;  //Remove the node from memory.
  199.     return;
  200. }
  201.  
  202. template <typename T>
  203. void LinkedList<T>::insert(Iterator<T> iter, T value) {
  204.     Node<T>* pos = iter.position;
  205.     Node<T>* newNode = new Node<T>(value);
  206.     //Check if list is empty.
  207.     if (first == NULL) {
  208.         first = newNode;
  209.         last = newNode;
  210.         return;
  211.     }
  212.     newNode->prev = pos->prev;
  213.     newNode->next = pos;
  214.     if (newNode->prev == NULL)  //Check if inserting at first position.
  215.         first = newNode;
  216.     else
  217.         (newNode->prev)->next = newNode;
  218.     pos->prev = newNode;
  219.     return;
  220. }
  221.  
  222. template <typename T>
  223. T& LinkedList<T>::operator[] (int index) {
  224.     Node<T>* pos = first;
  225.     for (int i=1; i<= index; i++)
  226.         pos = pos->next;
  227.     return pos->data;
  228. }
  229.  
  230. template <typename T>
  231. T LinkedList<T>::operator[] (int index) const {
  232.     Node<T>* pos = first;
  233.     for (int i=1; i<= index; i++)
  234.         pos = pos->next;
  235.     return pos->data;
  236. }
  237.  
  238. /**********************************/
Feb 13 '08 #3
Laharl
849 Expert 512MB
First off, we're not going to do your work for you. Also, please use the provided CODE tags so that your code is formatted for easy reading.

Where precisely are you having your problem(s)? Let us know what error messages you get/what the output is that it shouldn't be.
Feb 13 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Why are you using C++ to write a linked list when the standard library list template is already written???????

Is this an academic exercise??
Feb 13 '08 #5
APEJMAN
33
just I like to learn C++ as a hobby
by the way I wrote the program

thanks
Feb 13 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
The danger in doing something like this is your time investment. If this becomes your creation you will use it on a real job and interject more non-standard C++ software into the industry.

If you know how a linked list works, then there is no point in writing one for yourself. Most of the STL is infrastructure of a very common nature.
Feb 14 '08 #7

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

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
0
by: Maurice | last post by:
how could you pass the head from a linked list class so that it could be used in a main file? here is the content of the linkedlist.h file: #ifndef LINKEDLIST_H #define LINKEDLIST_H ...
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...
2
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.