76 lines
1.5 KiB
C
76 lines
1.5 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
// <20>w<EFBFBD>q<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>c
|
|||
|
typedef struct Node {
|
|||
|
int data;
|
|||
|
struct Node* prev;
|
|||
|
struct Node* next;
|
|||
|
} Node;
|
|||
|
|
|||
|
// <20>w<EFBFBD>q<EFBFBD><71><EFBFBD>V<EFBFBD>쵲<EFBFBD><ECB5B2><EFBFBD>C<EFBFBD><43><EFBFBD>c
|
|||
|
typedef struct {
|
|||
|
Node* head;
|
|||
|
Node* tail;
|
|||
|
} DoublyLinkedList;
|
|||
|
|
|||
|
// <20><><EFBFBD>l<EFBFBD><6C><EFBFBD><EFBFBD><EFBFBD>V<EFBFBD>쵲<EFBFBD><ECB5B2><EFBFBD>C
|
|||
|
void initList(DoublyLinkedList* list) {
|
|||
|
list->head = NULL;
|
|||
|
list->tail = NULL;
|
|||
|
}
|
|||
|
|
|||
|
// <20>b<EFBFBD><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>s<EFBFBD>`<60>I
|
|||
|
void insertAtTail(DoublyLinkedList* list, int data) {
|
|||
|
Node* newNode = (Node*)malloc(sizeof(Node));
|
|||
|
newNode->data = data;
|
|||
|
newNode->next = NULL;
|
|||
|
|
|||
|
if (list->tail == NULL) {
|
|||
|
newNode->prev = NULL;
|
|||
|
list->head = newNode;
|
|||
|
list->tail = newNode;
|
|||
|
} else {
|
|||
|
newNode->prev = list->tail;
|
|||
|
list->tail->next = newNode;
|
|||
|
list->tail = newNode;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>q<EFBFBD>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD>M<EFBFBD><4D><EFBFBD>æL<C3A6>X<EFBFBD><58><EFBFBD>C
|
|||
|
void printListForward(DoublyLinkedList* list) {
|
|||
|
Node* current = list->head;
|
|||
|
printf("<EFBFBD>q<EFBFBD>Y<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
|
|||
|
while (current != NULL) {
|
|||
|
printf("%d ", current->data);
|
|||
|
current = current->next;
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
|
|||
|
// <20>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD>M<EFBFBD><4D><EFBFBD>æL<C3A6>X<EFBFBD><58><EFBFBD>C
|
|||
|
void printListBackward(DoublyLinkedList* list) {
|
|||
|
Node* current = list->tail;
|
|||
|
printf("<EFBFBD>q<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Y: ");
|
|||
|
while (current != NULL) {
|
|||
|
printf("%d ", current->data);
|
|||
|
current = current->prev;
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
|
|||
|
// <20>D<EFBFBD><44><EFBFBD>ƥܨ<C6A5>
|
|||
|
int main() {
|
|||
|
DoublyLinkedList list;
|
|||
|
initList(&list);
|
|||
|
|
|||
|
insertAtTail(&list, 10);
|
|||
|
insertAtTail(&list, 20);
|
|||
|
insertAtTail(&list, 30);
|
|||
|
|
|||
|
printListForward(&list);
|
|||
|
printListBackward(&list);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|