943,745 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9674
  • C++ RSS
May 1st, 2004
1

Double Linked Lists and Functions required

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. #define MAX_NAME_LEN 30
  6.  
  7.  
  8. typedef struct Sheet
  9. {
  10. char Name[MAX_NAME_LEN + 1];
  11. struct Sheet *Next, *Prev;
  12. }Sheet;
  13.  
  14.  
  15.  
  16. //prototypes
  17. void initialise(Sheet *&, char arr[], Sheet *&);
  18. void right(Sheet *);
  19. void left(Sheet *);
  20. void first(Sheet *&);
  21. void last(Sheet *&);
  22. void erase(Sheet *);
  23. void insert(char word[], Sheet *&, Sheet *&);
  24. void rename();
  25. void display(Sheet *&);
  26. void strToLower(char s[]);
  27.  
  28.  
  29.  
  30.  
  31.  
  32. void main()
  33. {
  34.  
  35. Sheet *Head = NULL, *Tail = NULL;
  36. Sheet *mvlr=NULL;
  37.  
  38. char init1[MAX_NAME_LEN+1]="sheet1";
  39. char init2[MAX_NAME_LEN+1]="sheet2";
  40. char init3[MAX_NAME_LEN+1]="sheet3";
  41.  
  42.  
  43. initialise(Head, init1, Tail);
  44. initialise(Head, init2, Tail);
  45. initialise(Head, init3, Tail);
  46.  
  47. char Option;
  48. char OldName[MAX_NAME_LEN+1], NewName[MAX_NAME_LEN+1];
  49.  
  50.  
  51. cout<<"************ Selection Options *************"<<endl;
  52. cout<<endl;
  53.  
  54. while(Option != 'E')
  55. {
  56. cout<<"Press 'R' to move to the next sheet\n";
  57. cout<<"Press 'L' to move to the previous sheet\n";
  58. cout<<"Press 'H' to jump to the first sheet\n";
  59. cout<<"Press 'T' to jump to the last sheet\n";
  60. cout<<"Press 'S' to add a new sheet to the list\n";
  61. cout<<"Press 'D' to delete a sheet from the list\n";
  62. cout<<"Press 'V' to view the sheets currently in the list\n";
  63. cout<<"Press 'N' to rename a sheet\n";
  64. cout<<"Press 'E' to exit\n";
  65. cout<<endl;
  66.  
  67. cout<<"Enter your option: ";
  68. cin>>Option;
  69. Option = toupper(Option);
  70.  
  71. switch(Option)
  72. {
  73. case 'R':
  74. right(Head);
  75. break;
  76.  
  77. case 'L':
  78. left(Head);
  79. break;
  80.  
  81. case 'H':
  82. first(Head);
  83. break;
  84.  
  85. case 'T':
  86. last(Tail);
  87. break;
  88.  
  89. case 'S':
  90. cout<<"Enter a name for this new sheet: ";
  91. cin>>NewName;
  92. strToLower(NewName);
  93. insert(NewName, Head, Tail);
  94. cout<<endl;
  95. break;
  96.  
  97. case 'D':
  98. cout<<"Enter the name of the sheet you wish to delete: ";
  99. cin>>OldName;
  100. strToLower(OldName);
  101. erase(Head);
  102. break;
  103.  
  104. case 'V':
  105. cout<<"Displaying list: "<<endl;
  106. display(Head);
  107. cout<<endl;
  108. break;
  109.  
  110. case 'N':
  111. cout<<"Enter name to change: ";
  112. cin>>OldName[MAX_NAME_LEN + 1];
  113. strToLower(OldName);
  114. cout<<"Enter new name for sheet: ";
  115. cin>>NewName[MAX_NAME_LEN + 1];
  116. strToLower(NewName);
  117. rename();
  118. break;
  119.  
  120. case 'E':
  121. break;
  122.  
  123. default:
  124. cout<<endl;
  125. cout<<"Please select an option from the list"<<endl;
  126. cout<<endl;
  127. break;
  128.  
  129. }//end switch
  130. }//end while
  131.  
  132.  
  133. }//end main
  134.  
  135. void initialise(Sheet *&L, char arr[], Sheet *&T)
  136. {
  137. Sheet *e, *curr, *prev;
  138.  
  139. e = new Sheet;
  140. strcpy(e->Name, arr);
  141. e->Next = NULL;
  142. e->Prev = NULL;
  143.  
  144. if(L == NULL)
  145. {
  146. L = e;
  147. }
  148. else
  149. {
  150. prev = curr = L;
  151.  
  152. while(curr != NULL)
  153. {
  154. prev = curr;
  155. curr = curr->Next;
  156. }
  157. e->Next = NULL;
  158. prev->Next = e;
  159. e->Prev = prev;
  160. T = e;
  161. }
  162.  
  163. }
  164.  
  165.  
  166. void right(Sheet *Head)
  167. {
  168. Sheet *Current, *Previous;
  169.  
  170. Current = Head;
  171. Current = Current->Next;
  172. Previous = Current->Prev;
  173.  
  174. cout<<"Current Sheet: "<<Current->Name;
  175. cout<<endl;
  176. }
  177.  
  178. void left(Sheet *mvlr)
  179. {
  180. Sheet *Current = new Sheet, *Previous = new Sheet;
  181.  
  182. cout<<"Previous Sheet: "<<Current->Name<<endl;
  183. Current = Current->Prev;
  184. Previous = Current->Next;
  185. cout<<"Current Sheet: "<<Current->Name<<endl;
  186.  
  187.  
  188. if(Current->Prev == NULL)
  189. {
  190. cout<<"No more sheets!!!11!one"<<endl;
  191. }
  192. cout<<endl;
  193. }
  194.  
  195.  
  196. void first(Sheet *&L)
  197. {
  198. Sheet *e;
  199. e = L;
  200. cout<<"The first sheet in the list is:"<<'\t';
  201. cout<<e->Name<<endl;
  202. cout<<endl;
  203. }
  204.  
  205. void last(Sheet *&L)
  206. {
  207. Sheet *e;
  208. e = L;
  209. cout<<"The last sheet in the list is: "<<'\t';
  210. cout<<e->Name<<endl;
  211. cout<<endl;
  212. }
  213.  
  214.  
  215. void erase(Sheet *L)
  216. {
  217. Sheet *Current = L;
  218.  
  219. while(Current != NULL || (!strcmp("OldName", Current->Name)))
  220. {
  221. Current = Current->Next;
  222. delete Current;
  223. L = Current;
  224. }
  225. }
  226.  
  227.  
  228. //function to insert a new sheet
  229. //inserts at end of list only.
  230. void insert(char word[], Sheet *&L, Sheet *&T)
  231. {
  232. Sheet *e, *curr, *prev;
  233.  
  234. e = new Sheet;
  235. strcpy(e->Name, word);
  236. e->Next = NULL;
  237. e->Prev = NULL;
  238.  
  239. if(L == NULL)
  240. {
  241. L = e;
  242. }
  243. else
  244. {
  245. prev = curr = L;
  246.  
  247. while(curr != NULL)
  248. {
  249. prev=curr;
  250. curr=curr->Next;
  251. }
  252. e->Next = NULL;
  253. prev->Next = e;
  254. e->Prev = prev;
  255. T = e;
  256. }
  257. }
  258.  
  259.  
  260. //function to display the current sheets in list
  261. void display(Sheet *&L)
  262. {
  263. Sheet *****;
  264. **** = L;
  265.  
  266. while(**** != NULL)
  267. {
  268. cout<<****->Name<<endl;
  269. **** = ****->Next;
  270. }
  271. }
  272.  
  273.  
  274. //function to rename a sheet
  275. void rename()
  276. {
  277. Sheet *Current;
  278. char OldName[MAX_NAME_LEN + 1], NewName[MAX_NAME_LEN + 1];
  279.  
  280. if(!strcmp(Current->Name, OldName))
  281. {
  282. strcpy(NewName, Current->Name);
  283. cout<<Current->Name;
  284. }
  285. }
  286.  
  287.  
  288. void strToLower(char s[])
  289. { int i ;
  290. for(i=0 ; s[i] != '\0' ; i++)
  291. {
  292. s[i] = tolower(s[i]) ;
  293. }
  294. }

right, the code above partially works and I'm hoping somebody out there can help me understand why some of the functions and code wont work.

It's a double linked list.
user has to be able to
  1. move right throught the list and back left.
  2. jump to the head and jump to the tail.
  3. insert a new sheet anywhere and be in alphabetical order
  4. delete any sheet from anywhere
  5. rename a sheet
  6. display the current sheets in the list
  7. name are case insensitive
There are a few of us working on this together and so far, the display, jump to head/tails and insert [but only at tail] works.

MS Visual C++ 6.0 just crashes when I press 'R' or 'L' to move left or right.
Delete doesn't seem to like searching for the sheet we want to delete.
Rename I think is the comparing strings.

Any help or ideas is greatly appreciated.

thanks
/InvalidDLL
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
InvalidDLL is offline Offline
2 posts
since May 2004
May 3rd, 2004
1

Re: Double Linked Lists and Functions required

Hello,

I am not an expert programmer, but it looks to me that you should define the struct before you do the typdef statement. It might be possible to combine them as shown-- I am not sure.

You should also have a head pointer, and a tail pointer, and be using nulls to end your code. I'd be curious to see the whole code piece, and see what your headptr and tailptrs are doing. I think your heads are getting misused.

It might be handy while you are coding to actually draw out the list on paper. Draw out the head pointer. Define it in your code. Draw your structure. Define it in your code. Code in the head pointer assignment to the structure -- on your paper, make the arrow. Make the first data structure prev socket == null. Add that to your paper. Flow out the whole process on your paper, to help you out. Don't draw the arrows, or initialize any of the boxes until you manufacture the code.

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 4th, 2004
0

