К основному контенту

Связанный список на C, создание и распечатка

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

struct node {
  int id;
  char *name;
  struct node *next;
};

void display(struct node *n);

int main(int argc,char** argv) {

  struct node *root, *next;
  next = malloc(sizeof(struct node*));
  root = next;
  root->id = 1;
  root->name = argv[0];

  for(int i = 1; i < argc; i++) {
    next = next->next =  malloc(sizeof(struct node*));
    next->id = i + 1;
    next->name = argv[i];
  }
  
  next->next = NULL;
  display(root);
  return 0;
}

void display(struct node *n) {
  do {
    printf("[%d] = %s\r\n", n->id, n->name);
  } while( n = n->next );
}  

Комментарии

  1. для компиляции можно использовать https://bellard.org/tcc/ следующим образом:
    tcc main.c
    тестовый запуск:
    C:\tcc\examples>main.exe 1 2 3 [1] = C:\tcc\examples\main.exe [2] = 1 [3] = 2 [4] = 3

    ОтветитьУдалить

Отправить комментарий