Linked List Operations in C

Code for single linked list
   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
*/

/*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
 */
\File name ==ll_reverse.c==
//linked list implementation in C
//insert at beginning,delete at beginning
//display, reverse

#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void deletee();
void reverse();
struct node
{
    int data;
    struct node *next;
};
struct node *start;

int main()
{
    printf("\n\n");
    start=NULL;//starting with empty list
    insert(10);
    insert(10);
    insert(15);
    insert(10);
    insert(11);
    insert(15);
    insert(99);
    printf("\ndisplay() called:\n");
    display();

    printf("\ndeletee() called:\n");
    deletee();
    display();

    printf("\nreverse() called:\n");
    reverse();
    display();
    printf("\n\n");
    return 0;
}

void insert(int d)
{
    struct node *newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=d;
    newnode->next=start;
    start=newnode;
}
void display()
{
    struct node *ptr=start;
    while(ptr!=NULL)
    {
        printf("%d ",ptr->data);
        ptr=ptr->next;
    }
}
void deletee()
{
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    temp=start;
    start=start->next;
    if(temp!=NULL)
    {
        free(temp);
    }
}

void reverse()
{
    struct node *next=(struct node*)malloc(sizeof(struct node));
    struct node *result=(struct node*)malloc(sizeof(struct node));
    struct node *current=(struct node*)malloc(sizeof(struct node));
    next=NULL;
    result=NULL;
    current=start;
    while(current!=NULL)
    {
        next=current->next;
        current->next=result;
        result=current;
        current=next;
    }
    start=result;
}

/*
   display() called:
   99 15 11 10 15 10 10
   deletee() called:
   15 11 10 15 10 10
   reverse() called:
   10 10 15 10 11 15
   */


//sorted linked list latest
//#include<iostream.h>
//using namespace std;

#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;

void insert(int d)
{
node *newnode=(node *)malloc(sizeof( node));
newnode->data=d;
newnode->next=NULL;
//insert as first elt
if(start==NULL)
{

//start->next=newnode;
start=newnode;
}
//insert before first elt
if(newnode->data < start->data)
{
newnode->next=start;
start=newnode;
return;
}
//inserting at middle
node* prev; node *ptr;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(newnode->data < ptr->data)
{
prev->next=newnode;
newnode->next=ptr;
return;
}
}
//inserting at last
if(ptr==NULL)
{
prev->next=newnode;
newnode->next=NULL;//must
return;
}

}


void display()
{
node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;//must else infinite loop
}
printf("\n");
}

void deleteNode()
{
int key;
printf("Enter elt to delete:\n");
scanf("%d",&key);
//deleting the first node
if(key==start->data)
{
node *temp;
temp=start;
start=start->next;
free(temp);
return;
}
//deleting middle node
node *ptr; node *prev;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(ptr->data==key)
{
prev->next=ptr->next;
free( ptr);
return;
}
}
//deleteing last elt means data not present
if(ptr==NULL)
{
printf("Ket not present in the LL.\n");
return;
}
if(start==NULL)
{
printf("No elt to del in the LL.\n");
return;
}
}


int main()
{
insert(10);
insert(4);
insert(8);
insert(9);
insert(6);
insert(7);
insert(25);
display();
deleteNode();
display();
deleteNode();
display();
deleteNode();
display();
return 0;
}
/*
4 6 7 8 9 10 25
Enter elt to delete:
7
4 6 8 9 10 25
Enter elt to delete:
4
6 8 9 10 25
Enter elt to delete:
25
6 8 9 10
*/

//linked list implementation
//inserting at first,last and given position
//delete at given position
//display
//update given value with new value
//traverse reverse
//search
#include <stdio.h>
#include <malloc.h>

struct node
{
    int value;
    struct node *next;
};

typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void display();
void delete_pos();
void search_val();
void update_val();
void rev_display();
int main()
{
display();
//insert_node_first();
//display();
insert_node_first();
display();
insert_node_first();
display();
insert_node_last();
display();
insert_node_last();
display();
insert_node_first();
display();
insert_node_pos();
display();

delete_pos();
display();

delete_pos();
display();

update_val();
//display();

//search_val();
//display();

//rev_display();

printf("\n");
return 0;
}



/*
 * Creating Node
 */
snode* create_node(int val)
{
    newnode = (snode *)malloc(sizeof(snode));
    if (newnode == NULL)
    {
        printf("\nMemory was not allocated\n");
        return 0;
    }
    else
    {
        newnode->value = val;
        newnode->next = NULL;
        return newnode;
    }
}

/*
 * Inserting Node at First
 */
