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;
}