Lila25mila
April 8, 2019, 1:44 p.m.

Linked list types - singly linked, doubly linked and circular

There are three common types of linked list.

  • Single linked list
  • doubly linked list
  • Circular linked list

Single linked list

This is the most common. Each node has data and a pointer to the next node.

Where the address of the first node is a special name called HEAD.
The last node in the linked list is pointed to by NULL.

The node is represented as:

  1. struct node {
  2. int data;
  3. struct node *next;
  4. }

A three-element singly linked list can be created as:

  1. /* Инициализируем узлы */
  2. struct node *head;
  3. struct node *one = NULL;
  4. struct node *two = NULL;
  5. struct node *three = NULL;
  6.  
  7. /* Выделяем память */
  8. one = malloc(sizeof(struct node));
  9. two = malloc(sizeof(struct node));
  10. three = malloc(sizeof(struct node));
  11.  
  12. /* Назначаем значения данных */
  13. one->data = 1;
  14. two->data = 2;
  15. three->data = 3;
  16.  
  17. /* Соединяем узлы */
  18. one->next = two;
  19. two->next = three;
  20. three->next = NULL;
  21.  
  22. /* Сохраняем адрес первого узла в голове */
  23. head = one;
Doubly linked list

We add a pointer to the previous node in the doubly linked list. Thus, we can go in any direction: forward (next) or backward (prev).

The node is represented as:

  1. struct node {
  2. int data;
  3. struct node *next;
  4. struct node *prev;
  5. }

A doubly linked list of three members can be created as:

  1. /* Инициализируем узлы */
  2. struct node *head;
  3. struct node *one = NULL;
  4. struct node *two = NULL;
  5. struct node *three = NULL;
  6.  
  7. /* Выделяем память */
  8. one = malloc(sizeof(struct node));
  9. two = malloc(sizeof(struct node));
  10. three = malloc(sizeof(struct node));
  11.  
  12. /* Назначаем значения данных */
  13. one->data = 1;
  14. two->data = 2;
  15. three->data = 3;
  16.  
  17. /* Соединяем узлы */
  18. one->next = two;
  19. one->prev = NULL;
  20.  
  21. two->next = three;
  22. two->prev = one;
  23.  
  24. three->next = NULL;
  25. three->prev = two;
  26.  
  27. /* Сохраняем адрес первого узла в голове */
  28. head = one;
Circular linked list

A circular linked list is a variant of a linked list where the last element is linked to the first element. This forms a circular loop.

A circular linked list can be either singly linked or doubly linked.

  • For a singly linked list, the last element's next pointer points to the first element;
  • In a doubly linked list, the prev pointer of the first element also points to the last element.

A circular singly linked list of three members can be created as:

  1. /* Инициализируем узлы */
  2. struct node *head;
  3. struct node *one = NULL;
  4. struct node *two = NULL;
  5. struct node *three = NULL;
  6.  
  7. /* Выделяем память */
  8. one = malloc(sizeof(struct node));
  9. two = malloc(sizeof(struct node));
  10. three = malloc(sizeof(struct node));
  11.  
  12. /* Назначаем значения данных */
  13. one->data = 1;
  14. two->data = 2;
  15. three->data = 3;
  16.  
  17. /* Соединяем узлы */
  18. one->next = two;
  19. two->next = three;
  20. three->next = one;
  21.  
  22. /* Сохраняем адрес первого узла в голове */
  23. head = one;

By article asked0question(s)

1

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html