void insert_node_first()
{
    int val;

    printf("\nEnter the value for FIRST node:\n");
    scanf("%d", &val);
    newnode = create_node(val);
    if (first == last && first == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {

        newnode->next = first;
        first = newnode;
    }
    printf("\n----INSERTED----\n");  
}

/*
 * Inserting Node at Last
 */
void insert_node_last()
{
    int val;

    printf("\nEnter the value for the LAST Node:\n");  
    scanf("%d", &val);
    newnode = create_node(val);
    if (first ==NULL && last == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {
printf("value od last=%d\n",last);
printf("value od *last=%d\n",*last);
        last->next = newnode;
        last = newnode;
        last->next = NULL;
    }
 printf("\n----INSERTED----\n");
}  

/*
 * Inserting Node at position
 */
void insert_node_pos()
{
    int pos, val, cnt = 0, i;

    printf("\nEnter the value for the Node:\n");
    scanf("%d", &val);
    newnode = create_node(val);
     printf("\nEnter the position:\n ");
    scanf("%d", &pos);
    ptr = first;
    while (ptr != NULL)
    {
        ptr = ptr->next;
        cnt++;
    }
    if (pos == 1)
    {
        if (first == last && first == NULL)
        {
            first = last = newnode;
            first->next = NULL;
            last->next = NULL;
        }
        else
        {
newnode->next=first;
first=newnode;
        }
        printf("\nInserted\n");
    }
    else if (pos>1 && pos<=cnt)
    {
        ptr = first;
        for (i = 1;i < pos;i++)
        {
            prev = ptr;
            ptr = ptr->next;
        }
        prev->next = newnode;
        newnode->next = ptr;
        printf("\n----INSERTED----\n");
    }
    else
    {
        printf("\nPosition is out of range\n");
    }
}

/*
 * Displays non-empty List from Beginning to End
 */
 void display()
{
    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list to display\n");
    }
    else
    {
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {  
            printf("%d\t", ptr->value);
        }
    }
}


void delete_pos()
{
    int pos, cnt = 0, i;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo node to delete\n");
    }
    else
    {
        printf("\nEnter the position of value to be deleted:\n");
        scanf(" %d", &pos);
        ptr = first;
        if (pos == 1)
        {
            first = first->next;
            printf("\nElement deleted:\n");  
        }
        else
        {
            while (ptr != NULL)
            {
                ptr = ptr->next;
                cnt = cnt + 1;
            }
            if (pos > 0 && pos <= cnt)  
            {
                ptr = first;
                for (i = 1;i < pos;i++)
                {
                    prev = ptr;
                    ptr = ptr->next;
                }
                prev->next = ptr->next;
            }
            else
            {
                printf("\nPosition is out of range\n");
            }
        free(ptr);
        printf("\nElement deleted\n");
        }
    }
}
/*
 * Updating Node value in a non-empty list
 */
void update_val()
{
    int oldval, newval, flag = 0;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list to update\n");
    }
    else
    {
        printf("\nEnter the value to be updated:\n");
        scanf("%d", &oldval);
        printf("\nEnter the newvalue:\n");  
        scanf("%d", &newval);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            if (ptr->value == oldval)
            {
                ptr->value = newval;
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nUpdated Successfully\n");
        }
        else
        {
            printf("\nValue not found in List\n");
        }
    }  
}


/*
 * searching an element in a non-empty list
 */
void search_val()
{
    int flag = 0, key, pos = 0;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list\n");
    }
    else
    {
        printf("\nEnter the value to search\n");
        scanf("%d", &key);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            pos = pos + 1;
            if (ptr->value == key)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nElement %d found at %d position\n", key, pos);
        }
        else
        {
            printf("\nElement %d not found in list\n", key);
        }
    }  
}

/*
 * Display non-empty list in Reverse Order
 */
void rev_display(snode *ptr)
{
    int val;

    if (ptr == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes to display\n");
    }
    else
    {
        if (ptr != NULL)
        {
            val = ptr->value;
            rev_display(ptr->next);
            printf("%d\t", val);      
        }

    }
}

/*
EMPTY LIST:

No nodes in the list to display

Enter the value for FIRST node:
1

----INSERTED----
1
Enter the value for FIRST node:
2

----INSERTED----
2       1
Enter the value for the LAST Node:
3
value od last=158580744
value od *last=1

----INSERTED----
2       1       3
Enter the value for the LAST Node:
4
value od last=158580776
value od *last=3

----INSERTED----
2       1       3       4
Enter the value for FIRST node:
11

----INSERTED----
11      2       1       3       4
Enter the value for the Node:
22

Enter the position:
 2

----INSERTED----
11      22      2       1       3       4
Enter the position of value to be deleted:
33

Position is out of range

Element deleted
11      22      2       1       3       4
Enter the position of value to be deleted:
3

Element deleted
11      22      1       3       4
Enter the value to be updated:
22

Enter the newvalue:
20

Updated Successfully
*/

