-
Inserting nodes into a singly linked list
I cannot figure out why the "head_ptr" in the function:
int node_ins(struct ListNode *head_ptr, char new_uid[255]);
gets modified when inside the function, but loses any modification when the
function returns to main.
int node_ins(struct ListNode *head_ptr, char new_uid[255])
{
struct ListNode *new_ptr, *prev_ptr, *curr_ptr;
new_ptr = malloc(sizeof(struct ListNode));
/* malloc fails */
if (new_ptr == NULL)
{
printf("Error: Not enough memory to log in user %s.\n", new_uid);
return 1;
}
strcpy(new_ptr->uid, new_uid);
new_ptr->next_ptr = NULL;
/* list is empty */
if (head_ptr == NULL)
{
head_ptr = new_ptr;
return 0;
}
prev_ptr = NULL;
curr_ptr = head_ptr;
/* traverse list */
while (curr_ptr != NULL)
{
if (!strcmp(curr_ptr->uid, new_uid))
{
printf("Error: User %s already logged in.\n", new_uid);
return 1;
}
/* next node */
prev_ptr = curr_ptr;
curr_ptr = curr_ptr->next_ptr;
}
/* only 1 node in list */
if (prev_ptr == NULL)
{
new_ptr->next_ptr = head_ptr;
head_ptr = new_ptr;
}
/* insert in list */
else
{
prev_ptr->next_ptr = new_ptr;
new_ptr->next_ptr = curr_ptr;
}
return 0;
}
-
Re: Inserting nodes into a singly linked list
You are passing head_ptr by value. It is copied so the original value never
changes. If you want it to change you could pass a pointer to a pointer.
int node_ins(struct ListNode **head_ptr, char new_uid[255])
{
*head_ptr = new_ptr;
}
Brian
"hyyx" <olskool@email.com> wrote:
>
>I cannot figure out why the "head_ptr" in the function:
>
> int node_ins(struct ListNode *head_ptr, char new_uid[255]);
>
>gets modified when inside the function, but loses any modification when
the
>function returns to main.
>
>int node_ins(struct ListNode *head_ptr, char new_uid[255])
>{
> struct ListNode *new_ptr, *prev_ptr, *curr_ptr;
> new_ptr = malloc(sizeof(struct ListNode));
>
> /* malloc fails */
> if (new_ptr == NULL)
> {
> printf("Error: Not enough memory to log in user %s.\n", new_uid);
> return 1;
> }
>
> strcpy(new_ptr->uid, new_uid);
> new_ptr->next_ptr = NULL;
>
> /* list is empty */
> if (head_ptr == NULL)
> {
> head_ptr = new_ptr;
> return 0;
> }
>
> prev_ptr = NULL;
> curr_ptr = head_ptr;
>
> /* traverse list */
> while (curr_ptr != NULL)
> {
> if (!strcmp(curr_ptr->uid, new_uid))
> {
> printf("Error: User %s already logged in.\n", new_uid);
> return 1;
> }
>
> /* next node */
> prev_ptr = curr_ptr;
> curr_ptr = curr_ptr->next_ptr;
> }
>
> /* only 1 node in list */
> if (prev_ptr == NULL)
> {
> new_ptr->next_ptr = head_ptr;
> head_ptr = new_ptr;
> }
>
> /* insert in list */
> else
> {
> prev_ptr->next_ptr = new_ptr;
> new_ptr->next_ptr = curr_ptr;
> }
> return 0;
>}
-
Re: Inserting nodes into a singly linked list
int node_ins(struct ListNode *& head_ptr, char new_uid[255]);
and call the function as you already did.
Mike.
www.exontrol.com
"hyyx" <olskool@email.com> wrote:
>
>I cannot figure out why the "head_ptr" in the function:
>
> int node_ins(struct ListNode *head_ptr, char new_uid[255]);
>
>gets modified when inside the function, but loses any modification when
the
>function returns to main.
>
>int node_ins(struct ListNode *head_ptr, char new_uid[255])
>{
> struct ListNode *new_ptr, *prev_ptr, *curr_ptr;
> new_ptr = malloc(sizeof(struct ListNode));
>
> /* malloc fails */
> if (new_ptr == NULL)
> {
> printf("Error: Not enough memory to log in user %s.\n", new_uid);
> return 1;
> }
>
> strcpy(new_ptr->uid, new_uid);
> new_ptr->next_ptr = NULL;
>
> /* list is empty */
> if (head_ptr == NULL)
> {
> head_ptr = new_ptr;
> return 0;
> }
>
> prev_ptr = NULL;
> curr_ptr = head_ptr;
>
> /* traverse list */
> while (curr_ptr != NULL)
> {
> if (!strcmp(curr_ptr->uid, new_uid))
> {
> printf("Error: User %s already logged in.\n", new_uid);
> return 1;
> }
>
> /* next node */
> prev_ptr = curr_ptr;
> curr_ptr = curr_ptr->next_ptr;
> }
>
> /* only 1 node in list */
> if (prev_ptr == NULL)
> {
> new_ptr->next_ptr = head_ptr;
> head_ptr = new_ptr;
> }
>
> /* insert in list */
> else
> {
> prev_ptr->next_ptr = new_ptr;
> new_ptr->next_ptr = curr_ptr;
> }
> return 0;
>}
-
Re: Inserting nodes into a singly linked list
Brian Preston wrote:
>
> You are passing head_ptr by value. It is copied so the original value never
> changes. If you want it to change you could pass a pointer to a pointer.
>
> int node_ins(struct ListNode **head_ptr, char new_uid[255])
> {
> *head_ptr = new_ptr;
> }
>
> Brian
To avoid the complexities of pointers to pointers, one can pass the
pointer by reference:
int node_ins(struct ListNode *& head_ptr, char new_uid[255])
{
head_ptr = new_ptr;
}
This is a tad cleaner I think.
Danny
>
>
>
> "hyyx" <olskool@email.com> wrote:
> >
> >I cannot figure out why the "head_ptr" in the function:
> >
> > int node_ins(struct ListNode *head_ptr, char new_uid[255]);
> >
> >gets modified when inside the function, but loses any modification when
> the
> >function returns to main.
> >
> >int node_ins(struct ListNode *head_ptr, char new_uid[255])
> >{
> > struct ListNode *new_ptr, *prev_ptr, *curr_ptr;
> > new_ptr = malloc(sizeof(struct ListNode));
> >
> > /* malloc fails */
> > if (new_ptr == NULL)
> > {
> > printf("Error: Not enough memory to log in user %s.\n", new_uid);
> > return 1;
> > }
> >
> > strcpy(new_ptr->uid, new_uid);
> > new_ptr->next_ptr = NULL;
> >
> > /* list is empty */
> > if (head_ptr == NULL)
> > {
> > head_ptr = new_ptr;
> > return 0;
> > }
> >
> > prev_ptr = NULL;
> > curr_ptr = head_ptr;
> >
> > /* traverse list */
> > while (curr_ptr != NULL)
> > {
> > if (!strcmp(curr_ptr->uid, new_uid))
> > {
> > printf("Error: User %s already logged in.\n", new_uid);
> > return 1;
> > }
> >
> > /* next node */
> > prev_ptr = curr_ptr;
> > curr_ptr = curr_ptr->next_ptr;
> > }
> >
> > /* only 1 node in list */
> > if (prev_ptr == NULL)
> > {
> > new_ptr->next_ptr = head_ptr;
> > head_ptr = new_ptr;
> > }
> >
> > /* insert in list */
> > else
> > {
> > prev_ptr->next_ptr = new_ptr;
> > new_ptr->next_ptr = curr_ptr;
> > }
> > return 0;
> >}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks