Skip to main content

PROGRAMME FOR LINK LIST

WRITE A C PROGRAMME ON LINK LIST WHICH HAS FOLLOWING  FUNCTIONALITY :-

**********MENU***********
1-ADD NEW NODE AT BEGINNING OF LINK LIST
2)-ADD NEW NODE AT END OF LINK LIST
3)-ADD NEW NODE AT Nth POSITION  FROM STARTING
4)-REMOVE LAST NODE
5)-REMOVE FIRST NODE
6)-REMOVE Nth POSITION NODE FFROM STARTING
7)-VIEW LINK LIST
8)-TOTAL NO. OF NODES IN LINK LIST
9)-PRINT THE DATA OF LINK LIST IN REVERSE ORDER
10)-REVERSE THE LINK LIST
11)-SORT THE DATA OF LINK LIST
12)-EXIT



SOLUTION:-



#include<stdio.h>

#include<stdlib.h>

int count=0;
struct node
{
    int data;
    struct node *next;
}*head=NULL;
void add_begi()
{
    struct node *t=(struct node *)malloc(sizeof(struct node));
    printf("ENTER THE DATA :\t");
    scanf ("%d",&t->data);
    t->next=NULL;
    t->next=head;
    head=t;
    count++;
}
void add_end()
{
     if (head==NULL)
    {
        printf("\n******EMPTY LINK LIST*******\n");
        return;
    }
    struct node *t,*p;
    p=head;
    t=(struct node *)malloc(sizeof(struct node));
    printf("ENTER THE DATA :-\t");
    scanf ("%d",&t->data);
    t->next=NULL;
    while (p->next!=NULL)
    {
        p=p->next;
    }
    p->next=t;
    count++;
}
void view()
{
    struct node *t;
    t=head;
    if (head==NULL)
    {
        printf("\n******EMPTY LINK LIST*******\n");
        return;
    }
    while (t!=NULL)
    {
        printf ("%d\n",t->data);
        t=t->next;
    }
}
void add_nth()
{
    struct node *p,*x;
    int i,data,position;
    p=head;

    struct node *t=(struct node *)malloc(sizeof(struct node));
    printf ("ENTER THE DATA OF NEW NODE:-\t");
    scanf ("%d",&data);
    printf ("ENTER THE N:_\t");
    scanf ("%d",&position);
    t->data=data;
    t->next=NULL;


     if(position==1)
        {

         t->next=head;
         head=t;
         count++;

        }
     else if (position>count+1)
       {
            printf ("YOU ARE OUT OF LINK LIST.PLEASE CHOOSE VALID N.\n");
       }
    else
    {
        for(i=0;i<position-2;i++)
        {
        p=p->next;
        }
         x=p->next;
        p->next=t;
        t->next=x;
        count++;
    }
}

void del_last()
{
    struct node *t;
    t=head;
    if (head==NULL)
    {
        printf("\n******EMPTY LINK LIST******\n");return;
    }
    if(t->next==NULL)
    {
    printf("\n-> ONLY ONE NODE IS PRESENT IN THE LINK LIST.REMOVING FIRST NODE.........\n");
    head=NULL;
        free(t);
        count--;
    }

    else
    {
    while (t->next->next!=NULL)
    {
        t=t->next;
    }
    free(t->next->next);
    t->next=NULL;
    count--;
    }
}
void del_first()
{
    if (head==NULL)
    {
        printf("\n*****EMPTY LINK LIST******\n");
        return;
    }
    struct node *p;
    p=head;
    head=p->next;
    free(p);
    count--;
}
void del_nth()
{
    if (head==NULL)
    {
        printf("\n******EMPTY LINK LIST******\n");
        return;
    }
    int i,position;
    struct node*p,*q;p=head;
    printf("ENTER THE N:-\t");
    scanf("%d",&position);
    if (position==1)
    {
        head=p->next;
        free(p);
        count--;
    }
    else if (position>count)
    {
        printf("**********YOU ARE GOING TO OUTSIDE THE LINK LIST.ENTER THE VALID POSITION.**********");
    }
    else
    {
    for (i=0;i<position-2;i++)
    {
        p=p->next;

    }
    q=p->next;
    p->next=p->next->next;
    free(q);
    count--;

    }

}
void ReversePrint()
{
    if (head==NULL)
    {
        printf("\n******EMPTY LINK LIST******\n");
        return;
    }
    int count=0;
    struct node *p;
    p=head;
   while (p!=NULL)
   {
     p=p->next;
       count++;

   }
    int data[count],i=0;
    p=head;
    while (p!=NULL)
    {
        data[i]=p->data;
        p=p->next;
        i++;
    }
    for (i=count-1;i>=0;i--)
    {
     printf ("%d\n",data[i]);
    }
}
void Reverse()
{

    int a=0;
    struct node *p,*head1;p=head;
    while (p->next!=NULL)
    {
        p=p->next;
        a++;

    }
  head1=p;
    p=head;
    a++;
    struct node *rev[a] ;
    int i=0;
    while(p!=NULL)
    {
        rev[i]=p;
        p=p->next;
        i++;

    }
    rev[0]->next=NULL;
   for ( i=0;i<a-1;i++)
    {
       rev[i+1]->next=rev[i];
   }
   head=head1;

}

void sort()
{
int i,j,q;
struct node *p;
p=head;
for (i=0;i<count;i++)
{
    for (j=0;j<count-1;j++)
    {
        if(p->data > p->next->data)
        {
        q=p->data;
        p->data=p->next->data;
        p->next->data=q;


        }
         p=p->next;
    }
    p=head;
}
}
main()
{
    int choise;
    while(1){
    printf ("\n**********MENU***********\n1)-ADD NEW NODE AT BEGINNING OF LINK LIST\n2)-ADD NEW NODE AT END OF LINK LIST\n3)-ADD NEW NODE AT Nth POSITION OF NODE FROM STARTING\n4)-REMOVE LAST NODE\n5)-REMOVE FIRST NODE\n6)-REMOVE Nth POSITION NODE\n7)-VIEW LINK LIST\n8)-HOW MANY NODES IN LINK LIST\n9)-PRINT THE DATA OF LINK LIST IN REVERSE ORDER\n10)-REVERSE THE LINK LIST\n11)-SORT THE LINK LIST\n12)-EXIT\n");
    scanf("%d",&choise);
    switch (choise)
    {
        case 1:add_begi();break;
        case 2:add_end();break;
        case 3:add_nth();break;
        case 4:del_last();break;
        case 5:del_first();break;
        case 6:del_nth();break;
        case 7: printf ("\n***** DATA OF LINK LIST *****\n");view();break;
        case 8: printf ("TOTAL NODES IN THE LINK LIST IS %d\n",count);break;
        case 9: ReversePrint();
printf("\n********DATA IN REVERSE ORDER
*******\n");
break;
        case 10:Reverse();view();break;
        case 11:sort();printf("\n*******SORTED DATA********\n");view();break;
        case 12: exit(0);
        default:printf("\n**********ENTER THE VALID INPUT*******\n");
    }
}
}

Comments

Popular Posts

Dijkstra's algorithm

Djikstra's algorithm   solves the problem of finding the shortest path from a point in a graph (the  source ) to a destination. It turns out that one can find the shortest paths from a given source to  all  points in a graph in the same time, hence this problem is sometimes called the  single-source shortest paths  problem. This problem is related to the spanning tree one. The graph representing all the paths from one vertex to all the others must be a spanning tree - it must include all vertices. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G = (V,E) where V is a set of vertices and E is a set of edges. Dijkstra's algorithm keeps two sets of vertices: S    the set of vertices whose shortest paths from the source have already been determined  and V-    S :   the remaining v...

Balanced Brackets

Balanced Brackets :- A bracket is considered to be any one of the following characters:  ( ,  ) ,  { ,  } ,  [ , or  ] . Two brackets are considered to be a  matched pair  if the an opening bracket (i.e.,  ( ,  [ , or  { ) occurs to the left of a closing bracket (i.e.,  ) ,  ] , or  } )  of the exact same type . There are three types of matched pairs of brackets:  [] ,  {} , and  () . A matching pair of brackets is  not balanced  if the set of brackets it encloses are not matched. For example,  {[(])} is not balanced because the contents in between  {  and  }  are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket,  ( , and the pair of parentheses encloses a single, unbalanced closing square bracket,  ] . By this logic, we say a sequence of brackets is considered to be  balanced  if the follow...

Kruskal’s Minimum Spanning Tree Algorithm & Programme

PROBLEM :                  Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step. To understand Kruskal's algorithm let us consider the following example − Step 1 - Remove all loops and Parallel Edges Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which has the least cost associated and ove all others. Step 2 - Arrange all edges in their increasing order of weight The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost). Step 3 - Add the edge which has the least weightage Now we start adding edges to the graph beginning from the one which has the least weight. Th...