addLast
Coddy'nin Çift Yönlü Bağlı Liste - Veri Yapıları Serisi #6 kursunda ders 6 / 14.
Görev
KolayDoublyLinkedList sınıfına bir addLast metodu ekleyin.
Bu metot bir tam sayı value alır ve bu değeri taşıyan yeni bir düğümü listenin sonuna ekler. Bağlantının her iki yönünü de (yeni düğümdeki prev, eski kuyruktaki next) bağladığınızdan ve liste boşsa head değerini güncellediğinizden emin olun.
Kendin dene
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "doublylinkedlist.h"
int main() {
DoublyLinkedList ll;
DoublyLinkedList_init(&ll);
char line[256];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "state") == 0) printf("%s %s %d\n", ll.head == NULL ? "true" : "false", ll.tail == NULL ? "true" : "false", ll.count);
if (strcmp(cmd, "count") == 0) printf("%d\n", ll.count);
if (strcmp(cmd, "headValue") == 0) printf("%d\n", Node_getValue(ll.head));
if (strcmp(cmd, "tailValue") == 0) printf("%d\n", Node_getValue(ll.tail));
if (strcmp(cmd, "addFirst") == 0) {
char* arg = strtok(NULL, " \t");
DoublyLinkedList_addFirst(&ll, atoi(arg));
}
if (strcmp(cmd, "addLast") == 0) {
char* arg = strtok(NULL, " \t");
DoublyLinkedList_addLast(&ll, atoi(arg));
}
}
return 0;
}