Rashed Hasan
Rashed Hasan

Reputation: 3751

display output in `ascending` or `descending` order after storing value using linked list in c

I am storing five different values using from user input. And displaying as it is inserted from the command prompt, now I want to organize the output as ascending or descending order during display. I searched for this, but I am getting more complicated examples. My code is pretty simple. I am learning. Till now, what I did already, if I can implement ascending or descending order in my code, then it will be more help to me for easy understand. Would someone help me please to do this, if possible in my existing code.

#include <stdio.h>

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

int main(){

    struct list list1[5], *c;
    int i = 0;
    for(i = 0; i < 5; i++){
        printf("Enter value of list %d = ", i);
        scanf("%d", &list1[i].value);
    }
    for(i = 0; i < 4; i++){
        list1[i].next = &list1[i+1];
    }
    list1[4].next = NULL;

    c = &list1[0];
    while(c != NULL){
        printf("List value: %d\n", c->value);
        c = c->next;
    }
    return 0;
}

Upvotes: 2

Views: 260

Answers (2)

Papai from BEKOAIL
Papai from BEKOAIL

Reputation: 1539

you can store the data in order instead. can follow this code to store data in sorted order :=

#include<stdio.h>
#include<stdlib.h>

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

struct List * add(struct List * head, int data)
{
  struct List * newnode = (struct List *)malloc(sizeof(struct List));
  newnode->next = NULL;
  newnode->node = data;

 /* if list is empty (initially), put value  */
  if(head == NULL)
  {
     head = newnode;
     return head;
 }    
  /* if head itself smaller than the node to be inserted 
     the sign(<) has to be changed(>) for ascending order.
  */
 else if(head->node > data)
{
   newnode->next = head;
   head = newnode;

   return head;
 }
 struct List * temp = head;
 struct List * current = NULL;

 while(temp->next)
 {
     current = temp;
    /*if node is fitted between current node and next of current node..
        the sign(>=) has to be changed(<=) and the sign (<=) has to be changed                     (>=)for ascending order .
     */
    if(current->node <= data && current->next->node >= data)
    {
        newnode->next = current->next;
        current->next = newnode;
        return head;
    }
    temp = temp->next;
}
temp->next = newnode;

return head;
}

void display(struct List * head)
{
 if(head == NULL)
     return;

  printf("%i ", head->node);

  display(head->next);
 }
 int main(void){
   int arr[] = {3, 0, 1, 4, 2};
   int n = sizeof(arr)/sizeof(1);
   struct List * head = NULL;

  for(int i=0; i<n; i++){
    head = add(head, arr[i]);
  }

  display(head);
  printf("\n");
}

Upvotes: 0

Armali
Armali

Reputation: 19395

… implement ascending or descending order in my code … by changing the next pointers accordingly

You could use a kind of insertion sort to do this by going through your unsorted list and building a new list, reconnecting the next pointers in sorted order:

    struct list *head = NULL, *nc;  // new (initially empty) list starts at head
    for (c = list1; c; c = nc)      // go through original list
    {
        struct list **pred = &head, *d;
        while ((d=*pred) && c->value > d->value) pred = &d->next;   // find pos.
        nc = c->next;               // get next element (before overwritten)
        c->next = d, *pred = c;     // insert element into sorted list
    }

This sorts ascending; for descending order, change > to <.

Upvotes: 1

Related Questions