C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » General »

Add the entry to the linked list using c++


Posted Date: 24 Aug 2009    Resource Type: Code Snippets    Category: General
Author: Santosh Kumar SinghMember Level: Gold    
Rating: 1 out of 5Points: 4



////////////////////////////////////////
#include
#include

////////////////////////////////////////
// The ListEntry class.
////////////////////////////////////////
class ListEntry
{
char* listvalue;
ListEntry* preventry;

public:
ListEntry(char*);
~ListEntry()
{ delete [] listvalue; }
ListEntry* PrevEntry() const
{ return preventry; };
void display() const
{ std::cout << std::endl << listvalue; }

// Use the 'this' pointer to chain the list.
void AddEntry(ListEntry& le)
{ le.preventry = this; }
};

// The constructor definition.
ListEntry::ListEntry(char* s)
{
listvalue = new char[std::strlen(s)+1];
std::strcpy(listvalue, s);
preventry = 0;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
ListEntry* prev = 0;

// Read in some names.
while (1)
{
std::cout << std::endl
<< "Enter a name ('end' when done): ";
char name[25];
std::cin >> name;

if (std::strncmp(name, "end", 3) == 0)
break;

// Make a list entry of the name.
ListEntry* list = new ListEntry(name);

// Add the entry to the linked list.
if (prev != 0)
prev->AddEntry(*list);
prev = list;
}

// Display the names in reverse order.
while (prev != 0)
{
prev->display();
ListEntry* hold = prev;
prev = prev->PrevEntry();

// Delete the ListEntry object.
delete hold;
}

return 0;
}



Responses

Author: Miss Meetu Choudhary    27 Aug 2009Member Level: Diamond   Points : 2
Must Add Description to your code


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Add the entry to the linked list using c++  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: To lock and unlock a folder
Previous Resource: Static list Head and tail pointer in c++
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use