help with Sorting a Linked list is needed!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 5
Reputation: anifreak is an unknown quantity at this point 
Solved Threads: 0
anifreak anifreak is offline Offline
Newbie Poster

help with Sorting a Linked list is needed!

 
0
  #1
Jun 19th, 2008
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');
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: help with Sorting a Linked list is needed!

 
0
  #2
Jun 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: help with Sorting a Linked list is needed!

 
0
  #3
Jun 19th, 2008
Well, I suppose this would solve your problem. You can merge the sort() into the insert().
Here's the code:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: anifreak is an unknown quantity at this point 
Solved Threads: 0
anifreak anifreak is offline Offline
Newbie Poster

Re: help with Sorting a Linked list is needed!

 
0
  #4
Jun 20th, 2008
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!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC