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 »

Static list Head and tail pointer in 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
{
public:
// Static list head pointer.
static ListEntry* firstentry;

private:
// Static list tail pointer.
static ListEntry* lastentry;

char* listvalue;
ListEntry* nextentry;

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

// Define the static pointers.
ListEntry* ListEntry::firstentry; // static list head pointer.
ListEntry* ListEntry::lastentry; // static list tail pointer.

// The constructor.
ListEntry::ListEntry(char* s)
{
if (firstentry == 0)
firstentry = this;
if (lastentry != 0)
lastentry->nextentry = this;
lastentry = this;
listvalue = new char[std::strlen(s)+1];
std::strcpy(listvalue, s);
nextentry = 0;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Read in some names.
while (1)
{
std::cout << "\nEnter 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.
new ListEntry(name);
}

// Get a pointer to the first list entry.
ListEntry* next = ListEntry::firstentry;

// Display the names.
while (next != 0)
{
next->display();
ListEntry* hold = next;
next = next->NextEntry();
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.
Static list Head and tail pointer in 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: Add the entry to the linked list using c++
Previous Resource: Deletes all array elements 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