Thread: SIngle Linked List processing

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    10

    SIngle Linked List processing

    I wrote two functions to delete first list item. One with argument (a pointer that shows the head of the list. i wanted to choose which list through it's header, if i had more than one) and one without argument (head of the list is global pointer as you can see in code). The problem is that delete_first1() is working right (i use the print_list() to see what is happening) but delete_first2(struct node *ptr) is not. Can someone see why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    struct node *head=NULL;
    struct node *current=NULL;
    
    void insert_first(int number)
    {
        struct node *link =  (struct node *)malloc(sizeof(struct node)); //create the new node dynamically
        link->data = number; //entering data field
        link->next = head; //next field points where head was pointing
        head = link;//head points to the new node
    }
    
    
    void print_list()
    {
        struct node *ptr=head; //a pointer shows to head of list
        while(1)
        {
            if(ptr==NULL) break;
            printf("%d ",ptr->data); //print what you see
            ptr=ptr->next;//points to next node
        }
    }
    
    struct node *delete_first1()
    {
        if(head==NULL)return NULL;
    
        struct node *temptr=head;
        head=head->next;
    
        return temptr;
    }
    
    struct node *delete_first2(struct node *ptr)
    {
        if(ptr==NULL)return NULL;
    
        struct node *temptr=ptr;
        ptr=ptr->next;
    
        return temptr;
    }
    
    int isEmpty(struct node *ptr)
    {
        if(ptr==NULL)return 1;
        return 0;    
    }
    
    struct node *search(struct node *ptr, int k)
    {
        while(ptr!=NULL)
        {
            if(ptr->data==k)
                return ptr;
            ptr=ptr->next;
        }
        return NULL;
    }
    
    
    int main()
    {
        int a1=11,a2=12,a3=13,a4=14;
    
        
        insert_first(a1);
        insert_first(a2);
        insert_first(a3);
        insert_first(a4);
    
        print_list();
        delete_first2(head);
        //delete_first1();
        print_list();
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    Well the first one updates a global variable.

    The second call should be
    head = delete_first2(head);

    Try writing all the code so that head is declared as a local variable in main, and you always call

    func(head)
    for functions which don't modify the list, and

    head = func(head)
    for functions which do modify the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    So, i have to return ptr and not temptr, right?

  4. #4
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    struct node *insert_first(struct node *ptr, int number)
    {
        struct node *link =  (struct node *)malloc(sizeof(struct node)); //create the new node dynamically
        link->data = number; //entering data field
        link->next = ptr; //next field points where head was pointing
        ptr = link;//head points to the new node
    
        return ptr;
    }
    
    void print_list(struct node *ptr)
    {
        struct node *temptr=ptr;
        while(temptr!=NULL)
        {
            printf("%d ",temptr->data); //print what you see
            temptr=temptr->next;//points to next node
        }
    }
    
    struct node *delete_first(struct node *ptr)
    {
        if(ptr==NULL)return NULL;
    
        ptr=ptr->next;
    
        return ptr;
    }
    
    int isEmpty(struct node *ptr)
    {
        if(ptr==NULL)return 1;
        return 0;    
    }
    
    struct node *search(struct node *ptr, int k)
    {
        struct node *temptr = ptr;
        while(temptr!=NULL)
        {
            if(temptr->data==k)
                return temptr;
            temptr=temptr->next;
        }
        return NULL;
    }
    
    int main()
    {
        struct node *head=NULL;
        int a1=11,a2=12,a3=13,a4=14;
    
        printf("%d\n",isEmpty(head));
        head = insert_first(head,a1);
        head = insert_first(head,a2);
        head = insert_first(head,a3);
        head = insert_first(head,a4);
        printf("%d\n",isEmpty(head));
        print_list(head);
        head = delete_first(head);
        print_list(head);
        if(search(head,13)!=NULL)
            printf("Found 13\n");
        head = delete_first(head);
        print_list(head);
        if(search(head,13)==NULL)
            printf("Not found 13\n");
        
        return 0;
    }
    Thank you Salem!
    Last edited by Quinhagen; 07-15-2016 at 08:18 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    When you call your delete function, something needs to call free() to balance the malloc().

    Otherwise, you have a memory leak.

    Other than that, it's looking much improved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    Like this?

    Code:
    struct node *delete_first(struct node *ptr)
    {
        if(ptr==NULL)return NULL;
        struct node *temptr=ptr;
    
        ptr=ptr->next;
        free(temptr);
        
        return ptr;
    }

  7. #7
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    yes like this. thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Help with Insertion sort on a single linked list
    By goron350 in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2005, 08:38 PM
  3. single linked list
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 10:27 AM
  4. Changing single to double linked list
    By BR7 in forum C Programming
    Replies: 1
    Last Post: 09-06-2002, 07:21 PM
  5. Single-linked list help.
    By Nit in forum C Programming
    Replies: 3
    Last Post: 03-21-2002, 06:08 PM

Tags for this Thread