943,546 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 993
  • C++ RSS
Jun 19th, 2008
0

help with Sorting a Linked list is needed!

Expand Post »
hello,
ok i have this simple linked list program, as u can see below i got three functions: one to insert a node in the first of the list, the second to insert a node in the middle and the third to insert a node in the last of the list.
And now i want to merge all of them in only one function that will sort the list and decide where to put the new node according to its value.
Can you help me please?

#include<iostream.h>

class node{
public:
int data;
node* next;
node(int x){
data=x;
next=NULL;};

void display(){
cout<<data<<"  ";
};
};

class linked_list{
private:
node* first;
public:
linked_list(){
first=NULL;};

void insert_last(int d){
node* newnode=new node(d);
node* current=first ;
if(first==NULL)
first=newnode;
else{
while(current->next!=NULL)
current=current->next;
current->next=newnode;
}};

void insert_first(int z){
node* newnode= new node(z);
if(first==NULL)
first=newnode;
else{
newnode->next=first;
first=newnode;}
};

void display(){
node* current=first;
while(current!=NULL){
current->display();
current=current->next;}
};

void insert_mid(int d){
node* newnode=new node(d);
node* current=first;
node* temp;
while(current->next->data<d)
current=current->next;

temp=current->next;
current->next=newnode;
newnode->next=temp;
};

int l_delete(int x){
node* current=first;
if(first==NULL){
cout<<"List is empty"<<endl;
return -1;}
if(first->data==x)
first=first->next;
else{
while((current->next->data!=x)&&(current->next!=NULL))
current=current->next;}
if(current->next==NULL)
cout<<"error"<<endl;
else{
current->next=current->next->next;
delete current;}
return x;};
};



main(){
linked_list list;
int no, g, h, d, e;
char c, a0, a1, a2, a3;
do{
cout<<"Please choose the number of the option"<<endl<<endl;
cout<<"\t\t______________________ MENU _____________________"<<endl;
cout<<"\t\t1. Add new node to the end of the list"<<endl;
cout<<"\t\t2. Add new node to the beginning of the list"<<endl;
cout<<"\t\t3. Add new node in the middle of the list"<<endl;
cout<<"\t\t4. Delete a node from the list"<<endl;
cout<<"\t\t5. Display the list"<<endl;
cout<<"\t\t_________________________________________________"<<endl;
cin>>no;
switch(no){
case 1: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>g;
list.insert_last(g);
cout<<"Do you want to add more to the end of the list? y/n"<<endl;
cin>>a0;}
while(a0=='y');
break;
case 2: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>h;
list.insert_first(h);
cout<<"Do you want to add more to the beginning of the list? y/n"<<endl;
cin>>a1;}
while(a1=='y');
break;
case 3: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>d;
list.insert_mid(d);
cout<<"Do you want to add more to the mid of the list? y/n"<<endl;
cin>>a2;}
while(a2=='y');
break;
case 4: do{
cout<<"Please enter the value of the element you want to delete:"<<endl;
cin>>e;
list.l_delete(e);
cout<<"Do you want to delete more from the list? y/n"<<endl;
cin>>a3;}
while(a3=='y');
break;
case 5: list.display();
cout<<endl;
break;
default:
cout<<"Error in number"<<endl;
break;}
cout<<"Do you want to continue? y/n"<<endl;
cin>>c;}
while(c=='y');
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anifreak is offline Offline
5 posts
since Apr 2008
Jun 19th, 2008
0

Re: help with Sorting a Linked list is needed!

And if your code was indented AT ALL, some people might actually want to read it.

I mean, points++ for using code tags, but then you blew it by not indenting it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 19th, 2008
0

Re: help with Sorting a Linked list is needed!

