Предмет: Информатика, автор: kumiho9fox

Нужна помощь! Даю максимальное количество баллов! Язык программирования си (с функцией main) . Надо создать двусвязные списки. Функции: добавление элемента в начало, конец, в n-ую позицию. Очистка всего списка, очистка n-ого элемента в списке.
Умоляю, помогите!!!! ​Если можно, то с объяснением, дам лучший ответ!!! ​

Ответы

Автор ответа: Аноним
1

Ответ:

#include <stdio.h>

#include <stdlib.h>

typedef struct List List;

typedef struct Node Node;

struct List

{

unsigned int size;

Node* head;

Node* tail;

};

struct Node

{

int data;

Node* pNext;

Node* pPrev;

};

void init(List* list)

{

list->head = NULL;

list->tail = NULL;

list->size = 0;

}

void push_back(List* list, int data)

{

Node* new_node = (Node*) malloc(sizeof(Node));

if (new_node == NULL) return;

new_node->data = data;

new_node->pNext = NULL;

new_node->pPrev = NULL;

if (list->head == NULL)

{

 list->head = new_node;

 list->tail = new_node;

}

else

{

 new_node->pPrev = list->tail;

 list->tail->pNext = new_node;

 list->tail = new_node;

}

list->size++;

}

void push_front(List* list, int data)

{

Node* new_node = (Node*) malloc(sizeof(Node));

if (new_node == NULL) return;

new_node->data = data;

new_node->pNext = NULL;

new_node->pPrev = NULL;

if (list->head == NULL)

{

 list->head = new_node;

 list->tail = new_node;

}

else

{

 new_node->pNext = list->head;

 list->head->pPrev = new_node;

 list->head = new_node;

}

list->size++;

}

void insert(List* list, unsigned int index, int data)

{

if (index > list->size) return;

if (index == 0)

{

 push_front(list, data);

 return;

}

if (index == list->size)

{

 push_back(list, data);

 return;

}

Node* current = list->head;

for (unsigned int i = 0; i < index - 1; i++)

{

 current = current->pNext;

}

Node* new_node = (Node*) malloc(sizeof(Node));

if (new_node == NULL) return;

new_node->data = data;

new_node->pNext = current->pNext;

new_node->pPrev = current;

current->pNext->pPrev = new_node;

current->pNext = new_node;

list->size++;

}

void deleteAt(List* list, unsigned int index)

{

if (list->head == NULL) return;

Node* current = list->head;

unsigned int counter = 0;

while (counter <= index)

{

 if (counter >= list->size) return;

 

 if (counter == index)

 {

  if (current->pPrev != NULL)

  {

   current->pPrev->pNext = current->pNext;

  }

  else

  {

   list->head = current->pNext;

  }

  if (current->pNext != NULL)

  {

   current->pNext->pPrev = current->pPrev;

  }

  else

  {

   list->tail = current->pPrev;

  }

  free(current);

  list->size--;

  break;

 }

 current = current->pNext;

 counter++;

}

}

void deleteList(List* list)

{

Node* current = list->head;

while (current != NULL)

{

 Node* next = current->pNext;

 free(current);

 current = next;

}

}

Объяснение:

В main: (приклад)

List name;

init(&name);

push_back(&name);

З тебе краща відповідь


kumiho9fox: спасибо огромное! очень выручили!
Похожие вопросы
Предмет: Биология, автор: smaginaangelina6
Предмет: Физика, автор: muhadayana