LINUX.ORG.RU

История изменений

Исправление bormant, (текущая версия) :

Вероятно, имелось в виду что-то подобное:

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

struct data {
  int a;
};

struct node {
  struct node *next;
  struct data d;
};

struct node * 
newnode (int x, struct node *n) {
  struct node *p = malloc(sizeof(struct node));
  if (p) {
    p->d.a=x;
    p->next=n;
  }
  return p;
}

void 
insert(struct node **lst, int x) {
  if (!lst) return;
  struct node **p;
  p=lst;
  while (*p && (*p)->d.a<x) p=&((*p)->next);
  (*p)=newnode(x,*p);
}

void 
print(struct node *lst) {
  while (lst) {
    printf("%d ",lst->d.a);
    lst=lst->next;
  }
}

int main(int argc, char **argv) {
  struct node *lst = NULL;
  for (int i=5; i>0; i--) insert(&lst,i);
  print(lst); printf("\n");
  return 0;
}

Исходная версия bormant, :

Имелось в виду что-то подобное:

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

struct data {
  int a;
};

struct node {
  struct node *next;
  struct data d;
};

struct node * 
newnode (int x, struct node *n) {
  struct node *p = malloc(sizeof(struct node));
  if (p) {
    p->d.a=x;
    p->next=n;
  }
  return p;
}

void 
insert(struct node **lst, int x) {
  if (!lst) return;
  struct node **p;
  p=lst;
  while (*p && (*p)->d.a<x) p=&((*p)->next);
  (*p)=newnode(x,*p);
}

void 
print(struct node *lst) {
  while (lst) {
    printf("%d ",lst->d.a);
    lst=lst->next;
  }
}

int main(int argc, char **argv) {
  struct node *lst = NULL;
  for (int i=5; i>0; i--) insert(&lst,i);
  print(lst); printf("\n");
  return 0;
}