//finding the merge point of two lists
//list1 contains 10,15,30
//list 2 contains 3,6,9,15,30 such that
//15 in both the lists are stored in same location ie merge point
/*ALGO:
  -count the list1 say c1
  -count the list2 say c2
  -absoulte difference say d
  -traverse the longer list by d nodes
  -then traverse both the lists simueltaeously
  -when two nodes gets equal in address that is point of merge
 */
#include<stdio.h>
#include<stdlib.h>

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


void display(struct node *head1);
int countNode(struct node *head1);
int mergePoint(struct node *head1,struct node *head2,int d);
int main()
{
    struct node *head1=(struct node *)malloc(sizeof(struct node));
    head1->data=10;
    head1->next=NULL;

    struct node *head2=(struct node *)malloc(sizeof(struct node));
    head2->data=3;
    head2->next=NULL;

    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=6;
    head2->next=newnode;

    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=9;
    head2->next->next=newnode;

    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=15;
    head2->next->next->next=newnode;
    head1->next=newnode;
    printf("head2->next->next->next=newnode=%p\n",head2->next->next->next);
    printf("head1->next=%p\n",head1->next);

    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=30;
    head1->next->next=newnode;
    //head2->next->next->next->next=newnode;
    head1->next->next->next=NULL;

    display(head1);
    printf("\n");
    display(head2);
    printf("\n");
    int c1,c2,d;
    c1=countNode(head1);
    printf("\n");
    c2=countNode(head2);
    printf("\n");
    if(c1>c2)
    {
        d=c1-c2;
        printf("d=c1=%d\n",d);
    }
    else
    {
        d=c2-c1;
        printf("d=c2=%d\n",d);
    }
    int t=0;
    t=mergePoint(head1,head2,d);
    printf("Merege point is at node:%d\n",t);
    return 0;  
}
//function to display the nodes
void display(struct node *h)
{
    struct node *current=h;
    while(current!=NULL)
    {
        printf("%d ",current->data);
        current=current->next;
    }

}
//function to count the nodes
int countNode(struct node *h)
{
    int count=0;
    struct  node *current=h;
    while(current!=NULL)
    {
        count=count+1;
        current=current->next;
    }
    printf("size of list1=%d\n",count);

    return count;
}
//function to give the merge point
int mergePoint(struct node *head1,struct node *head2,int d)
{
    int i;
    struct node *current1=head1;  
    struct node *current2=head2;
    for(i=0;i<d;i++)
    {
        printf("i=%d,current2->data=%d\n",i,current2->data);
        current2=current2->next;
    }
    while(current1!=NULL && current2!=NULL)
    {
        printf("while=\n");
        if(current1==current2)
        {
            printf("\n\ncurrent1->data====%d\n\n",current1->data);
            return current1->data;
        }
        current1=current1->next;
        current2=current2->next;

    }

}

/*OUTPUT:
head2->next->next->next=newnode=0x9dc7048
head1->next=0x9dc7048
10 15 30
3 6 9 15 30
size of list1=3

size of list1=5

d=c2=2
i=0,current2->data=3
i=1,current2->data=6
while=
while=

current->data====15

Merege point is at node:15
*/

//linked list implementation
//inserting at first,last and given position
//delete at given position
//display
//update given value with new value
//traverse reverse
//search
#include <stdio.h>
#include <malloc.h>

struct node
{
    int value;
    struct node *next;
};

typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void display();
void delete_pos();
void search_val();
void update_val();
void rev_display();


int main()
{
display();
//insert_node_first();
//display();
insert_node_first();
display();
insert_node_first();
display();
//insert_node_last();
//display();
insert_node_last();
display();
insert_node_first();
display();
insert_node_pos();
display();

delete_pos();
display();

delete_pos();
display();

update_val();
//display();

//search_val();
//display();

//rev_display();

printf("\n");
return 0;
}



/*
 * Creating Node
 */
snode* create_node(int val)
{
    newnode = (snode *)malloc(sizeof(snode));
    if (newnode == NULL)
    {
        printf("\nMemory was not allocated\n");
        return 0;
    }
    else
    {
        newnode->value = val;
        newnode->next = NULL;
        return newnode;
    }
}

/*
 * Inserting Node at First
 */
void insert_node_first()
{
    int val;

    printf("\nEnter the value for FIRST node:\n");
    scanf("%d", &val);
    newnode = create_node(val);
    if (first == last && first == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {
        temp = first;
        first = newnode;
        first->next = temp;
    }
    printf("\n----INSERTED----\n");  
}

/*
 * Inserting Node at Last
 */
void insert_node_last()
{
    int val;

    printf("\nEnter the value for the LAST Node:\n");  
    scanf("%d", &val);
    newnode = create_node(val);
    if (first == last && last == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {
        last->next = newnode;
        last = newnode;
        last->next = NULL;
    }
 printf("\n----INSERTED----\n");
}  

/*
 * Inserting Node at position
 */
void insert_node_pos()
{
    int pos, val, cnt = 0, i;

    printf("\nEnter the value for the Node:\n");
    scanf("%d", &val);
    newnode = create_node(val);
     printf("\nEnter the position:\n ");
    scanf("%d", &pos);
    ptr = first;
    while (ptr != NULL)
    {
        ptr = ptr->next;
        cnt++;
    }
    if (pos == 1)
    {
        if (first == last && first == NULL)
        {
            first = last = newnode;
            first->next = NULL;
            last->next = NULL;
        }
        else
        {
            temp = first;
            first = newnode;
            first->next = temp;
        }
        printf("\nInserted\n");
    }
    else if (pos>1 && pos<=cnt)
    {
        ptr = first;
        for (i = 1;i < pos;i++)
        {
            prev = ptr;
            ptr = ptr->next;
        }
        prev->next = newnode;
        newnode->next = ptr;
        printf("\n----INSERTED----\n");
    }
    else
    {
        printf("\nPosition is out of range\n");
    }
}

/*
 * Displays non-empty List from Beginning to End
 */
 void display()
{
    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list to display\n");
    }
    else
    {
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {  
            printf("%d\t", ptr->value);
        }
    }
}


void delete_pos()
{
    int pos, cnt = 0, i;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo node to delete\n");
    }
    else
    {
        printf("\nEnter the position of value to be deleted:\n");
        scanf(" %d", &pos);
        ptr = first;
        if (pos == 1)
        {
            first = ptr->next;
            printf("\nElement deleted:\n");  
        }
        else
        {
            while (ptr != NULL)
            {
                ptr = ptr->next;
                cnt = cnt + 1;
            }
            if (pos > 0 && pos <= cnt)  
            {
                ptr = first;
                for (i = 1;i < pos;i++)
                {
                    prev = ptr;
                    ptr = ptr->next;
                }
                prev->next = ptr->next;
            }
            else
            {
                printf("\nPosition is out of range\n");
            }
        free(ptr);
        printf("\nElement deleted\n");
        }
    }
}
/*
 * Updating Node value in a non-empty list
 */
void update_val()
{
    int oldval, newval, flag = 0;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list to update\n");
    }
    else
    {
        printf("\nEnter the value to be updated:\n");
        scanf("%d", &oldval);
        printf("\nEnter the newvalue:\n");  
        scanf("%d", &newval);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            if (ptr->value == oldval)
            {
                ptr->value = newval;
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nUpdated Successfully\n");
        }
        else
        {
            printf("\nValue not found in List\n");
        }
    }  
}


/*
 * searching an element in a non-empty list
 */
void search_val()
{
    int flag = 0, key, pos = 0;

    if (first == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes in the list\n");
    }
    else
    {
        printf("\nEnter the value to search\n");
        scanf("%d", &key);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            pos = pos + 1;
            if (ptr->value == key)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nElement %d found at %d position\n", key, pos);
        }
        else
        {
            printf("\nElement %d not found in list\n", key);
        }
    }  
}

/*
 * Display non-empty list in Reverse Order
 */
void rev_display(snode *ptr)
{
    int val;

    if (ptr == NULL)
    {
  printf("\nEMPTY LIST:\n");
        printf("\nNo nodes to display\n");
    }
    else
    {
        if (ptr != NULL)
        {
            val = ptr->value;
            rev_display(ptr->next);
            printf("%d\t", val);      
        }

    }
}


//number to string

#include<stdio.h>
int main()
{
    char result[50];
    float num = 23.34;
    sprintf(result, "%f", num);
    printf("\n The string for the num is %s", result);
    getchar();
}

//toggling
/* 9. program for toggling  */

#include<stdio.h>

void toggle(int a, int pos, int n)
{
int i,t=1;
t=t<<pos-1;
for(i=0;i<=pos;i++)
{
a=a^t;
t=t+(pos+i);//left side

//t=pos+i;//right side
}
printf("After Toggling %d\n",a);
}
void main()
{
int a,pos,n;
printf("Please enter the no, pos and no of bits:");
scanf("%d %d %d",&a,&pos,&n);
toggle(a,pos,n);
}

No comments:

Post a Comment