![]() |
| | #1 |
| Registered User Join Date: Jul 2003
Posts: 85
| ![]() Code: /* File: linked-lists.c
* Date: November 12, 2003
* Info: Example of linked lists. There's no better way of
* learning pointers than learning linked lists.
* Author: Sean
*
* References:
* http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
*/
#include <stdio.h>
#include <stdlib.h>
struct node* initialize_list(void);
int add_node(struct node** headptr, int value);
int del_node(struct node** headptr);
int del_list(struct node* head);
int count_list(struct node* head);
int display_list(struct node* head);
struct node {
int data;
struct node* next;
};
int main(void)
{
struct node* head;
int count;
head = initialize_list();
add_node(&head,8);
add_node(&head,2);
add_node(&head,5);
add_node(&head,7);
count = count_list(head);
printf("Number of nodes in list: %d\n",count);
puts("The list currently contains: ");
display_list(head);
del_node(&head);
puts("After deleting a node\n");
count = count_list(head);
printf("Number of nodes in list: %d\n",count);
puts("The list currently contains: ");
display_list(head);
del_list(head); /* free the allocated memory (delete list) */
return 0;
}
/* initializes our list. */
struct node* initialize_list(void)
{
struct node* head = NULL;
return head;
}
/* adds value to a node. if head node is empty, */
/* the value is added there, if not, it's added */
/* to a new node before the head pointer and */
/* that node becomes the new head pointer. */
int add_node(struct node** headptr, int value)
{
struct node* tmp = NULL;
tmp = malloc(sizeof(struct node));
if( tmp == NULL )
return -1; /* memory allocation failed */
else {
tmp->data = value;
if( *headptr == NULL ) { /* if the head pointer is empty */
*headptr = tmp;
return 0;
}
tmp->next = *headptr;
*headptr = tmp;
}
return 0;
}
/* deletes the head node. we pass a pointer to */
/* our head pointer in this case because we need */
/* to alter our head pointer. */
int del_node(struct node** headptr)
{
struct node* tmp;
if( *headptr == NULL )
return -1; /* nothing to delete */
else {
tmp = *headptr;
*headptr = (*headptr)->next;
free(tmp);
}
return 0;
}
/* deletes the entire list. frees up all the */
/* allocated memory. uses the del_node function. */
int del_list(struct node* head)
{
struct node* cur = head;
while( cur != NULL ) {
del_node(&cur);
}
return 0;
}
/* counts the number of nodes in list and returns it */
int count_list(struct node* head)
{
struct node* cur = head;
int count;
/* goes through list */
for( count = 0; cur != NULL; cur = cur->next)
count++;
return count;
}
/* displays the contents of the list */
int display_list(struct node* head)
{
struct node* cur = head;
if( cur == NULL ) {
printf("The list is empty!");
return 0;
}
while( cur != NULL ) {
printf("%d\n",cur->data);
cur = cur->next;
}
return 0;
}
/* EOF */
|
| scrappy is offline | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,900
| del_node() and del_list() are fine. The problem is in add_node() when *headptr == NULL. You assign tmp to *headptr but tmp->next is uninitialized. gg |
| Codeplug is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 5,034
| >> tmp->next = *headptr; Here, you are inserting the new node *in front* the head (meaning you'll never see it again). You need to traverse to the end of the list, and point the last node's next ptr to the new node. The new node must have it's next ptr set to NULL. Then the very next line goes: >> *headptr = tmp; That's a memory leak. headptr now points to temp, and the memory headptr *was* pointing to is lost forever (so to speak). Everything else is OK. |
| Sebastiani is offline | |
| | #4 | ||
| Registered User Join Date: Jul 2003
Posts: 85
| Quote:
Quote:
Code: tmp->data = value;
if( *headptr == NULL ) { /* if the head pointer is empty */
*headptr = tmp;
return 0;
thanks for all of your guys' help ![]() scrappy | ||
| scrappy is offline | |
| | #5 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| You really don't even need to check for NULL, It's pointless to do so. Code: int add_node(struct node** headptr, int value)
{
struct node* tmp = NULL;
tmp = malloc(sizeof(struct node));
if( tmp == NULL )
return -1; /* memory allocation failed */
else {
tmp->data = value;
tmp->next = *headptr;
*headptr = tmp;
}
return 0;
}
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #6 |
| Guest Join Date: Aug 2001
Posts: 5,034
| >> i thought i was just moving the old headptr to the the new headptr's next. My mistake. I see what you were doing now. |
| Sebastiani is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked List Not Saving Value as Int | bar338 | C Programming | 4 | 05-04-2009 07:53 PM |
| C++ Linked list program need help !!! | dcoll025 | C++ Programming | 1 | 04-20-2009 10:03 AM |
| Unknown memory leak with linked lists... | RaDeuX | C Programming | 6 | 12-07-2008 04:09 AM |
| Problem with linked list ADT and incomplete structure | prawntoast | C Programming | 1 | 04-30-2005 01:29 AM |
| 1st Class LIST ADT | Unregistered | C++ Programming | 1 | 11-09-2001 07:29 PM |