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!