双向链表
大神的基操
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeTag Node;
typedef struct LinkedListTag LinkedList;
struct NodeTag
{
Node* prev;
Node* next;
void* data;
};
struct LinkedListTag
{
Node* head;
Node* tail;
Node* cur;
int size;
};
LinkedList* Create( void )
{
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
if(!list) return NULL;
list->head = NULL;
list->tail = NULL;
list->cur = NULL;
list->size = 0;
return list;
}
int AddBack(LinkedList* list, void* data)
{
Node* node = (Node *)malloc(sizeof(Node));
if(!node) return -1;
node->data = data;
if(list->tail)
{
list->tail->next = node;
node->prev = list->tail;
node->next = NULL;
}
else
{
node->next = NULL;
node->prev = NULL;
list->head = node;
}
list->tail = node;
return ++list->size;
}
int AddFront(LinkedList* list, void* data)
{
Node *node = (Node*)malloc(sizeof(Node));
if(!node) return 0;
node->data = data;
if(list->head)
{
list->head->prev = node;
node->next = list->head;
node->prev = NULL;
}
else
{
node->next = NULL;
node->prev = NULL;
list->tail = node;
}
list->head = node;
return ++list->size;
}
void* RemoveBack(LinkedList* list)
{
Node* temp;
void* data;
if(!list->size) return NULL;
temp = list->tail;
data = list->tail->data;
if(list->head == list->tail)
{
list->head = NULL;
list->tail = NULL;
list->cur = NULL;
}
else
{
list->tail = list->tail->prev;
list->tail->next = NULL;
}
--list->size;
free(temp);
return data;
}
void* RemoveFront(LinkedList* list)
{
Node* temp;
void* data;
if(!list->size) return NULL;
temp = list->head;
data = list->head->data;
if(list->head == list->tail)
{
list->head = NULL;
list->tail = NULL;
list->cur = NULL;
}
else
{
list->head = list->head->next;
list->head->prev = NULL;
}
--list->size;
free(temp);
return data;
}
int IsEmpty(LinkedList* list)
{
return list->size == 0;
}
int Size(LinkedList* list)
{
return list->size;
}
void Begin(LinkedList* list)
{
list->cur = list->head;
}
void End(LinkedList* list)
{
list->cur = list->tail;
}
void MoveNext(LinkedList* list)
{
list->cur = list->cur->next;
}
void MovePrev(LinkedList* list)
{
list->cur = list->cur->prev;
}
void Clear(LinkedList* list)
{
while(RemoveBack(list));
}
void Destroy(LinkedList* list)
{
Clear(list);
free(list);
}
int HasNext(LinkedList* list)
{
if (!list->cur) return 0;
if (list->cur == list->tail) return 1;
return list->cur->next != NULL;
}
int HasPrev(LinkedList* list)
{
if (!list->cur) return 0;
if (list->cur == list->head) return 1;
return list->cur->prev != NULL;
}
void* Current(LinkedList* list)
{
return list->cur->data;
}
void Traverse(LinkedList* list)
{
for( Begin(list); HasNext(list); MoveNext(list) )
printf("%d ", *(int*)Current(list));
putchar('\n');
}
void RTraverse(LinkedList* list)
{
for (End(list); HasPrev(list); MovePrev(list))
printf("%d ", *(int*)Current(list));
putchar('\n');
}
int main()
{
int i;
LinkedList* list = Create();
int array1[10];
int array2[10];
for(i=0; i<10; i++)
{
array1[i] = i+1;
array2[i] = i+100+1;
AddBack(list, &array1[i]);
}
printf("链表大小(SIZE): %d\n", Size(list));
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
printf("添加array2[0]数\n");
AddBack(list, &array2[0]);
printf("链表大小(SIZE): %d\n", Size(list));
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
printf("调用AddFront函数,添加array2[0]数\n");
AddFront(list, &array2[1]);
printf("链表大小(SIZE): %d\n", Size(list));
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
printf("从末尾移除的元素是: %d\n", *(int *)RemoveBack(list));
printf("链表大小(SIZE): %d\n", Size(list));
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
printf("从开头移除的元素是: %d\n", *(int *)RemoveFront(list));
printf("链表大小(SIZE): %d\n", Size(list));
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
printf("清空链表,Clear(list)后\n");
Clear(list);
printf("链表大小(SIZE): %d\n", Size(list));
for(i=0; i<10; i++)
{
AddFront(list, &array2[i]);
}
printf("正向打印链表:\n");
Traverse(list);
printf("反向打印链表:\n");
RTraverse(list);
Destroy(list);
printf("销毁链表Destroy(list)\n");
return 0;
}
大神代码的链接
自己的一点想法
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct Node
{
Node *prev;
Node *next;
ElemType *data;
} Node, *NodePointer;
typedef struct LinkList
{
Node *head;
Node *tail;
Node *cur;
int size;
} LinkList, *LinkListPointer;
LinkListPointer CreatLinkList()
{
LinkListPointer L = (LinkListPointer)malloc(sizeof(LinkList));
if (!L)
{
return NULL;
}
L->head = NULL;
L->cur = NULL;
L->tail = NULL;
L->size = 0;
return L;
}
int AddBackLinkList(LinkListPointer L, ElemType *data)
{
NodePointer node = (NodePointer)malloc(sizeof(Node));
if (!node)
{
return 0;
}
node->data = data;
if (L->tail)
{
L->tail->next = node;
node->prev = L->tail;
node->next = NULL;
}
else
{
L->head = node;
node->prev = NULL;
node->next = NULL;
}
L->tail = node;
return ++L->size;
}
int SizeLinkList(LinkListPointer L)
{
return L->size;
}
void Begin(LinkListPointer L)
{
L->cur = L->head;
}
void End(LinkListPointer L)
{
L->cur = L->tail;
}
void MoveNext(LinkListPointer L)
{
L->cur = L->cur->next;
}
int HasNext(LinkListPointer L)
{
if (!L->cur)
{
return 0;
}
else
{
return 1;
}
}
ElemType *Current(LinkListPointer L)
{
return L->cur->data;
}
void Traverse(LinkListPointer L)
{
for (Begin(L); HasNext(L); MoveNext(L))
{
printf("%-5d", *Current(L));
}
cout << endl;
}
int main(int argc, char const *argv[])
{
LinkListPointer L = CreatLinkList();
ElemType temp[10];
for (int i = 0; i < sizeof(temp) / sizeof(temp[0]); i++)
{
temp[i] = i;
AddBackLinkList(L, &temp[i]);
}
Traverse(L);
return 0;
}