Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to build a list

Status
Not open for further replies.

Greenleaf

Programmer
Feb 14, 2002
93
IT
Hello, can somebody of you give a hint on how to build a list (ordered list) ?
I think that I should declare elements like this:
Code:
struct node
     {
         int data;
         struct node *next;
         struct node *prev;
     };
How should I start building the list if I don't know from the beginning how many elements there will be in the list?
Thank you.
 
Hi,

I try to help you as I can by giving you a function.
I don't test this but I think it should correctly work.


typedef struct node anode;

/* Don't forget to initialize new_node->prev & next with NULL */
void AddNode( anode any_node, anode *new_node )
{
void *cur_node;

if( any_node != NULL && new_node != null )
{
cur_node = any_node;
while( cur_node.data >= new_node.data && cur_node.data != NULL)
{
new_node->next = any_node;
new_node->prev = any_node->prev;
new_node->prev->next = new_node;
any_node->prev = new_node;

cur_node = new_node->prev;
}
cur_node = any_node;
while { cur_node.data < new_node.data && cur_node.data != NULL}
{
new_node->prev = any_node;
new_node->next = any_node->next;
new_node->next->prev = new_node;
any_node->next = new_node;

cur_node = new_node->next;
}
}
}


 
My favorite, Linked lists. I wrote this a few years
ago while learning C.

//*****************************
struct structure
{
// This line contains the data that this
// container carries.
char textline [81];

// This is the link to the next node.
struct structure *nextptr;
};

//these are aliases for the buffer structure.
typedef struct structure STRCT;
typedef STRCT *PTR;

// The pointer *PTR is the pointer that is used
// to declare any new nodes.
//*****************************

//variable declarations.
PTR ptr;
PTR savedptr;
PTR headptr;
PTR wrkptr;


//////////////////////////////////////////////
// The function allocats the memory for the new node.
STRCTPTR Add_Node (void)
{
//*********************************
#ifdef testrun
Application->MessageBox(&quot;Add_Node ();\n\nAdding new node.&quot;,
&quot;DEBUG &quot;, MB_OK);
#endif
//*********************************

TXTPTR newptr = new structure;

//******************************************
//Initializes the structure.
newptr->textline [0] = NULL;
newptr->nextptr = NULL;
//******************************************

//Return the pointer to the new node created.
return newptr;
}
//////////////////////////////////////////////
// Nuff said.
void Delete_Node (void)
{
//*********************************
#ifdef deletenode
Application->MessageBox
(&quot;delete_node ();\n\nDeleting entry from memory.&quot;, &quot;DEBUG &quot;,
MB_OK);
#endif
//*********************************

// The ptr is the first entry in the list. The ptr is
// incremented to the nextptr to reestablish the start of the list.
if (ptr == headptr)
{
headptr = ptr->nextptr;
delete ptr;
}

//The object to delete is inside the list.
else
{
// Go through the list and compare the title to the ptr->field.
for (wrkptr = headptr; wrkptr->nextptr != NULL; wrkptr =
wrkptr->nextptr)
{
// Here the determinating status is found
// to halt the loop and delete the appropriate node.
if (!strcmp (title, wrkptr->textline))
{
savedptr->nextptr = wrkptr->nextptr;
delete wrkptr;
wrkptr = NULL;
break;
}
else
{
// Save address of object before object to delete.
// The saved ptr is either declared at the beginning
// of the function or is a global.
savedptr = wrkptr;
}
}
}
}
//////////////////////////////////////////////
// Clears any list resident in memory.
void clear_linked_list (void)
{
PTR nextptr;

//******************************************
ptr = headptr;

if (ptr != NULL)
{
while (ptr != NULL)
{
nextptr = ptr->nextptr;
free (ptr);
ptr = nextptr;
}
//Free the last ptr.
free (ptr);

headptr = NULL;
}//end of if.
//******************************************
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top