0

I need to find the maximum number and the minimum number in a circular linked list and i should move the minimum number to the start of the list (before head) and the maximum number to the end of the list (before minimum)

why my code is giving me error in output?

Note: Using doubly linked list is disallowed ,we should do this only with circular linked list.

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

typedef struct node
{
  int num;
  struct node *next;
} NODE;

void init(NODE **h)
{
  (*h) = (NODE*)malloc(sizeof(NODE));
  (*h)->next = *h;
}

NODE* add(NODE* h,NODE* p,int x)
{
  int i;
  NODE *temp;
  temp = (NODE*)malloc(sizeof(NODE));
  temp->num = x;
  temp->next = h;
  if(h->next == h)
  {
    h->next = temp;
    return h;
  }
  else
  {
    temp = p->next;
    p = temp;
    return h;
  }
  return h;
}

NODE* fin(NODE *h,NODE *p)
{
  NODE* ptr,*pmin,*pmax,*temp,*temp2,*mnprev,*mxprev;
  // temp: minimum
  // temp2: maximum
  // pmin: holds the minimum
  // pmax: holds the maximum
  // ptr: o(n) search
  // mnprev: holds the previous node of the minimum
  // mxprev: hold the previous node of the maximum
  mnprev = mxprev = pmin = pmax = h;
  ptr = h->next;
  int mini, maxi;
  mini = h->num;
  maxi = h->num;
  do
  {
    if(ptr->num < mini)
    {
      mini = ptr->num;
      pmin = ptr;
      mnprev->next = pmin;
    }
    if(ptr->num > maxi)
    {
      maxi = ptr->num;
      pmax = ptr;
      mxprev->next = pmax;
    }

    ptr = ptr->next;
  } while(ptr != h);
  temp = pmin;
  temp2 = pmax;
  mnprev->next = pmin->next;
  mxprev->next = pmax->next;
  free(pmin);
  free(pmax);

  temp->next = h;
  temp2->next = temp;

  ptr = temp;
  do {
    printf("%d ",ptr->num);
    ptr = ptr->next;
  } while(ptr != h);

}

int main()
{
  int i,x;
  NODE *lis,*p;
  init(&lis);
  p = lis;
for(i=0;i<7;i++)
  {
    scanf("%d",&x);
add(lis,p,x);
    }
      fin(lis,p);

}
banarun
  • 2,305
  • 2
  • 23
  • 40
  • What do you mean by *"end of the list (before minimum)"*? – NPE Jun 29 '13 at 12:21
  • what is the error that you are getting? – voidMainReturn Jun 29 '13 at 12:27
  • @NPE after you find minimum and move it before head .. you should now move maximum at the end ( before head) remember thats its a circular linked list .. – Waseem Gabour Jun 29 '13 at 12:48
  • @WaseemGabour Why don't you tell us what 'bad output', we can't guess. Also don't expect people to fix your problems by just posting a long piece of code without details of the problem. – meaning-matters Jun 29 '13 at 13:58
  • `temp = p->next;` at function `add`, rewrite malloced and setuped temp. – BLUEPIXY Jun 29 '13 at 14:26
  • Few of the novice mistakes can be easily seen like the return type of `fin()` is struct pointer and you are returning nothing.Casting malloc ain't a good idea see here :http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc........ not using variable `i` will generate a warning ,etc. – 0decimal0 Jun 29 '13 at 14:58

1 Answers1

1

rewrite your code

  • function init delete as it will create nodes that are not used.
  • Changed to the node between the maximum and minimum values ​​since it is sufficient to simply replace the value.

like this

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

typedef struct node {
    int num;
    struct node *next;
} NODE;

NODE* add(NODE **head, NODE **tail, int x){
    NODE *temp;
    temp = (NODE*)malloc(sizeof(NODE));
    temp->num = x;
    if(*head == NULL){
        temp->next = temp;
        *tail = *head = temp;
    } else {
        temp->next = *head;
        (*tail)->next = temp;
        *tail = temp;
    }
    return *head;
}

void print(NODE *p){
    NODE *top =p;
    do{
        printf("%d ", p->num);
        p=p->next;
    }while(p != top);
    printf("\n");
}

void drop(NODE *head, NODE *tail){
    NODE *p = head;

    tail->next = NULL;
    while(p){
        NODE *temp = p;
        p = p->next;
        free(temp);
    }
}

void minmax(NODE *head, NODE *tail){
    NODE *p, *maxp, *minp;
    int temp;

    maxp = minp = p = head;
    do{
        if(maxp->num < p->num){
            maxp = p;
        }
        if(minp->num > p->num){
            minp = p;
        }
        p = p->next;
    }while(p != head);
    temp = maxp->num; maxp->num = tail->num; tail->num = temp;
    if(tail == minp)//exchanged minp
        minp = maxp;//fixed
    temp = minp->num; minp->num = head->num; head->num = temp;
}

int main(void){
    int i, x;
    NODE *head, *tail;

    tail = head = NULL;
    for(i=0;i<7;i++){
        printf("%d>", i+1);
        scanf("%d", &x);
        add(&head, &tail, x);
    }
    //print(head);
    minmax(head, tail);
    print(head);
    drop(head, tail);

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70