Tuesday, 14 March 2017

Singly Linked list

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

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

int initilize(void){
    head=(struct node *)malloc(sizeof (struct node));
    head->data=0;
    head->next=NULL;
}

int display(struct node *head){
    if(head==NULL)
        printf("No data avaliable\n");
    else{
        while(head !=NULL){
        printf("%d ",head->data);
        head=head->next;
        }
        printf("\n");
    }
}

int insert(struct node *head,int val)
{
    struct node *temp=(struct node *)malloc(sizeof (struct node));
    temp->data=val;
    temp->next=NULL;

    while(head->next != NULL){
    head=head->next;
    }
    head->next=temp;
}

struct node * delete_first(struct node *head)
{
    if(head==NULL)
        printf("No data avaliable\n");
    else{
        head=head->next;
    }
    return head;
}
struct node * delete(struct node *head,int val)
{
    if(head==NULL)
        printf("No data avaliable\n");
    else{
        while(head->data!=val){
           
        }
        head=head->next;
    }
    return head;
}

int main()
{
    printf("Hello, World!\n");
    initilize();
    display(head);
    insert(head,10);
    insert(head,20);
    insert(head,30);

    display(head);
    head=delete_first(head);
    display(head);
    head=delete_first(head);
    display(head);
   
    return 0;
}

No comments:

Post a Comment