Singly linked list all operations by passing head

/*Code for:
  linked list through head
  1.insert at beginning
  2.inser after given position
  3.inser at end/append
  4.display
  5.reverse
  6.delete from beginning
  7.delete the given node value
  8.countnumber of nodes
 */
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;//*head is start

void insertBegin(struct node **head, int d);
void insertPosition(struct node **head, int position, int data);
void insertEnd(struct node **head, int d);
void display(struct node *head);
void count(struct node *head);
void reverse(struct node **head);
void deleteBeginning(struct node **head);
void deleteKey(struct node **head);



int main()
{
    printf("\n\n");
    head=NULL;//initially ll is empty

    printf("\nInserting 10,20,15,77,101,202\n");
    insertBegin(&head,10);
    insertBegin(&head,20);
    insertBegin(&head,15);
    insertBegin(&head,77);
    insertBegin(&head,101);
    insertBegin(&head,202);
    display(head);
    printf("\n\n");
 
    count(head);
    display(head);
    printf("\n\n");



    insertPosition(&head,1,333);//inserting at 1st ie 0,1th location
    display(head);
    printf("\n\n");

    insertEnd(&head,99);
    display(head);
    printf("\n\n");


    deleteBeginning(&head);
    display(head);
    printf("\n\n");

    reverse(&head);
    display(head);
    printf("\n\n");

    deleteKey(&head);
    display(head);
    printf("\n\n");

    return 0;
}

void insertBegin(struct node **head,int d)
{
    printf("\ninserBegin called.\n");
    //creating a node to insertBeginning
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=d;
    newnode->next=*head;
    *head=newnode;
}
void insertPosition(struct node **head, int position, int d)
{
    printf("\ninsertPosition called.\n");
    struct node *current=*head;
    int i;
    for(i=0;i<position;i++)
    {
        current=current->next;
    }
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=d;
    newnode->next=current->next;
    current->next=newnode;
}

void insertEnd(struct node **head, int d)
{
    printf("\ninsertEnd called.\n");


        //if link is empty then insert as first element
        if(*head==NULL)//*head == NULL ie start==null
        {
            struct node *newnode=(struct node *)malloc(sizeof(struct node));
            newnode->data=d;
            newnode->next=*head;
            *head=newnode;

        }
        //if more the 0 element then insert at last by traversing till end
        else
        {
            struct node *current=*head;
            while(current->next!=NULL)//next is must
            {
                current=current->next;
            }
        //insert at last
        struct node *newnode=(struct node *)malloc(sizeof(struct node));
        newnode->data=d;
        newnode->next=NULL;
        current->next=newnode;
        }
}

void display(struct node *head)
{
    printf("display called:\n");
    struct node  *current=head;
    while(current!=NULL)
    {
        printf("%d \n",current->data);
        current=current->next;
    }
}
void count(struct node *head)
{
    int count=0;
    printf("count called:\n");
    struct node  *current=head;
    while(current!=NULL)
    {
        count++;
        current=current->next;
    }
    printf("Number of nodes=%d\n\n",count);
}
void reverse(struct node **head)
{
    printf("reverse called:\n");
    struct node *current=*head;//*head=start
    struct node *next=NULL;
    struct node *result=NULL;
    while(current!=NULL)
    {
        next=current->next;
        current->next=result;
        result=current;
        current=next;
    }
    *head=result;
}

void deleteBeginning(struct node **head)
{
    printf("\ndeleteBeginning called.\n");
    struct node *temp=*head;
    *head=(*head)->next;
    free(temp);
}

void deleteKey(struct node **head)
{
    printf("\ndeleteKey Called\n");

    int key;
    printf("Enter value to delete:\n");
    scanf("%d",&key);

    struct node *previous;
    struct node *temp=*head;//since *head is start
    while(temp!=NULL)
    {
        //if key==first node
        if(temp->data==key)
        {
            if(temp==*head)//= always first elt deleted
            {
                *head=temp->next;
                free(temp);
                return;
            }
            else
            {
                previous->next=temp->next;//deletes intermediate node
                free(temp);
                return;
            }
        }

        //other node==key
        else
        {
            previous=temp;
            temp=temp->next;

        }
    }//while ends
    printf("key doesnot exists.\n");
    //return;
}

/*
Inserting 10,20,15,77,101,202

inserBegin called.

inserBegin called.

inserBegin called.

inserBegin called.

inserBegin called.

inserBegin called.
display called:
202
101
77
15
20
10

insertPosition called.
display called:
202
101
333
77
15
20
10

count called.
Number of Nodes=6;

deleteBeginning called.
display called:
101
333
77
15
20
10
99

reverse called:
display called:
99
10
20
15
77
333
101

deleteKey Called
Enter value to delete:
333
display called:
99
10
20
15
77
101
 */

No comments:

Post a Comment