Well, I suppose this would solve your problem. You can merge the sort() into the insert().
Here's the code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. class List
  6. {
  7. private:
  8. struct ListN
  9. {
  10. char item;
  11. ListN* next;
  12. };
  13. ListN* head;
  14. ListN* tail;
  15. int size;
  16.  
  17. public:
  18. List();
  19. ~List();
  20. void insert (char );
  21. void remove (int );
  22. void deleteAll ();
  23. void retrieve ();
  24. void countIte();
  25. void countRec();
  26. void display();
  27. void sort();
  28. void save();
  29. };
  30.  
  31.  
  32. List :: List()
  33. {
  34. head = tail = NULL;
  35. size = 0;
  36. }
  37.  
  38. List :: ~List()
  39. {
  40. deleteAll ();
  41. }
  42.  
  43. void List :: sort()
  44. {
  45. ListN* curPtr;
  46. curPtr = head;
  47. char buffer;
  48. int i;
  49. int j;
  50.  
  51. for (i=1; i<size; i++)
  52. {
  53. for(j=size-1, curPtr = head; j>=i; j--, curPtr = curPtr -> next)
  54. {
  55. if (curPtr->item > curPtr->next->item)
  56. {
  57. buffer = curPtr -> item;
  58. curPtr->item = curPtr->next->item;
  59. curPtr->next->item = buffer;
  60. }
  61. }
  62. }
  63.  
  64. //alternate method for sorting
  65. /*if (size >= 2)
  66. {
  67. for (curPtr = head; curPtr->next != NULL; curPtr = curPtr -> next)
  68. {
  69. cout << "sorting " << curPtr-> item << endl;
  70. if (curPtr->item > curPtr->next->item)
  71. {
  72. buffer = curPtr -> item;
  73. curPtr->item = curPtr->next->item;
  74. curPtr->next->item = buffer;
  75. }
  76. }
  77. }*/
  78. }
  79.  
  80. void List :: insert(char item)
  81. {
  82. ListN* newPtr;
  83. ListN* curPtr;
  84. newPtr = new ListN;
  85. curPtr = head;
  86.  
  87. newPtr -> next = NULL;
  88. newPtr -> item = item;
  89.  
  90. if (size == 0)
  91. {
  92. head = newPtr;
  93. newPtr -> next = NULL;
  94. }
  95.  
  96. else
  97. {
  98. curPtr = tail;
  99. curPtr->next = newPtr;
  100. }
  101.  
  102. tail = newPtr;
  103. size++;
  104. }
  105.  
  106. void List :: retrieve()
  107. {
  108. ifstream read;
  109. char temp1;
  110. char check;
  111.  
  112. read.open("Q2.txt");
  113. read >> temp1;
  114.  
  115. insert(temp1);
  116. check = temp1;
  117.  
  118. for (;;)
  119. {
  120. read >> temp1;
  121.  
  122. if (temp1 == check)
  123. break;
  124. check = temp1;
  125. insert(temp1);
  126. }
  127. sort();
  128. }
  129.  
  130. void List :: display()
  131. {
  132. int i;
  133. ListN* curPtr;
  134. curPtr = head;
  135.  
  136. sort();
  137. if (size == 0)
  138. cout << "No item in the List!!" << endl;
  139.  
  140. else
  141. {
  142. cout << "\tThe list of record is :\t";
  143. for (i=1; i<=size; i++)
  144. {
  145. cout << curPtr -> item << " ";
  146. curPtr = curPtr -> next;
  147. }
  148. }
  149.  
  150.  
  151. }
  152.  
  153. void List :: save()
  154. {
  155. int i;
  156. ListN* curPtr;
  157. ofstream save;
  158.  
  159. save.open("Q2.txt");
  160.  
  161. curPtr = head;
  162. for( i=0; i<size; i++ )
  163. {
  164. save << curPtr -> item << endl;
  165. curPtr = curPtr -> next;
  166. }
  167. }
  168.  
  169. void List :: remove(int index)
  170. {
  171. int i;
  172. ListN* previous;
  173. ListN* curPtr;
  174. curPtr = head;
  175.  
  176. if (index <= size && index >0)
  177. {
  178. if(index == 1)
  179. {
  180. head = head -> next;
  181. delete curPtr;
  182. curPtr->next = NULL;
  183. }
  184.  
  185. for(i=1; i<index-1; i++)
  186. curPtr = curPtr -> next;
  187.  
  188. previous = curPtr;
  189.  
  190. curPtr = curPtr -> next;
  191.  
  192. previous -> next = curPtr -> next;
  193.  
  194. curPtr = NULL;
  195. size--;
  196. }
  197.  
  198. else if (index<0 || index>size)
  199. cout << "The record number is out of range!!" << endl;
  200.  
  201. previous = NULL;
  202.  
  203. }
  204.  
  205. void List :: deleteAll ()
  206. {
  207. cout << size << endl;
  208. int i;
  209.  
  210. for (i=size; i>0 ; i--)
  211. {
  212. if (i == 1)
  213. {
  214. head = NULL;
  215. size--;
  216. }
  217. else
  218. remove(i);
  219. }
  220. }
  221.  
  222. void List :: countIte()
  223. {
  224. int count;
  225. int i;
  226. ListN* curPtr;
  227.  
  228. count = 0;
  229.  
  230. for (i=0, curPtr = head; i<size; i++, curPtr = curPtr->next)
  231. count++;
  232.  
  233. cout << "The total records is :\t\t" << count << endl;
  234. }
  235.  
  236. void List :: countRec()
  237. {
  238. ListN* curPtr;
  239. int count;
  240.  
  241. count = 0;
  242.  
  243. for(curPtr = head; curPtr != NULL; curPtr = curPtr->next, count++);
  244. {
  245. }
  246.  
  247. cout << "The total records is :\t\t" << count << endl;
  248. }
  249.  
  250. int main()
  251. {
  252. List a;
  253. int choice;
  254. int index;
  255. char val;
  256.  
  257. do
  258. {
  259. cout << "1 Insert a new item into the linked list." << endl;
  260. cout << "2 Remove an item from the linked list." << endl;
  261. cout << "3 Remove all items from the linked list." << endl;
  262. cout << "4 Get the item from a text file and added to the sorted linked list." << endl;
  263. cout << "5 Count the number of item iteratively." << endl;
  264. cout << "6 Count the number of item recursively." << endl;
  265. cout << "7 List the content of the linked list." << endl;
  266. cout << "8\tQuit." << endl << endl;
  267. cout << "\tYour Choice :\t";
  268. cin >> choice;
  269.  
  270. cout << "\n\n\n" << endl;
  271.  
  272. switch(choice)
  273. {
  274. case 1:
  275. cout << "Enter a value for insert :\t";
  276. cin >> val;
  277. a.insert(val);
  278. cout << "\n\n" << endl;
  279. break;
  280. case 2:
  281. cout << "Enter the index of record for deletion :\t";
  282. cin >> index;
  283. a.remove(index);
  284. cout << "\n\n" << endl;
  285. break;
  286. case 3:
  287. a.deleteAll();
  288. cout << "\n\n" << endl;
  289. break;
  290. case 4:
  291. a.retrieve();
  292. cout << "\n\n" << endl;
  293. break;
  294. case 5:
  295. a.countIte();
  296. cout << "\n\n" << endl;
  297. break;
  298. case 6:
  299. a.countRec();
  300. cout << "\n\n" << endl;
  301. break;
  302. case 7:
  303. a.display();
  304. cout << "\n\n" << endl;
  305. break;
  306.  
  307. case 8:
  308. break;
  309. }
  310. }while(choice != 8);
  311.  
  312. return 0;
  313. }
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008
Jun 20th, 2008
0

Re: help with Sorting a Linked list is needed!

Oh thank u so much!
there were some additional lines and some stuff that i didn't need, i fixed them and i got the program i wanted... thanx again raul!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anifreak is offline Offline
5 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: switch color of CListCtrl text
Next Thread in C++ Forum Timeline: What is C++ Good For?





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


Follow us on Twitter


© 2011 DaniWeb® LLC