Linked list all operations without head pass in c

/*   code for single linked list operations:
   1.insert at Begin
   2.insert at Last//append
   3.Insert at Given Position
   4.Display
   5.reverse
   6.delete at beginning
   7.delete given node
 */
#include<stdio.h>
#include<stdlib.h>

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

void insertAtBegin(int d);
void insertAtLast(int d);
void insertAtPosition(int d,int p);
void display();
void reverse();
void deleteAtBegin();
void deleteKey();
void count();



int main()
{
    printf("\ninserting 10 20  30  40 \n");
    start=NULL;//initially ll is empty

    insertAtBegin(10);
    insertAtBegin(20);
    insertAtBegin(30);
    insertAtBegin(40);
    display();

    count();

    insertAtLast(200);
    insertAtLast(300);
    display();

    insertAtPosition(555,3);
    display();

    reverse();
    display();

    deleteAtBegin();
    display();

    deleteKey();
    display();

    printf("\n\n");

    return 0;
}

void insertAtBegin(int d)
{
    printf("insertAtBegin called:\n");
    //creating a node to insertAtBeginy
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=d;
    newnode->next=start;
    start=newnode;
}

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

void deleteAtBegin()
{
    printf("deleteAtBegin called:\n");
    struct node *current=start;
    start=start->next;
    free(current);
}

void insertAtLast(int d)
{
    printf("insertAtLast called:\n");
    /*insert as first element in empty list*/
    if(start==NULL)//if first is pointing to null ie list is emty
    {
        struct node *newnode=(struct node *)malloc(sizeof(struct node));
        newnode->data=d;
        newnode->next=start;
        start=newnode;
    }
    /*insert as last element iby traversing till end*/
    else
    {
        struct node *current=start;
        while(current->next!=NULL)//USE of next mind it
        {
            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 insertAtPosition(int d,int p)
{
    printf("\ninsertAtPosition called.\n");
    //traverse till the position then insert
    int i;
    struct node *current=start;
    for(i=0;i<p;i++)
    {
        current=current->next;
    }
    if(current==NULL)//end of list
    {
        printf("Enter less number of position.");
        //return;
    }
    else
    {
        struct node *newnode=(struct node *)malloc(sizeof(struct node));
        newnode->data=d;
        newnode->next=current->next;
        current->next=newnode;
    }

    printf("\n\n");
}
void deleteKey()
{
    printf("deleteKey Called:\n\n");

    int key;
    scanf("%d",&key);

    struct node *previous;
    struct node *temp=start;
    while(temp!=NULL)
    {
        //if key==first node
        if(temp->data==key)
        {
            if(temp==start)//= always first elt deleted
            {
                start=temp->next;
                free(temp);
                return;//otherwise last printf always gets printed
            }
            else
            {
                previous->next=temp->next;//deletes intermediate node
                free(temp);
                return;//otherwise last printf always gets printed
            }
        }

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

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

/*
inserting 10 20  30  40
insertAtBegin called:
insertAtBegin called:
insertAtBegin called:
insertAtBegin called:
display called:
40
30
20
10
count called:
Number of nodes=4

insertAtLast called:
insertAtLast called:
display called:
40
30
20
10
200
300

insertAtPosition called.
display called:
40
30
20
10
555
200
300
reverse called:
display called:
300
200
555
10
20
30
40
deleteAtBegin called:
display called:
200
555
10
20
30
40
deleteKey Called:
20
display called:
200
555
10
30
40
*/

No comments:

Post a Comment