简体   繁体   中英

Linked list implementation in c without using double-pointer

I have implemented a simple linked list in C language, but can it be implemented without using double-pointer(**).I want to implement same program by using only single pointers.

#include <stdio.h>
#include <stdlib.h>

struct node
{
  int data;
  struct node *next;
};


void push(struct node** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}



void append(struct node** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    struct node *last = *head_ref;  /* used in step 5*/
    new_node->data  = new_data;
    new_node->next = NULL;
    if (*head_ref == NULL)
    {
       *head_ref = new_node;
       return;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    return;
}

void printList(struct node *node)
{
  while (node != NULL)
  {
     printf(" %d ", node->data);
     node = node->next;
  }
}
int main()
{
  struct node* head = NULL;
  append(&head, 6);
  push(&head, 7);
  push(&head, 1);
  append(&head, 4);
  printf("\n Created Linked list is: ");
  printList(head);
  getchar();
  return 0;
}

Is it possible to replace "struct node** head_ref" with "struct node* head_ref"?


Changed code after suggestions(still not getting output)

#include <stdio.h>
#include <stdlib.h>

struct node
{
  int data;
  struct node *next;
};


struct node* push(struct node* head, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = head;
    head   = new_node;
    return head;
}

struct node* append(struct node* head, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    struct node *last = head;  /* used in step 5*/
    new_node->data  = new_data;
    new_node->next = NULL;
    if (head == NULL)
    {
       head = new_node;
       return head;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    return head;
}


void printList(struct node *node)
{
  while (node != NULL)
  {
     printf(" %d ", node->data);
     node = node->next;
  }
}

int main()
{
  struct node* head = NULL;
  head= append(&head, 6);
  head=push(&head, 7);
  head=push(&head, 1);
  head=append(&head, 4);
  printf("\n Created Linked list is: ");
  printList(head);
  getchar();
  return 0;
}

Yes, you can rewrite this code using only single pointers, but you would have to change the semantic of your API and the pattern in which it is used.

Essentially, you replace the second level of indirection in

void push(struct node** head_ref, int new_data)

with a client-side assignment, ie

struct node* push(struct node* head, int new_data)

This means that instead of

push(&head, num);

the caller will have to write

head = push(head, num);

Same goes for the implementation of append .

将(&head,6)替换为(head,6)。由于您没有传递头部的地址,因此在接收端您具有push(struct node * head,int new_data)。以上给出的答案已对所有其他方法进行了澄清

Another solution is to create an empty node called head , and then create a pointer to that node called list . You can then pass list to all of the functions, like this

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void push(struct node *list, int new_data)
{
    struct node* new_node = malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = list->next;
    list->next = new_node;
}

void append(struct node *list, int new_data)
{
    while ( list->next != NULL ) 
        list = list->next;
    push( list, new_data );
}

void printList(struct node *node)
{
    for ( node=node->next; node != NULL; node=node->next )
        printf(" %d ", node->data);
    printf( "\n" );
}

int main( void )
{
    struct node head = { 0, NULL };
    struct node *list = &head;

    append(list, 6);
    push(list, 7);
    push(list, 1);
    append(list, 4);
    printf("\n Created Linked list is: ");
    printList(list);
    getchar();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM