Linked list

#include<stdio.h>
struct node
{
        int data;
        struct node* next;
};
void addatbeg(struct node**,int);
void append(struct node**,int);
void display(struct node*);
void delete(struct node**,int num);
void count(struct node*);
void insert(struct node**,int,int);
void reverse(struct node**);
void reversing(struct node*);
void mid(struct node*);
main()
{
        struct node *head;
        head = NULL;
        addatbeg(&head,10);
        addatbeg(&head,20);
        addatbeg(&head,30);
        display(head);
        append(&head,60);
        append(&head,70);
        printf("\n");
        display(head);
        delete(&head,60);
        printf("\n");
        printf("after deletion the linked list be a\n");
        display(head);
        printf("\n");
        printf("the number of nodes will be\n");
        count(head);
        insert(&head,333,20);
        printf("\n");
        display(head);
        reverse(&head);
        printf("\n");
        printf("this is reverse the nodes\n");
        display(head);
        reversing(head);
        printf("\n");
        printf("this is reverse printing\n");
        display(head);
        printf("\n");
        printf("the mid element is\n");
        mid(head);


}
void addatbeg(struct node **q,int num)
{
        struct node *temp;
        temp = (struct node*)malloc(sizeof(struct node));
        temp->data = num;
        temp->next = *q;
        *q = temp;
}
void display(struct node *q)
{
        while(q!=NULL)
        {
                printf("%d  ",q->data);
                q = q->next;
        }
}
void append(struct node**q,int num)
{
        struct node *temp,*r;
        if(q==NULL)
        {
                temp = (struct node*)malloc(sizeof(struct node));
                temp->data = num;
                temp->next = *q;
                *q = temp;
        }
        else
        {
                temp = *q;
                while(temp->next!=NULL)
                        temp = temp->next;
                r = (struct node*)malloc(sizeof(struct node));
                r->data = num;
                r->next = NULL;
                temp->next = r;
        }
}
void delete(struct node **q,int num)
{
        struct node* temp,*old;
        temp = *q;
        while(temp!=NULL)
        {
                if(temp->data == num)
                {
                        if(temp == *q)
                        {
                                *q = NULL;
                                free(temp);
                        }
                        else
                      {
                                old->next = temp->next;
                                free(temp);
                        }
                }
                else
                {



                        old = temp;
                        temp = temp->next;
                }


        }
}
void count(struct node*q)
{
        int c=0;
        while(q!=NULL)
        {
        c++;
        q = q->next;
        }
        printf("the nuber of nodes = %d\n",c);

}
void insert(struct node**q,int num,int n)
{
        struct node *temp,*r;
        temp = *q;
        while(temp!=NULL)
        {
               if(temp->data == n)
                {
                        r = (struct node*)malloc(sizeof(struct node));
                        r->data = num;
                        r->next = temp->next;
                        temp->next = r;
                }
                        temp = temp->next;
        }
}
void reverse(struct node**q)
{
        struct node *temp,*prev=NULL,*rev;
        temp = *q;
        while(temp!=NULL)
        {
        rev = temp;
        temp = temp->next;
        rev->next = prev;
        prev = rev;
        }
        *q = rev;
}
void reversing(struct node*q)
{
        if(q==NULL)
        {
        reversing(q->next);
        printf("%d ",q->data);
        }
}
void mid(struct node*q)
{
        int m=0;
        if(q->next==NULL)
        {
        printf("only one node is there\n");
        }
        if(q->next->next==NULL)
        {
        printf(" two nodes is there\n");
        }
        while( (q->next!=NULL) && (q->next->next!=NULL))
        {
                m++;
                q = q->next;
        }
        printf("%d\n",m);
}

-------------------------------------------------------------------------------------------------------------------------------------------------

30  20  10
30  20  10  60  70
after deletion the linked list be a
30  20  10  70
the number of nodes will be
the nuber of nodes = 4

30  20  333  10  70
this is reverse the nodes
70  10  333  20  30
this is reverse printing
70  10  333  20  30
the mid element is
3
 

Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: