| | |
help implementing singly linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<process.h> class Soldier { public : Soldier * Sort(Soldier *); void Show(Soldier *); private: Soldier * ptrNext; int kills; int sorter; char nickname[25]; }; Soldier * Soldier::Sort(Soldier* temp) { Soldier * ptrNew; Soldier* dummy1; ptrNew = new Soldier; cout << "\nEnter soldiers nickname\n"; cin >> nickname; cout << "\nEnter number of kills for the month:\n"; cin >> sorter; dummy1 = temp; if (temp == NULL || dummy1->kills > sorter) { ptrNew->kills = sorter; ptrNew->ptrNext=temp; temp=ptrNew; return temp; } else { Soldier * ptrCurrent; Soldier * dummy; ptrCurrent = temp; dummy = ptrCurrent->ptrNext ; while(ptrCurrent->ptrNext!=NULL) { if(dummy->kills > sorter) { ptrNew->ptrNext = ptrCurrent->ptrNext; ptrNew->kills = sorter; ptrCurrent->ptrNext = ptrNew; return temp; } dummy = dummy->ptrNext ; ptrCurrent = ptrCurrent->ptrNext; } ptrNew->kills = sorter; ptrNew->ptrNext=NULL; ptrCurrent->ptrNext=ptrNew; } return temp; } void Soldier::Show(Soldier *temp) { while(temp!=NULL) { cout << nickname << temp->kills << endl; temp = temp->ptrNext; } } void main() { Soldier *first=NULL,l1; int choice; while(1) { cout<<"-> [1] Insert Soldier \n-> [2] View Soldiers \n-> [3] Exit\n"; cin>>choice; switch (choice) { case 1: first=l1.Sort(first); break; case 2: l1.Show(first); break; case 3: exit(0); } } }
Last edited by alc6379; Aug 7th, 2004 at 7:27 pm.
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:
It makes your code easier to read, and you can keep all of the indentation and the formatting that makes the code readible.
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:
C++ Syntax (Toggle Plain Text)
Soldier *first=NULL,l1; int choice; while(1) { cout<<"-> [1] Insert Soldier \n-> [2] View Soldiers \n-> [3] Exit\n"; cin>>choice; switch (choice) { case 1: first=l1.Sort(first); break; case 2: l1.Show(first); break; case 3: 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
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Solved Threads: 0
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!
i'm getting some parse errors in the addSoldier function, help!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string.h> class Soldier { public: Soldier(){} ~Soldier(){} void setKills(int setKills){kills = setKills;} int getKills(){return kills;} void setNickname(char setNickname[25]){nickname[25] = setNickname[25];} char getNickname(){return nickname[25];} void setEmail(char setEmail[50]){email[50] = setEmail[50];} char getEmail(){return email[50];} void setRank(char setRank[]); char getRank(){return rank[10];} void Show(); private: int kills; char rank[10]; char nickname[25]; char email[50]; Soldier * ptrNext; }; void Soldier::setRank(char setRank[]) { if(kills >= 1000) char setRank[] = "General"; if(kills < 1000 && kills >= 500) char setRank[] = "Captain"; if(kills < 500 && kills > 1) char setRank[] = "Soldier"; if(kills <= 1) char setRank[] = "Trainee"; strcpy(rank,setRank); } void Soldier::Show() { std::cout << nickname[25] << email[50] << kills << rank[10]; } class LinkedList { public: LinkedList(){ptrHead = NULL;} Soldier * addSoldier(Soldier *); int removeLowest(); private: Soldier * ptrHead; Soldier * ptrCurrent; }; Soldier * LinkedList::addSoldier(Soldier * ptrTemp1) { Soldier * ptrNew = new Soldier; Soldier * ptrTemp2; cout << "\nEnter nickname\n"; char setNickname[25]; cin >> setNickname; ptrNew->setNickname(char setNickname[25]); cout << "\nEnter email address\n"; char setEmail[50]; cin >> setEmail; ptrNew->setEmail(char setEmail[50]); cout << "\nEnter number of kills\n"; int setKills; cin >> setKills; ptrNew->setKills(int setKills); } int LinkedList::removeLowest() { if(ptrHead == NULL) { std::cout << "No more soldiers to remove!/n"; return 0; } } int main() { return 0; }
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 2
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
Regards,
Rammohan Alampally,
<snip false signature>
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
C++ Syntax (Toggle Plain Text)
# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; /* Insert the Value in the List */ struct node *insert(struct node *p, int n) { struct node *temp; if(p==NULL) { p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("Error\n"); exit(0); } p-> data = n; p-> link = NULL; } else p->link = insert(p->link,n);/* the while loop replaced by recursive call */ return (p); } /* a function to sort reverse list */ struct node *reverse(struct node *p) { struct node *prev, *curr; prev = NULL; curr = p; while (curr != NULL) { p = p-> link; curr-> link = prev; prev = curr; curr = p; } return(prev); } /* a function to sort a list */ struct node *sortlist(struct node *p) { struct node *temp1,*temp2,*min,*prev,*q; q = NULL; while(p != NULL) { prev = NULL; min = temp1 = p; temp2 = p -> link; while ( temp2 != NULL ) { if(min -> data > temp2 -> data) { min = temp2; prev = temp1; } temp1 = temp2; temp2 = temp2-> link; } if(prev == NULL) p = min -> link; else prev -> link = min -> link; min -> link = NULL; if( q == NULL) 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*/ else { temp1 = q; /* traverses the list pointed to by q to get pointer to its last node */ while( temp1 -> link != NULL) temp1 = temp1 -> link; /* 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*/ temp1 -> link = min; } } return (q); } void printlist ( struct node *p ) { printf("The data values in the list are\n"); while (p!= NULL) { printf("%d\t",p-> data); p = p-> link; } } void main() { int n; int x; struct node *start = NULL ; printf("Enter the nodes to be created \n"); scanf("%d",&n); while ( n- > 0 ) { printf( "Enter the data values to be placed in a node\n"); scanf("%d",&x); start = insert ( start, x ); } printf("The created list is\n"); printlist ( start ); }
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
![]() |
Similar Threads
- Code Snippet: Singly Linked List Implementation (C)
- how to create linked list? (C)
- Performing MergeSort on a singly linked list (C#)
- Singly Linked List - help with error (C++)
- Reversing singly linked list? (C++)
- deleting a random node from a singly linked list (C++)
- problem with singly linked list (C++)
- Deleting any node from a singly linked list (C++)
- Deleting Pointed node in Singly Linked List (C)
Other Threads in the C++ Forum
- Previous Thread: TAPI
- Next Thread: Explain the the problem for me. plzz..
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






