顺序表
#include <stdio.h>
#include <stdlib.h>
typedef struct List {
int * arr;
int capacity;
int size;
} * ArrayList;
_Bool initList(ArrayList list) {
list->arr = malloc(sizeof(int) * 5);
if(list->arr == NULL) {
return 0;
}
list->capacity = 5;
list->size = 0;
return 1;
}
_Bool insertList(ArrayList list, int index, int data) {
if(index < 1 || index > list->size + 1) {
return 0;
}
if(list->size == list->capacity) {
int newCapacity = list->capacity + (list->capacity >> 1);
int * newArr = realloc(list->arr, sizeof(int) * newCapacity);
list->arr = newArr;
list->capacity = newCapacity;
}
for (int i = list->size; i > index - 1; i--) {
list->arr[i] = list->arr[i-1];
}
list->arr[index - 1] = data;
list->size++;
return 1;
}
_Bool deleteList(ArrayList list, int index) {
if(index < 1 || index > list->size) {
return 0;
}
for (int i = index - 1; i < list->size; ++i) {
list->arr[i] = list->arr[i+1];
}
list->size--;
return 1;
}
int getDataByIndex(ArrayList list, int index) {
if(index < 1 || index > list->size) {
return INT_MAX;
}
int data = list->arr[index - 1];
return data;
}
int getListLength(ArrayList list) {
return list->size;
}
void printList(ArrayList list) {
for (int i = 0; i < list->size; ++i) {
printf("%d", list->arr[i]);
printf("\t");
}
}
int main() {
struct List list;
printf("开始初始化\n");
if(initList(&list)) {
insertList(&list, 1, 20);
insertList(&list, 1, 30);
insertList(&list, 1, 50);
insertList(&list, 1, 60);
insertList(&list, 1, 70);
insertList(&list, 1, 80);
deleteList(&list, 6);
printList(&list);
printf("当前长度:%d\n", getListLength(&list));
printf("当前查找元素为:%d", getDataByIndex(&list, 1));
} else {
printf("初始化失败");
}
}
链表
单链表
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode{
int data;
struct LinkNode * next;
} * LinkList;
void initLinkNode(LinkList lNode) {
struct LinkNode * head = lNode;
head->next = NULL;
}
_Bool insertNode(LinkList head, int index, int data) {
if(index < 1) {
return 0;
}
struct LinkNode * p = head;
while(--index) {
p = p->next;
if(p == NULL) {
return 0;
}
}
LinkList newNode = malloc(sizeof(LinkList));
newNode->data = data;
newNode->next = p->next;
p->next = newNode;
return 1;
}
_Bool deleteNode(LinkList head, int index) {
if(index < 1) {
return 0;
}
LinkList p = head;
if(p->next == NULL) {
return 0;
}
while(--index){
p = p->next;
if(p == NULL) {
return 0;
}
}
struct LinkNode * q = p->next;
p->next = q->next;
free(q);
return 1;
}
int findNodeData(LinkList head, int index) {
if(index < 1) {
return 0;
}
LinkList p = head;
if(p->next == NULL) {
return INT_MAX;
}
while (--index) {
p = p->next;
if(p == NULL) {
return INT_MAX;
}
}
return p->next->data;
}
int getLinkListLength(LinkList head) {
int size = 0;
while (head->next) {
head = head->next;
size++;
}
return size;
}
void printLinkList(LinkList linkList) {
while (linkList) {
linkList = linkList->next;
printf("%d\t", linkList->data);
}
}
int main() {
struct LinkNode head;
initLinkNode(&head);
insertNode(&head, 1, 10);
insertNode(&head, 1, 20);
insertNode(&head, 3, 30);
deleteNode(&head, 1);
printf("%d\n", findNodeData(&head, 2));
printf("%d\n", getLinkListLength(&head));
printLinkList(&head);
}
双向链表
#include <stdio.h>
#include <stdlib.h>
typedef struct DoubleLinkNode{
int data;
struct DoubleLinkNode * next;
struct DoubleLinkNode * prev;
} * DoubleLinkList;
void initLinkNode(DoubleLinkList head) {
head->next = NULL;
head->prev = NULL;
}
_Bool insertNode(DoubleLinkList head, int index, int data) {
if(index < 1) {
return 0;
}
struct DoubleLinkNode * p = head;
while(--index) {
p = p->next;
if(p == NULL) {
return 0;
}
}
DoubleLinkList newNode = malloc(sizeof(DoubleLinkList));
if(newNode == NULL) {
return 0;
}
newNode->data = data;
if(p->next) {
newNode->next = p->next;
p->next->prev = newNode;
} else {
newNode->next = NULL;
}
p->next = newNode;
newNode->prev = p;
return 1;
}
_Bool deleteNode(DoubleLinkList head, int index) {
if(index < 1) {
return 0;
}
DoubleLinkList p = head;
if(p->next == NULL) {
return 0;
}
while(--index){
p = p->next;
if(p == NULL) {
return 0;
}
}
struct DoubleLinkNode * q = p->next;
if(q->next) {
p->next = q->next;
q->next->prev = p;
} else {
p->next = NULL;
}
free(q);
return 1;
}
int findNodeData(DoubleLinkList head, int index) {
if(index < 1) {
return 0;
}
DoubleLinkList p = head;
if(p->next == NULL) {
return INT_MAX;
}
while (--index) {
p = p->next;
if(p == NULL) {
return INT_MAX;
}
}
return p->next->data;
}
int getDoubleLinkListLength(DoubleLinkList head) {
int size = 0;
while (head->next) {
head = head->next;
size++;
}
return size;
}