User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,489 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,693 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3535 | Replies: 3
Reply
Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

help implementing singly linked list

  #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.

#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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: help implementing singly linked list

  #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:

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
Reply With Quote  
Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

Re: help implementing singly linked list

  #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!

#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;
}
Reply With Quote  
Join Date: Aug 2008
Posts: 15
Reputation: findsyntax has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 2
findsyntax findsyntax is offline Offline
Newbie Poster

Re: help implementing singly linked list

  #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
# 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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC