help implementing singly linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

help implementing singly linked list

 
0
  #1
Aug 7th, 2004
hey all,

i'm a newbie to C++ programming and i'm having some difficulties implementing this singly linked list.

what it has to do is accept:
nickname
email address
number of kills

and store them in a node of the linked list in order according to number of kills.

here's what i've got, it seems to accept the number of kills, but i run into problems when i try to accept anything else.

  1. #include<iostream.h>
  2. #include<process.h>
  3. class Soldier
  4. {
  5. public :
  6. Soldier * Sort(Soldier *);
  7. void Show(Soldier *);
  8.  
  9. private:
  10. Soldier * ptrNext;
  11. int kills;
  12. int sorter;
  13. char nickname[25];
  14.  
  15. };
  16. Soldier * Soldier::Sort(Soldier* temp)
  17. {
  18. Soldier * ptrNew;
  19. Soldier* dummy1;
  20. ptrNew = new Soldier;
  21. cout << "\nEnter soldiers nickname\n";
  22. cin >> nickname;
  23. cout << "\nEnter number of kills for the month:\n";
  24. cin >> sorter;
  25. dummy1 = temp;
  26. if (temp == NULL || dummy1->kills > sorter)
  27. {
  28. ptrNew->kills = sorter;
  29. ptrNew->ptrNext=temp;
  30. temp=ptrNew;
  31. return temp;
  32. }
  33. else
  34. {
  35. Soldier * ptrCurrent;
  36. Soldier * dummy;
  37. ptrCurrent = temp;
  38. dummy = ptrCurrent->ptrNext ;
  39. while(ptrCurrent->ptrNext!=NULL)
  40. {
  41. if(dummy->kills > sorter)
  42. {
  43. ptrNew->ptrNext = ptrCurrent->ptrNext;
  44. ptrNew->kills = sorter;
  45. ptrCurrent->ptrNext = ptrNew;
  46. return temp;
  47. }
  48. dummy = dummy->ptrNext ;
  49. ptrCurrent = ptrCurrent->ptrNext;
  50. }
  51. ptrNew->kills = sorter;
  52. ptrNew->ptrNext=NULL;
  53. ptrCurrent->ptrNext=ptrNew;
  54. }
  55. return temp;
  56. }
  57. void Soldier::Show(Soldier *temp)
  58. {
  59. while(temp!=NULL)
  60. {
  61. cout << nickname << temp->kills << endl;
  62. temp = temp->ptrNext;
  63. }
  64.  
  65. }
  66.  
  67.  
  68.  
  69. void main()
  70. {
  71. Soldier *first=NULL,l1;
  72. int choice;
  73. while(1)
  74. {
  75. cout<<"-> [1] Insert Soldier \n-> [2] View Soldiers \n-> [3] Exit\n";
  76. cin>>choice;
  77.  
  78. switch (choice)
  79. {
  80. case 1:
  81. first=l1.Sort(first);
  82. break;
  83. case 2:
  84. l1.Show(first);
  85. break;
  86. case 3:
  87. exit(0);
  88.  
  89. }
  90.  
  91. }
  92. }
Last edited by alc6379; Aug 7th, 2004 at 7:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: help implementing singly linked list

 
0
  #2
Aug 7th, 2004
Hi Paynekiller! Welcome to TechTalk!

