Data_Structure/作業/unit4/try.c

291 lines
7.6 KiB
C
Raw Permalink Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
// <20>w<EFBFBD>q<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>c
typedef struct Node {
int value;
struct Node* next;
} Node;
// <20>w<EFBFBD>q<EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD><43><EFBFBD>c
typedef struct {
Node* head;
int length;
size_t memoryUsed; // <20>s<EFBFBD>W<EFBFBD>G<EFBFBD>l<EFBFBD>ܰO<DCB0><4F><EFBFBD><EFBFBD><EFBFBD>ϥ<EFBFBD>
} LinkedList;
int i;
// <20><><EFBFBD>ƭ쫬
Node* createNode(int value);
void initLinkedList(LinkedList* list);
void push(LinkedList* list, int value);
Node* pop(LinkedList* list);
void unshift(LinkedList* list, int value);
Node* shift(LinkedList* list);
void insert(LinkedList* list, int index, int value);
Node* removeNode(LinkedList* list, int index);
void get(LinkedList* list, int index);
void printAll(LinkedList* list);
void freeLinkedList(LinkedList* list);
// <20>D<EFBFBD><44><EFBFBD><EFBFBD>
int main() {
LinkedList list;
initLinkedList(&list);
int choice, value, index;
while (1) {
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>ާ@<40><><EFBFBD><EFBFBD>:\n");
printf("1. Push (<28>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD>W<EFBFBD>`<60>I)\n");
printf("2. Pop (<28>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I)\n");
printf("3. Unshift (<28>q<EFBFBD>Y<EFBFBD><59><EFBFBD>s<EFBFBD>W<EFBFBD>`<60>I)\n");
printf("4. Shift (<28>q<EFBFBD>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I)\n");
printf("5. Insert (<28>b<EFBFBD><62><EFBFBD>w<EFBFBD><77><EFBFBD>m<EFBFBD><6D><EFBFBD>J<EFBFBD>`<60>I)\n");
printf("6. Remove (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>w<EFBFBD><77><EFBFBD>m<EFBFBD><6D><EFBFBD>`<60>I)\n");
printf("7. Get (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>w<EFBFBD><77><EFBFBD>m<EFBFBD><6D><EFBFBD>`<60>I<EFBFBD><49>)\n");
printf("8. Print All (<28><><EFBFBD>ܩҦ<DCA9><D2A6>`<60>I)\n");
printf("0. <20>h<EFBFBD>X\n");
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>A<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
freeLinkedList(&list);
return 0;
case 1:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD>s<EFBFBD>W<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%d", &value);
push(&list, value);
break;
case 2:
{
Node* popped = pop(&list);
if (popped) {
printf("<EFBFBD>u<EFBFBD>X<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", popped->value);
free(popped);
list.memoryUsed -= sizeof(Node);
} else {
printf("<EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}
break;
case 3:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD>s<EFBFBD>W<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%d", &value);
unshift(&list, value);
break;
case 4:
{
Node* shifted = shift(&list);
if (shifted) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", shifted->value);
free(shifted);
} else {
printf("<EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}
break;
case 5:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>m: ");
scanf("%d", &index);
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%d", &value);
insert(&list, index, value);
break;
case 6:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m: ");
scanf("%d", &index);
{
Node* removed = removeNode(&list, index);
if (removed) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", removed->value);
free(removed);
list.memoryUsed -= sizeof(Node);
} else {
printf("<EFBFBD>L<EFBFBD>Ī<EFBFBD><EFBFBD><EFBFBD><EFBFBD>m\n");
}
}
break;
case 7:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m: ");
scanf("%d", &index);
get(&list, index);
break;
case 8:
printAll(&list);
break;
default:
printf("<EFBFBD>L<EFBFBD>Ī<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C>Э<EFBFBD><D0AD>s<EFBFBD><73><EFBFBD>J\n");
}
}
return 0;
}
// <20>Ыطs<D8B7>`<60>I
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("<EFBFBD>O<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>t<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
exit(1);
}
newNode->value = value;
newNode->next = NULL;
return newNode;
}
// <20><><EFBFBD>l<EFBFBD><6C><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C
void initLinkedList(LinkedList* list) {
list->head = NULL;
list->length = 0;
list->memoryUsed = 0;
}
// <20>q<EFBFBD><71><EFBFBD>s<EFBFBD>W<EFBFBD>`<60>I
void push(LinkedList* list, int value) {
Node* newNode = createNode(value);
if (list->length == 0) {
list->head = newNode;
} else {
Node* currentNode = list->head;
while (currentNode->next != NULL) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
list->length++;
list->memoryUsed += sizeof(Node);
}
// <20>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I
Node* pop(LinkedList* list) {
if (list->length == 0) {
return NULL;
} else if (list->length == 1) {
Node* temp = list->head;
list->head = NULL;
list->length = 0;
return temp;
} else {
Node* currentNode = list->head;
for (i = 1; i <= list->length - 2; i++) {
currentNode = currentNode->next;
}
Node* temp = currentNode->next;
currentNode->next = NULL;
list->length--;
return temp;
}
}
// <20>q<EFBFBD>Y<EFBFBD>s<EFBFBD>W<EFBFBD>`<60>I
void unshift(LinkedList* list, int value) {
Node* newNode = createNode(value);
if (list->length == 0) {
list->head = newNode;
} else {
newNode->next = list->head;
list->head = newNode;
}
list->length++;
list->memoryUsed += sizeof(Node);
}
// <20>q<EFBFBD>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD>`<60>I
Node* shift(LinkedList* list) {
if (list->length == 0) {
return NULL;
} else {
Node* temp = list->head;
list->head = list->head->next;
list->length--;
return temp;
}
}
// <20>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD>W<EFBFBD>`<60>I
void insert(LinkedList* list, int index, int value) {
if (index < 0 || index > list->length) {
return;
} else if (index == 0) {
unshift(list, value);
} else if (index == list->length) {
push(list, value);
} else {
Node* newNode = createNode(value);
Node* currentNode = list->head;
for ( i = 0; i < index - 1; i++) {
currentNode = currentNode->next;
}
newNode->next = currentNode->next;
currentNode->next = newNode;
list->length++;
}
}
// <20>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I
Node* removeNode(LinkedList* list, int index) {
if (index < 0 || index >= list->length) {
return NULL;
} else if (index == 0) {
return shift(list);
} else if (index == list->length - 1) {
return pop(list);
} else {
Node* currentNode = list->head;
for ( i = 0; i < index - 1; i++) {
currentNode = currentNode->next;
}
Node* temp = currentNode->next;
currentNode->next = currentNode->next->next;
list->length--;
return temp;
}
}
// <20><><EFBFBD>o<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>T
void get(LinkedList* list, int index) {
if (index < 0 || index >= list->length) {
printf("<EFBFBD><EFBFBD><EFBFBD>޶W<EFBFBD>X<EFBFBD>d<EFBFBD><EFBFBD>\n");
} else {
Node* currentNode = list->head;
for ( i = 0; i < index; i++) {
currentNode = currentNode->next;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><><EFBFBD><EFBFBD>: %d\n", index, currentNode->value);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T
void printAll(LinkedList* list) {
if (list->length == 0) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
} else {
Node* currentNode = list->head;
while (currentNode != NULL) {
printf("%d ", currentNode->value);
currentNode = currentNode->next;
}
printf("\n");
}
}
// <20>s<EFBFBD>W<EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C
void freeLinkedList(LinkedList* list) {
Node* current = list->head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
list->head = NULL;
list->length = 0;
list->memoryUsed = 0;
printf("<EFBFBD>Ҧ<EFBFBD><EFBFBD>O<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>w<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}