teaLeef
teaLeef

Reputation: 1989

Creating a linked list to implement a queue

I am newbie in C and I am trying to write a linked list in which each node simply contains an int. The definition of the structure is ok, but I also want to write methods to update this linked list (add element at the tail and delete the head element). (I want to be able to read the most recently added element)

I wrote the functions below, but I don't know where the free should take place and how to implement it. Could anyone help me with this?

typedef struct Node{
    Node next = NULL;
    int number;
} Node;

void add_node(Node *LL,int val){ 
    // add node to the end of the linked list
    new_node = (struct Node *)malloc(1*sizeof(struct Node));
    new_node->number = val;
    Node n = *LL;
    while (n.next != NULL){
        n = n.next;
    }
    n.next = new_node;
}

void delete_head(Node *LL){
     // update the head
    *LL = LL->next;
    //free?
}

void update_LL(*LL,int val){
    add_node(*LL,val);
    delete_head(*LL);
}

Upvotes: 0

Views: 112

Answers (4)

Jared Wadsworth
Jared Wadsworth

Reputation: 847

Possibly a duplicate of this question LinkedList - How to free the memory allocated using malloc

Basically you have to store the pointer to the node you want to delete otherwise you will leak that memory as there will be no reference to that memory location anywhere in your code.

try this in your delete_head function:

Node *temp = LL;

*LL = LL->next;

free(LL);

Hope this helps!

Upvotes: 0

user3522371
user3522371

Reputation:

I rename your data structure this way:

struct pointer
            {
            int field; 

            struct pointer *link; 
            };
typedef struct pointer cell;  

Then we can use this function for your need:

void ad_an_element_at_the_end_of_the_list()
         {
         cell *p=NULL;
         cell *ptr=head;

         int value;

         cout<<" Integer number to insert at the end of the list: ";
         cin>>value;
         p=(cell*)malloc(sizeof(cell));
         p->field=value;
         p->link=NULL;
         if(ptr==NULL) 
            {
            ptr=p;
            head=ptr;

            }else
                {
                if(ptr->link==NULL)  t
                  {
                  ptr->link=p;
                  head=ptr;

                  }else
                     {
                      while(ptr->link!=NULL) 
                        {
                        ptr=ptr->link;
                        }
                     ptr->link=p;

                    }
            }
    }

Upvotes: 1

Murtaza Zaidi
Murtaza Zaidi

Reputation: 597

You need to save your link to node which is next before you delete current node. Otherwise you won't be able to refer any node of your linkedlist. Now when you have backed up your link to next node, you can free current node pointed by LL, and then assign LL pointer to next node which you had earlier backed up in temp pointer.

Node *temp = LL->next;
free(LL);
LL = temp;

Upvotes: 0

AeroX
AeroX

Reputation: 3443

Try changing *LL = LL->next; for Node *nextNode = LL->next;. Then you can call free(LL) followed by LL = nextNode.

void delete_head(Node *LL){
    Node *nextNode = LL->next;
    free(LL);
    LL = nextNode;
}

This then frees the Node at the head and moves the pointer to the next one in the Linked List.

Upvotes: 0

Related Questions