I just moderate this forum, so I wouldn't be able to help troubleshoot your code, but could you surround your code with [ code] and [ /code] tags? Like this:

  1.  
  2. Soldier *first=NULL,l1;
  3. int choice;
  4. while(1)
  5. {
  6. cout<<"-> [1] Insert Soldier \n-> [2] View Soldiers \n-> [3] Exit\n";
  7. cin>>choice;
  8.  
  9. switch (choice)
  10. {
  11. case 1:
  12. first=l1.Sort(first);
  13. break;
  14. case 2:
  15. l1.Show(first);
  16. break;
  17. case 3:
  18. exit(0);

It makes your code easier to read, and you can keep all of the indentation and the formatting that makes the code readible.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

Re: help implementing singly linked list

 
0
  #3
Aug 8th, 2004
done a bit of rehashing and came up with this, it seems to be more on the right track, doesn't it??

i'm getting some parse errors in the addSoldier function, help!

  1. #include <iostream>
  2. #include <string.h>
  3. class Soldier
  4. {
  5. public:
  6. Soldier(){}
  7. ~Soldier(){}
  8.  
  9. void setKills(int setKills){kills = setKills;}
  10. int getKills(){return kills;}
  11.  
  12. void setNickname(char setNickname[25]){nickname[25] = setNickname[25];}
  13. char getNickname(){return nickname[25];}
  14.  
  15. void setEmail(char setEmail[50]){email[50] = setEmail[50];}
  16. char getEmail(){return email[50];}
  17.  
  18. void setRank(char setRank[]);
  19. char getRank(){return rank[10];}
  20.  
  21. void Show();
  22.  
  23. private:
  24. int kills;
  25. char rank[10];
  26. char nickname[25];
  27. char email[50];
  28. Soldier * ptrNext;
  29. };
  30.  
  31. void Soldier::setRank(char setRank[])
  32. {
  33. if(kills >= 1000)
  34. char setRank[] = "General";
  35. if(kills < 1000 && kills >= 500)
  36. char setRank[] = "Captain";
  37. if(kills < 500 && kills > 1)
  38. char setRank[] = "Soldier";
  39. if(kills <= 1)
  40. char setRank[] = "Trainee";
  41. strcpy(rank,setRank);
  42. }
  43.  
  44. void Soldier::Show()
  45. {
  46. std::cout << nickname[25] << email[50] << kills << rank[10];
  47. }
  48.  
  49.  
  50. class LinkedList
  51. {
  52. public:
  53. LinkedList(){ptrHead = NULL;}
  54. Soldier * addSoldier(Soldier *);
  55. int removeLowest();
  56.  
  57. private:
  58. Soldier * ptrHead;
  59. Soldier * ptrCurrent;
  60. };
  61.  
  62. Soldier * LinkedList::addSoldier(Soldier * ptrTemp1)
  63. {
  64. Soldier * ptrNew = new Soldier;
  65. Soldier * ptrTemp2;
  66.  
  67. cout << "\nEnter nickname\n";
  68. char setNickname[25];
  69. cin >> setNickname;
  70. ptrNew->setNickname(char setNickname[25]);
  71. cout << "\nEnter email address\n";
  72. char setEmail[50];
  73. cin >> setEmail;
  74. ptrNew->setEmail(char setEmail[50]);
  75. cout << "\nEnter number of kills\n";
  76. int setKills;
  77. cin >> setKills;
  78. ptrNew->setKills(int setKills);
  79. }
  80.  
  81. int LinkedList::removeLowest()
  82. {
  83. if(ptrHead == NULL)
  84. {
  85. std::cout << "No more soldiers to remove!/n";
  86. return 0;
  87. }
  88. }
  89.  
  90. int main()
  91. {
  92. return 0;
  93. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: findsyntax has a little shameless behaviour in the past 
Solved Threads: 2
findsyntax findsyntax is offline Offline
Newbie Poster

Re: help implementing singly linked list

 
-1
  #4
Aug 21st, 2008
Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:
I have provided you my own sample code for your request: Insert, Reverse, print the list, Sort the list
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *link;
  8. };
  9. /* Insert the Value in the List */
  10. struct node *insert(struct node *p, int n)
  11. {
  12. struct node *temp;
  13. if(p==NULL)
  14. {
  15. p=(struct node *)malloc(sizeof(struct node));
  16. if(p==NULL)
  17. {
  18. printf("Error\n");
  19. exit(0);
  20. }
  21. p-> data = n;
  22. p-> link = NULL;
  23. }
  24. else
  25. p->link = insert(p->link,n);/* the while loop replaced by recursive call */
  26. return (p);
  27. }
  28.  
  29.  
  30. /* a function to sort reverse list */
  31. struct node *reverse(struct node *p)
  32. {
  33. struct node *prev, *curr;
  34. prev = NULL;
  35. curr = p;
  36. while (curr != NULL)
  37. {
  38. p = p-> link;
  39. curr-> link = prev;
  40. prev = curr;
  41. curr = p;
  42. }
  43. return(prev);
  44. }
  45.  
  46. /* a function to sort a list */
  47. struct node *sortlist(struct node *p)
  48. {
  49. struct node *temp1,*temp2,*min,*prev,*q;
  50. q = NULL;
  51. while(p != NULL)
  52. {
  53. prev = NULL;
  54. min = temp1 = p;
  55. temp2 = p -> link;
  56. while ( temp2 != NULL )
  57. {
  58. if(min -> data > temp2 -> data)
  59. {
  60. min = temp2;
  61. prev = temp1;
  62. }
  63. temp1 = temp2;
  64. temp2 = temp2-> link;
  65. }
  66. if(prev == NULL)
  67. p = min -> link;
  68. else
  69. prev -> link = min -> link;
  70. min -> link = NULL;
  71. if( q == NULL)
  72. q = min; /* moves the node with lowest data value in the list pointed to by p to the list pointed to by q as a first node*/
  73. else
  74. {
  75. temp1 = q;
  76. /* traverses the list pointed to by q to get pointer to its last node */
  77. while( temp1 -> link != NULL)
  78. temp1 = temp1 -> link;
  79. /* moves the node with lowest data value in the list pointed to by p to the list pointed to by q at the end of list pointed by q*/
  80. temp1 -> link = min;
  81. }
  82. }
  83. return (q);
  84. }
  85.  
  86. void printlist ( struct node *p )
  87. {
  88. printf("The data values in the list are\n");
  89. while (p!= NULL)
  90. {
  91. printf("%d\t",p-> data);
  92. p = p-> link;
  93. }
  94. }
  95.  
  96. void main()
  97. {
  98. int n;
  99. int x;
  100. struct node *start = NULL ;
  101. printf("Enter the nodes to be created \n");
  102. scanf("%d",&n);
  103. while ( n- > 0 )
  104. {
  105. printf( "Enter the data values to be placed in a node\n");
  106. scanf("%d",&x);
  107. start = insert ( start, x );
  108. }
  109. printf("The created list is\n");
  110. printlist ( start );
  111. }

Regards,
Rammohan Alampally,
<snip false signature>
Last edited by Ancient Dragon; Aug 21st, 2008 at 7:41 am. Reason: snip false signature and add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC