Wednesday, August 17, 2011

QUES 28 ASCENDING ORDER LINKED LIST


C LANGUAGE BOOK





ASCENDING ORDER LINKED LIST

 addition
addition operation should be done so that
data is in sorted in ascending order.

 if  list is empty.
 create a temporary node assign data.
set its next pointer of temporary node to point  to Null

set the head pointer of list to point to temporary node.


if list has more than one node
count the node.

use iteration and
compare the data to be inserted with node data.during iteration
and insert at specified position so that list is in sorted order.


deletion

same linked list deletion operation.


/*WAP to create a ascending order linked list */

#include<alloc.h>
#include<stdio.h>

struct me
{
int data;
struct me *link;
};

void main()
{

 struct me *p;
 int j,m,n;

 do
 {
 printf("\n\tLINKED LIST");
 printf("\n\t1.INSERTION");
 printf("\n\t2.DELETION");
 printf("\n\t3.DISPLAY");
 printf("\n\t4.EXIT\n");
 printf("\n\tWHAT DO YOU WANT TO DO (PRESS 1 TO 4): ");
 scanf("%d", &j);
 printf("\n");
 switch(j)
 {
  case 1:   Insertion(&p);
        break;
  case 2:   Deletion(&p);
        break;
  case 3:   Display(p);
        printf("\n");
        break;
  case 4:   exit(1);
default :  err_msg(3);

 }
}while(j>=1 && j<=4);
}

Insertion(struct me **q)
{
 int i, m;
 char ch[15];
 do
  {
        printf("\n\tINSERTION");
        printf("\n\t1.INSERT THE NUMBER");
        printf("\n\t2.RETURN\n");
        printf("\n\tWHAT DO YOU WANT TO DO (PRESS 1 TO 4): ");
        scanf("%d", &i);
        printf("\n");
        switch(i)
        {
        case 1: printf("\n\tENTER THE NUMBER : ");
            scanf("%d", &m);
            Add(&(*q),m);
            printf("\n");
            break;
        case 2: return;
      default : err_msg(3);
           }
     }while(i>=1 && i<=2);

}

Deletion(struct me **q)
{
 int i,m,n;
 do
 {
        printf("\n\tDELETION");
        printf("\n\t1.DELETION AT BEGINNING");
        printf("\n\t2.DELETION AT POSITION");
        printf("\n\t3.DELETION AT END");
        printf("\n\t4.RETURN\n");
        printf("\n\tWHAT DO YOU WANT TO DO (PRESS 1 TO 4): ");
        scanf("%d", &i);
        printf("\n");
        switch(i)
        {
        case 1: DeleteAtBeg(&(*q));
            printf("\n");
            break;
        case 2: printf("\tENTER THE POSITION : ");
            scanf("%d", &n);
            DeleteAtPos(*q ,n);
            printf("\n");
            break;
        case 3: DeleteAtEnd(&(*q));
            printf("\n");
            break;
        case 4: return;
      default : err_msg(3);

        }
       }while(i>=1 && i<=4);
}

Add(struct me **q, int x)
{

struct me *p,*w,*z,*y;
int c = 0,i,n = 0;

p = *q;
z = *q;

for(y = *q ; y != NULL; y = y->link)
{
  n++;
}

printf("\n\tTOTAL NODES : %d", n);

for(i = 0; i < n ; i++)
{
 if(p->data < x)
 {
 p = p->link;
 c++;
 }
}

printf("\n\tPOSITION FOR THIS NUMBER TO BE INSERTED IS : %d" ,c);

if(*q == NULL)
{
w = malloc(sizeof(struct me));
w->data = x;
w->link = *q;
*q = w;
}
else
if(*q != NULL)
{
    if((*q)->data < x)
    {
     for(i = 1 ; i < c ; i++)
     {
        z = z->link;
     }
     w = malloc(sizeof(struct me));
     w->data = x;
     w->link = z->link;
     z->link = w;
    return;
    }
    else
    {
    w = malloc(sizeof(struct me));
    w->data = x;
    w->link = *q;
    *q = w;
    return;
    }
}
}

DeleteAtBeg(struct me **q)
{
struct me *p,*w;
p = *q;
if(p == NULL)
{
  err_msg(1);
}
else
{
 if(p->link == NULL)
 {
  printf("\tTHE LIST");
  *q = NULL;
  free(p);
 }
 else
 {
 w = p;
 *q = w->link;
 free(w);
 }

}
return;
}

DeleteAtPos(struct me *q,int x)
{
struct me *p,*w, *z;
int i;
p = q;
w = q;

if(p == NULL)
{
  err_msg(4);
}
else
{
for(i = 0; i < x; i++)
{
 w = w->link;
if(w == NULL)
{
printf("\tCANNOT DELETE THERE ARE LESS THAN %d \n\tELEMENTS IN THE LIST", x);
return;
}
}
z = w->link;
w->link = z->link;
free(z);
return;
}
}

DeleteAtEnd(struct me **q)
{
struct me *p,*w,*z;
p = *q;
w = *q;
if(p == NULL)
{
  err_msg(1);
  }
else
if(p->link == NULL)
{
  *q = NULL;
  free(p);
}
else
{
while(p->link->link != NULL)
{
p = p->link;
}

z = p->link;
p->link = p->link->link;
free(z);
}
return;
}

Display(struct me *q)
{
struct me *z;
z = q;
if(z == NULL)
{
err_msg(1);
return;
}
else
{
while(z != NULL)
{
 printf("\t%d", z->data);
  z = z->link;
 printf("\n");
 }
return;
}
}

err_msg(int p)
{
if(p==1)
printf("\tTHE LIST IS EMPTY");
if(p==2)
printf("\tHELLO\n");
if(p==3)
printf("\tWRONG CHOICE\n");
if(p==4)
printf("\tDELETION AT POS REQUIRED TWO NODE\t");


}













C LANGUAGE BOOK

No comments:

Post a Comment