Re: Double Linked Lists and Functions required

after doing some more work, it is apparent that the head pointer is gettin ****ed over somewhere

I'm back with the group after the weekend so we'll put own collective heads together and try and fix this.

I'll post back anyway with anything we get working just for reference

/InvalidDLL
Reputation Points: 11
Solved Threads: 0
Newbie Poster
InvalidDLL is offline Offline
2 posts
since May 2004
Jul 14th, 2004
0

Re: Double Linked Lists and Functions required

/* * * A R R * * */
//using c++
class CSheet{
public:
CString Name;
CSheet * Nxt;
CSheet * Prv;
//default constructor
CSheet():Nxt(NULL),Prv(NULL){}
// destructor binds objects Nxt and Prv
~CShett(){
if(Nxt)Nxt->Prv=Prv;
if(Prv)Prv->Nxt=Nxt;
}
};

// if Nxt is NULL then this is last
// if Prv is NULL then this is first


class CSheetPtr{
CSheet *ptr;
public:

void operator= (CSheet * p){ptr=p;}
CString GetName(){return Name;}
void SetName(CString nm){Name=nm}
CSheet* GetSheetAddr(){return ptr;}

CSheet* MoveNext(){return ptr=ptr->Nxt;}
CSheet* MovePrev(){return ptr=ptr->Prv;}
CSheet* MoveLast()
{
while(ptr->Nxt)
ptr=ptr->Nxt;
return ptr;
}

CSheet* MoveFirst()
{
while(ptr->Prv)
ptr=ptr->Prv;
return ptr;
}

};


/*Just rote this so may not be correct but using c++ classes is better*/
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Talib is offline Offline
3 posts
since Jul 2004
Jul 19th, 2004
0

Re: Double Linked Lists and Functions required

A R R
you may name CSheetPtr::MoveNext CSheetPtr::operator++ so it will work as real pointers
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Talib is offline Offline
3 posts
since Jul 2004
Jul 20th, 2004
0

Re: Double Linked Lists and Functions required

The following code is an example of how to implement a Doubly Linked List. Let me know what you think of the code and if it has helped you in any way.
C++ Syntax (Toggle Plain Text)
  1. // "DoublyLinkedList.cpp"
  2. #include<iostream>
  3. #include<conio.h>
  4. using namespace std;
  5. class node
  6. {
  7. public:
  8. int data;
  9. node *right_link, *left_link;
  10. };
  11. class friend_node
  12. {
  13. private:
  14.  
  15. node *head, *current_node;
  16. void add_to_list(int);
  17. int move_right();
  18. int move_left();
  19. void display_list();
  20. void delete_record();
  21. void dispose_list();
  22. void print_options();
  23. public:
  24.  
  25. friend_node();
  26. ~friend_node();
  27. void handle_choice();
  28. };
  29. friend_node::friend_node()
  30. {
  31. head = new node;
  32. head->data = 0;
  33. head->left_link = head;
  34. head->right_link = head;
  35. current_node = head;
  36. }
  37. friend_node::~friend_node()
  38. {
  39. delete head;
  40. }
  41. void main()
  42. {
  43. friend_node go;
  44. cout << "Welcome to the Doubly Linked List Simulation\n\n";
  45. cout << "1 - Exit program\n";
  46. cout << "2 - Add\n";
  47. cout << "3 - Move left\n";
  48. cout << "4 - Move right\n";
  49. cout << "5 - Print\n";
  50. cout << "6 - Delete\n";
  51. cout << "7 - Dispose\n";
  52. cout << "8 - Show these options again\n";
  53. go.handle_choice();
  54. }
  55. void friend_node::handle_choice()
  56. {
  57. friend_node free_memory;
  58. int value;
  59. int choice;
  60. while (choice != 1)
  61. {
  62. cout << "\nEnter choice: ";
  63. cin >> choice;
  64. int temp = 0;
  65. switch(choice)
  66. {
  67. case 1:
  68. free_memory.~friend_node();
  69. cout << "\nThank you!\n\n";
  70. getch();
  71. exit(1);
  72. break;
  73.  
  74. case 2:
  75. cout << "\nEnter a number: ";
  76. cin >> value;
  77. temp = value;
  78. cout << "\n" << temp << " has been added!\n";
  79. getch();
  80. add_to_list(value);
  81. break;
  82.  
  83. case 3:
  84. cout << "\nCurrent data to the left is: " << move_left() << "\n\n";
  85. getch();
  86. break;
  87.  
  88. case 4:
  89. cout << "\nCurrent data to the right is: " << move_right() << "\n\n";
  90. getch();
  91. break;
  92. case 5:
  93. display_list();
  94. break;
  95.  
  96. case 6:
  97. delete_record();
  98. break;
  99. case 7:
  100. dispose_list();
  101. break;
  102.  
  103. case 8:
  104. print_options();
  105. break;
  106. default:
  107. cout << "\nInvalid choice!\n\n";
  108. getch();
  109. break;
  110. }
  111. }
  112. }
  113. void friend_node::add_to_list(int add_data)
  114. {
  115. node *add = new node;
  116. add->data = add_data;
  117. add->left_link = current_node;
  118. add->right_link = current_node->right_link;
  119. current_node->right_link->left_link = add;
  120. current_node->right_link = add;
  121. current_node = add;
  122. }
  123. int friend_node::move_left()
  124. {
  125. char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
  126. if(head->right_link == head)
  127. {
  128. cout << empty << endl;
  129. getch();
  130. handle_choice();
  131. }
  132. else
  133. {
  134. current_node = current_node->left_link;
  135.  
  136. if(current_node == head)
  137. {
  138. current_node = current_node->left_link;
  139. return (current_node->data);
  140. }
  141. else
  142. return(current_node->data);
  143. }
  144. return (0);
  145. }
  146. int friend_node::move_right()
  147. {
  148. char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
  149.  
  150. if(head->right_link == head)
  151. {
  152. cout << empty << endl;
  153. getch();
  154. handle_choice();
  155. }
  156.  
  157. else
  158. {
  159. current_node = current_node->right_link;
  160. if(current_node == head)
  161. {
  162. current_node = current_node->right_link;
  163. return (current_node->data);
  164. }
  165. else
  166. return(current_node->data);
  167. }
  168. return (0);
  169. }
  170. void friend_node::display_list(void)
  171. {
  172. char empty[30] = {"\n\n\a\a\a\aNothing to print!\n\n"};
  173. node *print;
  174. if(head->right_link == head)
  175. {
  176. cout << empty << endl;
  177. getch();
  178. handle_choice();
  179. }
  180. else
  181. {
  182. cout << "\nCurrent data in the list: ";
  183. print = head->right_link;
  184. while(print != head)
  185. {
  186. cout << print->data <<" ";
  187. print = print->right_link;
  188. }
  189. }
  190. getch();
  191. cout <<"\n\n";
  192. }
  193. void friend_node::delete_record()
  194. {
  195. char empty[30] = {"\n\n\a\a\a\aNothing to delete!\n\n"};
  196. node *temp_current = new node;
  197. node *temp_current2 = temp_current;
  198. temp_current = current_node;
  199. if (head->right_link == head)
  200. {
  201. cout << empty << endl;
  202. getch();
  203. handle_choice();
  204. }
  205. while(current_node != head)
  206. {
  207. cout << "\n\n" << current_node->data << " has been deleted!\n\n";
  208. current_node->left_link->right_link = current_node->right_link;
  209. current_node->right_link->left_link = current_node->left_link;
  210. current_node = current_node->right_link;
  211. current_node = current_node->left_link;
  212. getch();
  213. handle_choice();
  214. }
  215. }
  216. void friend_node::dispose_list()
  217. {
  218. char empty[30] = {"\n\n\a\a\a\aNothing to dispose!\n\n"};
  219. if(head->right_link == head)
  220. {
  221. cout << empty << endl;
  222. getch();
  223. handle_choice();
  224. }
  225. while(current_node != head)
  226. {
  227. current_node->left_link->right_link = current_node->right_link;
  228. current_node->right_link->left_link = current_node->left_link;
  229. current_node = current_node->right_link;
  230. current_node = current_node->left_link;
  231. if(current_node == head)
  232. {
  233. cout << "\n\nThe list has been disposed!\n\n";
  234. getch();
  235. }
  236. }
  237. }
  238. void friend_node::print_options()
  239. {
  240. cout << "\n\n1 - Exit program\n";
  241. cout << "2 - Add\n";
  242. cout << "3 - Move left\n";
  243. cout << "4 - Move right\n";
  244. cout << "5 - Print\n";
  245. cout << "6 - Delete\n";
  246. cout << "7 - Dispose\n";
  247. cout << "8 - Show these options again\n\n";
  248. }
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: summations and combinations
Next Thread in C++ Forum Timeline: C++ Builder 6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC