项目描述:
临近毕业设计,想搞一下java方面的组件代码。前面已学过java的接口技术(比如jni和jna)。jni性能虽好但是要想用java代码调用c/c++写的函数需要先写一个包含原始方法(native)的类,然后需要用javah生成头文件,接着需要实现头文件里的函数,最后需要用gcc或g++编译工具生成动态链接库。因此此次组件采用更为简单的jna框架实现。虽然性能较jni稍微比较弱,尤其是在自定义数据类型比如结构体或联合体等等因转换成native可识别的数据类型造成的性能损失。但是本着能简单一点是一点的基本原则,我就动手翻译了之前的老项目的c/c++函数库。项目如下所示:
项目结构:
项目的目录及文件结构如下所示:
C:\USERS\12952\DESKTOP\COM
+---AdroitFisherman
| \---DataHandler
| | Set_Encode_Format.java
| |
| +---middle_wares
| | CircularDoubleLinkedList.java
| | CircularQueue.java
| | CircularSingleLinkedList.java
| | DoubleLinkedList.java
| | DoubleLinkedListWithoutHeadNode.java
| | HuffManTree.java
| | LinkedArray.java
| | LinkedQueue.java
| | LinkedStack.java
| | SequentialArray.java
| | SequentialList.java
| | SequentialQueue.java
| | SequentialStack.java
| | SingleLinkedList.java
| | SingleLinkedListWithoutHeadNode.java
| |
| \---VariousType
| CircularDoubleLinked_List.java
| CircularSingleLinked_List.java
| Circular_Queue.java
| DoubleLinkedList_WithoutHeadNode.java
| DoubleLinked_List.java
| HuffMan_Tree.java
| Linked_Array.java
| Linked_Queue.java
| Linked_Stack.java
| Sequential_Array.java
| Sequential_List.java
| Sequential_Queue.java
| Sequential_Stack.java
| SingleLinkedList_WithoutHeadNode.java
| SingleLinked_List.java
|
\---pre-compile
| compile.py
|
+---CircularDoubleLinkedList
| CircularDoubleLinkedList_char.cpp
| CircularDoubleLinkedList_char.h
| CircularDoubleLinkedList_double.cpp
| CircularDoubleLinkedList_double.h
| CircularDoubleLinkedList_int.cpp
| CircularDoubleLinkedList_int.h
| CircularDoubleLinkedList_string.cpp
| CircularDoubleLinkedList_string.h
|
+---CircularQueue
| CircularQueue_char.cpp
| CircularQueue_char.h
| CircularQueue_double.cpp
| CircularQueue_double.h
| CircularQueue_int.cpp
| CircularQueue_int.h
| CircularQueue_string.cpp
| CircularQueue_string.h
|
+---CircularSingleLinkedList
| CircularSingleLinkedList_char.cpp
| CircularSingleLinkedList_char.h
| CircularSingleLinkedList_double.cpp
| CircularSingleLinkedList_double.h
| CircularSingleLinkedList_int.cpp
| CircularSingleLinkedList_int.h
| CircularSingleLinkedList_string.cpp
| CircularSingleLinkedList_string.h
|
+---DoubleLinkedList
| DoubleLinkedList_char.cpp
| DoubleLinkedList_char.h
| DoubleLinkedList_double.cpp
| DoubleLinkedList_double.h
| DoubleLinkedList_int.cpp
| DoubleLinkedList_int.h
| DoubleLinkedList_string.cpp
| DoubleLinkedList_string.h
|
+---DoubleLinkedListWithoutHeadNode
| DoubleLinkedListWithoutHeadNode_char.cpp
| DoubleLinkedListWithoutHeadNode_char.h
| DoubleLinkedListWithoutHeadNode_double.cpp
| DoubleLinkedListWithoutHeadNode_double.h
| DoubleLinkedListWithoutHeadNode_int.cpp
| DoubleLinkedListWithoutHeadNode_int.h
| DoubleLinkedListWithoutHeadNode_string.cpp
| DoubleLinkedListWithoutHeadNode_string.h
|
+---HuffManTree
| HuffManTree_char.cpp
| HuffManTree_char.h
| HuffManTree_double.cpp
| HuffManTree_double.h
| HuffManTree_int.cpp
| HuffManTree_int.h
| HuffManTree_string.cpp
| HuffManTree_string.h
|
+---LinkedArray
| LinkedArray_char.cpp
| LinkedArray_char.h
| LinkedArray_double.cpp
| LinkedArray_double.h
| LinkedArray_int.cpp
| LinkedArray_int.h
| LinkedArray_string.cpp
| LinkedArray_string.h
|
+---LinkedQueue
| LinkedQueue_char.cpp
| LinkedQueue_char.h
| LinkedQueue_double.cpp
| LinkedQueue_double.h
| LinkedQueue_int.cpp
| LinkedQueue_int.h
| LinkedQueue_string.cpp
| LinkedQueue_string.h
|
+---LinkedStack
| LinkedStack_char.cpp
| LinkedStack_char.h
| LinkedStack_double.cpp
| LinkedStack_double.h
| LinkedStack_int.cpp
| LinkedStack_int.h
| LinkedStack_string.cpp
| LinkedStack_string.h
|
+---output
+---SequentialArray
| SequentialArray_char.cpp
| SequentialArray_char.h
| SequentialArray_double.cpp
| SequentialArray_double.h
| SequentialArray_int.cpp
| SequentialArray_int.h
| SequentialArray_string.cpp
| SequentialArray_string.h
|
+---SequentialList
| SequentialList_char.cpp
| SequentialList_char.h
| SequentialList_double.cpp
| SequentialList_double.h
| SequentialList_int.cpp
| SequentialList_int.h
| SequentialList_string.cpp
| SequentialList_string.h
|
+---SequentialQueue
| SequentialQueue_char.cpp
| SequentialQueue_char.h
| SequentialQueue_double.cpp
| SequentialQueue_double.h
| SequentialQueue_int.cpp
| SequentialQueue_int.h
| SequentialQueue_string.cpp
| SequentialQueue_string.h
|
+---SequentialStack
| SequentialStack_char.cpp
| SequentialStack_char.h
| SequentialStack_double.cpp
| SequentialStack_double.h
| SequentialStack_int.cpp
| SequentialStack_int.h
| SequentialStack_string.cpp
| SequentialStack_string.h
|
+---SingleLinkedList
| SingleLinkedList_char.cpp
| SingleLinkedList_char.h
| SingleLinkedList_double.cpp
| SingleLinkedList_double.h
| SingleLinkedList_int.cpp
| SingleLinkedList_int.h
| SingleLinkedList_string.cpp
| SingleLinkedList_string.h
|
\---SingleLinkedListWithoutHeadNode
SingleLinkedListWithoutHeadNode_char.cpp
SingleLinkedListWithoutHeadNode_char.h
SingleLinkedListWithoutHeadNode_double.cpp
SingleLinkedListWithoutHeadNode_double.h
SingleLinkedListWithoutHeadNode_int.cpp
SingleLinkedListWithoutHeadNode_int.h
SingleLinkedListWithoutHeadNode_string.cpp
SingleLinkedListWithoutHeadNode_string.h
函数库源码:
HuffManTree_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "HuffManTree_char.h"
int InitHuffTree(HTree* tree,int weight,ElemType elem)
{
HTNode* summon=(HTNode*)malloc(sizeof(HTNode));
if(summon!=NULL)
{
summon->elem=elem;
summon->weight=weight;
summon->left=NULL;
summon->parent=NULL;
summon->right=NULL;
(*tree)=summon;
return TRUE;
}else
{
return FALSE;
}
}
void DestroyHuffTree(HTree* tree)
{
if((*tree)!=NULL)
{
DestroyHuffTree(&(*tree)->left);
DestroyHuffTree(&(*tree)->right);
free((*tree));
(*tree)=NULL;
}
}
void PreTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
printf("%c\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
}
}
void InTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
printf("%c\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->right);
}
}
void PostTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
printf("%c\t",(*htnode)->elem);
}
}
HTree CreateHuffTree(int* weights,ElemType* elems,int length)
{
HTNode* collection[length*2-1];
HTNode* temp=NULL;
for(int i=0;i<length;i++)
{
InitHuffTree(&collection[i],weights[i],elems[i]);
}
for(int i=length;i<length*2-1;i++)
{
InitHuffTree(&collection[i],0,NULL);
}
for(int i=length;i<length*2-1;i++)
{
for(int j=(i-length)*2;j<i;j++)
{
for(int k=j+1;k<i;k++)
{
if(collection[j]->weight>collection[k]->weight)
{
temp=collection[j];
collection[j]=collection[k];
collection[k]=temp;
}
}
}
collection[i]->weight=collection[(i-length)*2]->weight+collection[(i-length)*2+1]->weight;
collection[i]->left=collection[(i-length)*2];
collection[i]->right=collection[(i-length)*2+1];
collection[i]->left->parent=collection[i];
collection[i]->right->parent=collection[i];
}
return collection[length*2-2];
}
int GetLength(ElemType* elems)
{
int i=0;
while(elems[i]!='\0')
{
++i;
}
return i;
}
HTNode* GetNode(HTree* tree,ElemType elem)
{
HTNode* result=NULL;
if((*tree)==NULL)
{
return NULL;
}
else if((*tree)->elem==elem)
{
return (*tree);
}else
{
result=GetNode(&(*tree)->left,elem);
if(result!=NULL)
{
return result;
}
result=GetNode(&(*tree)->right,elem);
if(result!=NULL)
{
return result;
}
return NULL;
}
}
HuffCode GetHuffCode(HTree* tree,ElemType elem)
{
HTNode* tmp=GetNode(&(*tree),elem);
HuffCode result;
int counter=0,tag=0;
result.elem=elem;
if(tmp==NULL)
{
result.huffcode=(char*)malloc(sizeof(char));
result.weight=0;
result.code_length=0;
return result;
}
while(tmp->parent!=NULL)
{
tmp=tmp->parent;
counter++;
}
tmp=GetNode(&(*tree),elem);
result.elem=elem;
result.huffcode=(char*)malloc(sizeof(char)*(counter+1));
result.weight=tmp->weight;
while(tmp->parent!=NULL)
{
if(tmp->parent->left==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='0';
}
else if(tmp->parent->right==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='1';
}
tmp=tmp->parent;
tag++;
}
result.huffcode[tag]='\0';
result.code_length=counter;
return result;
}
void DestroyHuffCode(HuffCode* code)
{
free(code->huffcode);
code->huffcode=NULL;
}
int GetHuffTreeDepth(HTree* tree)
{
int max_depth=0;
int left=0;
int right=0;
if((*tree)==NULL)
{
return 0;
}
else
{
left=GetHuffTreeDepth(&(*tree)->left)+1;
if(max_depth<left)
{
max_depth=left;
}
right=GetHuffTreeDepth(&(*tree)->right)+1;
if(max_depth<right)
{
max_depth=right;
}
return max_depth;
}
}
int GetWPL(HTree* tree,ElemType* elems)
{
int counter=GetLength(elems);
int start=0;
int result=0;
HuffCode temp;
while(start<counter)
{
temp=GetHuffCode(&(*tree),elems[start]);
result=result+temp.code_length*temp.weight;
start++;
}
return result;
}
HuffManTree_char.h
#ifndef HUFFMANTREE_CHAR_H_INCLUDED
#define HUFFMANTREE_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node{
ElemType elem;
struct Node* left;
struct Node* right;
struct Node* parent;
int weight;
}HTNode,*HTree;
typedef struct
{
ElemType elem;
char* huffcode;
int code_length;
int weight;
}HuffCode;
extern "C" __declspec(dllexport) int InitHuffTree(HTree* tree,int weight,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffTree(HTree* tree);
extern "C" __declspec(dllexport) void PreTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void InTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void PostTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) HTree CreateHuffTree(int* weights,ElemType* elems,int length);
extern "C" __declspec(dllexport) int GetLength(ElemType* elems);
extern "C" __declspec(dllexport) HTNode* GetNode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) HuffCode GetHuffCode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffCode(HuffCode* code);
extern "C" __declspec(dllexport) int GetHuffTreeDepth(HTree* tree);
extern "C" __declspec(dllexport) int GetWPL(HTree* tree,ElemType* elems);
#endif // HUFFMANTREE_CHAR_H_INCLUDED
HuffManTree_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "HuffManTree_double.h"
int InitHuffTree(HTree* tree,int weight,ElemType elem)
{
HTNode* summon=(HTNode*)malloc(sizeof(HTNode));
if(summon!=NULL)
{
summon->elem=elem;
summon->weight=weight;
summon->left=NULL;
summon->parent=NULL;
summon->right=NULL;
(*tree)=summon;
return TRUE;
}else
{
return FALSE;
}
}
void DestroyHuffTree(HTree* tree)
{
if((*tree)!=NULL)
{
DestroyHuffTree(&(*tree)->left);
DestroyHuffTree(&(*tree)->right);
free((*tree));
(*tree)=NULL;
}
}
void PreTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
printf("%f\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
}
}
void InTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
printf("%f\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->right);
}
}
void PostTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
printf("%f\t",(*htnode)->elem);
}
}
HTree CreateHuffTree(int* weights,ElemType* elems,int length)
{
HTNode* collection[length*2-1];
HTNode* temp=NULL;
for(int i=0;i<length;i++)
{
InitHuffTree(&collection[i],weights[i],elems[i]);
}
for(int i=length;i<length*2-1;i++)
{
InitHuffTree(&collection[i],0,NULL);
}
for(int i=length;i<length*2-1;i++)
{
for(int j=(i-length)*2;j<i;j++)
{
for(int k=j+1;k<i;k++)
{
if(collection[j]->weight>collection[k]->weight)
{
temp=collection[j];
collection[j]=collection[k];
collection[k]=temp;
}
}
}
collection[i]->weight=collection[(i-length)*2]->weight+collection[(i-length)*2+1]->weight;
collection[i]->left=collection[(i-length)*2];
collection[i]->right=collection[(i-length)*2+1];
collection[i]->left->parent=collection[i];
collection[i]->right->parent=collection[i];
}
return collection[length*2-2];
}
int GetLength(ElemType* elems)
{
int i=0;
while(elems[i]!='\0')
{
++i;
}
return i;
}
HTNode* GetNode(HTree* tree,ElemType elem)
{
HTNode* result=NULL;
if((*tree)==NULL)
{
return NULL;
}
else if((*tree)->elem==elem)
{
return (*tree);
}else
{
result=GetNode(&(*tree)->left,elem);
if(result!=NULL)
{
return result;
}
result=GetNode(&(*tree)->right,elem);
if(result!=NULL)
{
return result;
}
return NULL;
}
}
HuffCode GetHuffCode(HTree* tree,ElemType elem)
{
HTNode* tmp=GetNode(&(*tree),elem);
HuffCode result;
int counter=0,tag=0;
result.elem=elem;
if(tmp==NULL)
{
result.huffcode=(char*)malloc(sizeof(char));
result.weight=0;
result.code_length=0;
return result;
}
while(tmp->parent!=NULL)
{
tmp=tmp->parent;
counter++;
}
tmp=GetNode(&(*tree),elem);
result.elem=elem;
result.huffcode=(char*)malloc(sizeof(char)*(counter+1));
result.weight=tmp->weight;
while(tmp->parent!=NULL)
{
if(tmp->parent->left==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='0';
}
else if(tmp->parent->right==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='1';
}
tmp=tmp->parent;
tag++;
}
result.huffcode[tag]='\0';
result.code_length=counter;
return result;
}
void DestroyHuffCode(HuffCode* code)
{
free(code->huffcode);
code->huffcode=NULL;
}
int GetHuffTreeDepth(HTree* tree)
{
int max_depth=0;
int left=0;
int right=0;
if((*tree)==NULL)
{
return 0;
}
else
{
left=GetHuffTreeDepth(&(*tree)->left)+1;
if(max_depth<left)
{
max_depth=left;
}
right=GetHuffTreeDepth(&(*tree)->right)+1;
if(max_depth<right)
{
max_depth=right;
}
return max_depth;
}
}
int GetWPL(HTree* tree,ElemType* elems)
{
int counter=GetLength(elems);
int start=0;
int result=0;
HuffCode temp;
while(start<counter)
{
temp=GetHuffCode(&(*tree),elems[start]);
result=result+temp.code_length*temp.weight;
start++;
}
return result;
}
HuffManTree_double.h
#ifndef HUFFMANTREE_DOUBLE_H_INCLUDED
#define HUFFMANTREE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node{
ElemType elem;
struct Node* left;
struct Node* right;
struct Node* parent;
int weight;
}HTNode,*HTree;
typedef struct
{
ElemType elem;
char* huffcode;
int code_length;
int weight;
}HuffCode;
extern "C" __declspec(dllexport) int InitHuffTree(HTree* tree,int weight,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffTree(HTree* tree);
extern "C" __declspec(dllexport) void PreTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void InTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void PostTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) HTree CreateHuffTree(int* weights,ElemType* elems,int length);
extern "C" __declspec(dllexport) int GetLength(ElemType* elems);
extern "C" __declspec(dllexport) HTNode* GetNode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) HuffCode GetHuffCode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffCode(HuffCode* code);
extern "C" __declspec(dllexport) int GetHuffTreeDepth(HTree* tree);
extern "C" __declspec(dllexport) int GetWPL(HTree* tree,ElemType* elems);
#endif // HUFFMANTREE_DOUBLE_H_INCLUDED
HuffManTree_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "HuffManTree_int.h"
int InitHuffTree(HTree* tree,int weight,ElemType elem)
{
HTNode* summon=(HTNode*)malloc(sizeof(HTNode));
if(summon!=NULL)
{
summon->elem=elem;
summon->weight=weight;
summon->left=NULL;
summon->parent=NULL;
summon->right=NULL;
(*tree)=summon;
return TRUE;
}else
{
return FALSE;
}
}
void DestroyHuffTree(HTree* tree)
{
if((*tree)!=NULL)
{
DestroyHuffTree(&(*tree)->left);
DestroyHuffTree(&(*tree)->right);
free((*tree));
(*tree)=NULL;
}
}
void PreTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
printf("%d\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
}
}
void InTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
printf("%d\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->right);
}
}
void PostTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
printf("%d\t",(*htnode)->elem);
}
}
HTree CreateHuffTree(int* weights,ElemType* elems,int length)
{
HTNode* collection[length*2-1];
HTNode* temp=NULL;
for(int i=0;i<length;i++)
{
InitHuffTree(&collection[i],weights[i],elems[i]);
}
for(int i=length;i<length*2-1;i++)
{
InitHuffTree(&collection[i],0,NULL);
}
for(int i=length;i<length*2-1;i++)
{
for(int j=(i-length)*2;j<i;j++)
{
for(int k=j+1;k<i;k++)
{
if(collection[j]->weight>collection[k]->weight)
{
temp=collection[j];
collection[j]=collection[k];
collection[k]=temp;
}
}
}
collection[i]->weight=collection[(i-length)*2]->weight+collection[(i-length)*2+1]->weight;
collection[i]->left=collection[(i-length)*2];
collection[i]->right=collection[(i-length)*2+1];
collection[i]->left->parent=collection[i];
collection[i]->right->parent=collection[i];
}
return collection[length*2-2];
}
int GetLength(ElemType* elems)
{
int i=0;
while(elems[i]!='\0')
{
++i;
}
return i;
}
HTNode* GetNode(HTree* tree,ElemType elem)
{
HTNode* result=NULL;
if((*tree)==NULL)
{
return NULL;
}
else if((*tree)->elem==elem)
{
return (*tree);
}else
{
result=GetNode(&(*tree)->left,elem);
if(result!=NULL)
{
return result;
}
result=GetNode(&(*tree)->right,elem);
if(result!=NULL)
{
return result;
}
return NULL;
}
}
HuffCode GetHuffCode(HTree* tree,ElemType elem)
{
HTNode* tmp=GetNode(&(*tree),elem);
HuffCode result;
int counter=0,tag=0;
result.elem=elem;
if(tmp==NULL)
{
result.huffcode=(char*)malloc(sizeof(char));
result.weight=0;
result.code_length=0;
return result;
}
while(tmp->parent!=NULL)
{
tmp=tmp->parent;
counter++;
}
tmp=GetNode(&(*tree),elem);
result.elem=elem;
result.huffcode=(char*)malloc(sizeof(char)*(counter+1));
result.weight=tmp->weight;
while(tmp->parent!=NULL)
{
if(tmp->parent->left==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='0';
}
else if(tmp->parent->right==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='1';
}
tmp=tmp->parent;
tag++;
}
result.huffcode[tag]='\0';
result.code_length=counter;
return result;
}
void DestroyHuffCode(HuffCode* code)
{
free(code->huffcode);
code->huffcode=NULL;
}
int GetHuffTreeDepth(HTree* tree)
{
int max_depth=0;
int left=0;
int right=0;
if((*tree)==NULL)
{
return 0;
}
else
{
left=GetHuffTreeDepth(&(*tree)->left)+1;
if(max_depth<left)
{
max_depth=left;
}
right=GetHuffTreeDepth(&(*tree)->right)+1;
if(max_depth<right)
{
max_depth=right;
}
return max_depth;
}
}
int GetWPL(HTree* tree,ElemType* elems)
{
int counter=GetLength(elems);
int start=0;
int result=0;
HuffCode temp;
while(start<counter)
{
temp=GetHuffCode(&(*tree),elems[start]);
result=result+temp.code_length*temp.weight;
start++;
}
return result;
}
HuffManTree_int.h
#ifndef HUFFMANTREE_INT_H_INCLUDED
#define HUFFMANTREE_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node{
ElemType elem;
struct Node* left;
struct Node* right;
struct Node* parent;
int weight;
}HTNode,*HTree;
typedef struct
{
ElemType elem;
char* huffcode;
int code_length;
int weight;
}HuffCode;
extern "C" __declspec(dllexport) int InitHuffTree(HTree* tree,int weight,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffTree(HTree* tree);
extern "C" __declspec(dllexport) void PreTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void InTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void PostTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) HTree CreateHuffTree(int* weights,ElemType* elems,int length);
extern "C" __declspec(dllexport) int GetLength(ElemType* elems);
extern "C" __declspec(dllexport) HTNode* GetNode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) HuffCode GetHuffCode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffCode(HuffCode* code);
extern "C" __declspec(dllexport) int GetHuffTreeDepth(HTree* tree);
extern "C" __declspec(dllexport) int GetWPL(HTree* tree,ElemType* elems);
#endif // HUFFMANTREE_INT_H_INCLUDED
HuffManTree_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "HuffManTree_string.h"
int InitHuffTree(HTree* tree,int weight,ElemType elem)
{
HTNode* summon=(HTNode*)malloc(sizeof(HTNode));
if(summon!=NULL)
{
summon->elem=elem;
summon->weight=weight;
summon->left=NULL;
summon->parent=NULL;
summon->right=NULL;
(*tree)=summon;
return TRUE;
}else
{
return FALSE;
}
}
void DestroyHuffTree(HTree* tree)
{
if((*tree)!=NULL)
{
DestroyHuffTree(&(*tree)->left);
DestroyHuffTree(&(*tree)->right);
free((*tree));
(*tree)=NULL;
}
}
void PreTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
printf("%s\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
}
}
void InTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
printf("%s\t",(*htnode)->elem);
PreTraverseTree(&(*htnode)->right);
}
}
void PostTraverseTree(HTree* htnode)
{
if((*htnode)!=NULL)
{
PreTraverseTree(&(*htnode)->left);
PreTraverseTree(&(*htnode)->right);
printf("%s\t",(*htnode)->elem);
}
}
HTree CreateHuffTree(int* weights,ElemType* elems,int length)
{
HTNode* collection[length*2-1];
HTNode* temp=NULL;
for(int i=0;i<length;i++)
{
InitHuffTree(&collection[i],weights[i],elems[i]);
}
for(int i=length;i<length*2-1;i++)
{
InitHuffTree(&collection[i],0,NULL);
}
for(int i=length;i<length*2-1;i++)
{
for(int j=(i-length)*2;j<i;j++)
{
for(int k=j+1;k<i;k++)
{
if(collection[j]->weight>collection[k]->weight)
{
temp=collection[j];
collection[j]=collection[k];
collection[k]=temp;
}
}
}
collection[i]->weight=collection[(i-length)*2]->weight+collection[(i-length)*2+1]->weight;
collection[i]->left=collection[(i-length)*2];
collection[i]->right=collection[(i-length)*2+1];
collection[i]->left->parent=collection[i];
collection[i]->right->parent=collection[i];
}
return collection[length*2-2];
}
int GetLength(ElemType* elems)
{
int i=0;
while(elems[i]!='\0')
{
++i;
}
return i;
}
HTNode* GetNode(HTree* tree,ElemType elem)
{
HTNode* result=NULL;
if((*tree)==NULL)
{
return NULL;
}
else if((*tree)->elem==elem)
{
return (*tree);
}else
{
result=GetNode(&(*tree)->left,elem);
if(result!=NULL)
{
return result;
}
result=GetNode(&(*tree)->right,elem);
if(result!=NULL)
{
return result;
}
return NULL;
}
}
HuffCode GetHuffCode(HTree* tree,ElemType elem)
{
HTNode* tmp=GetNode(&(*tree),elem);
HuffCode result;
int counter=0,tag=0;
result.elem=elem;
if(tmp==NULL)
{
result.huffcode=(char*)malloc(sizeof(char));
result.weight=0;
result.code_length=0;
return result;
}
while(tmp->parent!=NULL)
{
tmp=tmp->parent;
counter++;
}
tmp=GetNode(&(*tree),elem);
result.elem=elem;
result.huffcode=(char*)malloc(sizeof(char)*(counter+1));
result.weight=tmp->weight;
while(tmp->parent!=NULL)
{
if(tmp->parent->left==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='0';
}
else if(tmp->parent->right==tmp&&tmp->parent!=NULL)
{
result.huffcode[counter-tag-1]='1';
}
tmp=tmp->parent;
tag++;
}
result.huffcode[tag]='\0';
result.code_length=counter;
return result;
}
void DestroyHuffCode(HuffCode* code)
{
free(code->huffcode);
code->huffcode=NULL;
}
int GetHuffTreeDepth(HTree* tree)
{
int max_depth=0;
int left=0;
int right=0;
if((*tree)==NULL)
{
return 0;
}
else
{
left=GetHuffTreeDepth(&(*tree)->left)+1;
if(max_depth<left)
{
max_depth=left;
}
right=GetHuffTreeDepth(&(*tree)->right)+1;
if(max_depth<right)
{
max_depth=right;
}
return max_depth;
}
}
int GetWPL(HTree* tree,ElemType* elems)
{
int counter=GetLength(elems);
int start=0;
int result=0;
HuffCode temp;
while(start<counter)
{
temp=GetHuffCode(&(*tree),elems[start]);
result=result+temp.code_length*temp.weight;
start++;
}
return result;
}
HuffManTree_string.h
#ifndef HUFFMANTREE_DOUBLE_H_INCLUDED
#define HUFFMANTREE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node{
ElemType elem;
struct Node* left;
struct Node* right;
struct Node* parent;
int weight;
}HTNode,*HTree;
typedef struct
{
ElemType elem;
char* huffcode;
int code_length;
int weight;
}HuffCode;
extern "C" __declspec(dllexport) int InitHuffTree(HTree* tree,int weight,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffTree(HTree* tree);
extern "C" __declspec(dllexport) void PreTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void InTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) void PostTraverseTree(HTree* htnode);
extern "C" __declspec(dllexport) HTree CreateHuffTree(int* weights,ElemType* elems,int length);
extern "C" __declspec(dllexport) int GetLength(ElemType* elems);
extern "C" __declspec(dllexport) HTNode* GetNode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) HuffCode GetHuffCode(HTree* tree,ElemType elem);
extern "C" __declspec(dllexport) void DestroyHuffCode(HuffCode* code);
extern "C" __declspec(dllexport) int GetHuffTreeDepth(HTree* tree);
extern "C" __declspec(dllexport) int GetWPL(HTree* tree,ElemType* elems);
#endif // HUFFMANTREE_DOUBLE_H_INCLUDED
CircularDoubleLinkedList_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularDoubleLinkedList_char.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
L->instance->prior=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
free(L->instance);
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next==L->instance)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=L->instance)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance->next;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
int i=0;
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==0)
{
return p->prior->prior->elem;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==ListLength(L)-1)
{
return p->next->next->elem;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->next->prior=s;
s->prior=p;
p->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else
{
int i=0;
LNode *p=L->instance->next,*del;
while(i<index)
{
p=p->next;
i++;
}
del=p;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
LNode *p=L->instance->next;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
printf("%c\t",p->elem);
p=p->prior;
}
printf("\n");
}
CircularDoubleLinkedList_char.h
#ifndef CIRCULARDOUBLELINKEDLIST_CHAR_H_INCLUDED
#define CIRCULARDOUBLELINKEDLIST_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // CIRCULARDOUBLELINKEDLIST_CHAR_H_INCLUDED
CircularDoubleLinkedList_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularDoubleLinkedList_double.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
L->instance->prior=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
free(L->instance);
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next==L->instance)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=L->instance)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance->next;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
int i=0;
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==0)
{
return p->prior->prior->elem;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==ListLength(L)-1)
{
return p->next->next->elem;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->next->prior=s;
s->prior=p;
p->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else
{
int i=0;
LNode *p=L->instance->next,*del;
while(i<index)
{
p=p->next;
i++;
}
del=p;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
LNode *p=L->instance->next;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
printf("%f\t",p->elem);
p=p->prior;
}
printf("\n");
}
CircularDoubleLinkedList_double.h
#ifndef CIRCULARDOUBLELINKEDLIST_DOUBLE_H_INCLUDED
#define CIRCULARDOUBLELINKEDLIST_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // CIRCULARDOUBLELINKEDLIST_DOUBLE_H_INCLUDED
CircularDoubleLinkedList_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularDoubleLinkedList_int.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
L->instance->prior=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
free(L->instance);
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next==L->instance)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=L->instance)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance->next;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
int i=0;
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==0)
{
return p->prior->prior->elem;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==ListLength(L)-1)
{
return p->next->next->elem;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->next->prior=s;
s->prior=p;
p->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else
{
int i=0;
LNode *p=L->instance->next,*del;
while(i<index)
{
p=p->next;
i++;
}
del=p;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
LNode *p=L->instance->next;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
printf("%d\t",p->elem);
p=p->prior;
}
printf("\n");
}
CircularDoubleLinkedList_int.h
#ifndef CIRCULARDOUBLELINKEDLIST_INT_H_INCLUDED
#define CIRCULARDOUBLELINKEDLIST_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // CIRCULARDOUBLELINKEDLIST_INT_H_INCLUDED
CircularDoubleLinkedList_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularDoubleLinkedList_string.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
L->instance->prior=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
free(L->instance);
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance,*clc;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
clc=p;
p=clc->prior;
clc->prior->next=clc->next;
clc->next->prior=clc->prior;
free(clc);
clc=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next==L->instance)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=L->instance)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance->next;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
int i=0;
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==0)
{
return p->prior->prior->elem;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(LocateElem(L,elem)==ListLength(L)-1)
{
return p->next->next->elem;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->next->prior=s;
s->prior=p;
p->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
s->next->prior=s;
s->prior=p;
s->prior->next=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else
{
int i=0;
LNode *p=L->instance->next,*del;
while(i<index)
{
p=p->next;
i++;
}
del=p;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
LNode *p=L->instance->next;
while(p->next!=L->instance)
{
p=p->next;
}
while(p!=L->instance)
{
printf("%s\t",p->elem);
p=p->prior;
}
printf("\n");
}
CircularDoubleLinkedList_string.h
#ifndef CIRCULARDOUBLELINKEDLIST_STRING_H_INCLUDED
#define CIRCULARDOUBLELINKEDLIST_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // CIRCULARDOUBLELINKEDLIST_STRING_H_INCLUDED
CircularQueue_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularQueue_char.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
for(int i=0;i<size;i++)
{
S->base[i]=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
return S->base[S->front%S->size];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear%S->size==S->front%S->size&&S->tag==1)
{
return FALSE;
}
else
{
S->base[S->rear%S->size]=elem;
S->rear++;
S->tag=1;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front%S->size];
S->base[S->front%S->size]=NULL;
S->front++;
S->tag=0;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=0;
while(i<S->size)
{
if(S->base[i]==NULL)
{
i++;
continue;
}
else
{
printf("%c\t",S->base[i]);
i++;
}
}
printf("\n");
}
CircularQueue_char.h
#ifndef CIRCULARQUEUE_CHAR_H_INCLUDED
#define CIRCULARQUEUE_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
int tag;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // CIRCULARQUEUE_CHAR_H_INCLUDED
CircularQueue_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularQueue_double.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
for(int i=0;i<size;i++)
{
S->base[i]=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
return S->base[S->front%S->size];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear%S->size==S->front%S->size&&S->tag==1)
{
return FALSE;
}
else
{
S->base[S->rear%S->size]=elem;
S->rear++;
S->tag=1;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front%S->size];
S->base[S->front%S->size]=NULL;
S->front++;
S->tag=0;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=0;
while(i<S->size)
{
if(S->base[i]==NULL)
{
i++;
continue;
}
else
{
printf("%f\t",S->base[i]);
i++;
}
}
printf("\n");
}
CircularQueue_double.h
#ifndef CIRCULARQUEUE_DOUBLE_H_INCLUDED
#define CIRCULARQUEUE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
int tag;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // CIRCULARQUEUE_DOUBLE_H_INCLUDED
CircularQueue_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularQueue_int.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
for(int i=0;i<size;i++)
{
S->base[i]=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
return S->base[S->front%S->size];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear%S->size==S->front%S->size&&S->tag==1)
{
return FALSE;
}
else
{
S->base[S->rear%S->size]=elem;
S->rear++;
S->tag=1;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front%S->size];
S->base[S->front%S->size]=NULL;
S->front++;
S->tag=0;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=0;
while(i<S->size)
{
if(S->base[i]==NULL)
{
i++;
continue;
}
else
{
printf("%d\t",S->base[i]);
i++;
}
}
printf("\n");
}
CircularQueue_int.h
#ifndef CIRCULARQUEUE_INT_H_INCLUDED
#define CIRCULARQUEUE_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
int tag;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // CIRCULARQUEUE_INT_H_INCLUDED
CircularQueue_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularQueue_string.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
for(int i=0;i<size;i++)
{
S->base[i]=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
S->tag=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
return S->base[S->front%S->size];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear%S->size==S->front%S->size&&S->tag==1)
{
return FALSE;
}
else
{
S->base[S->rear%S->size]=elem;
S->rear++;
S->tag=1;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front%S->size==S->rear%S->size&&S->tag==0)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front%S->size];
S->base[S->front%S->size]=NULL;
S->front++;
S->tag=0;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=0;
while(i<S->size)
{
if(S->base[i]==NULL)
{
i++;
continue;
}
else
{
printf("%s\t",S->base[i]);
i++;
}
}
printf("\n");
}
CircularQueue_string.h
#ifndef CIRCULARQUEUE_STRING_H_INCLUDED
#define CIRCULARQUEUE_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
int tag;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // CIRCULARQUEUE_STRING_H_INCLUDED
CircularSingleLinkedList_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularSingleLinkedList_char.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(p!=L->instance)
{
p=p->next;
}
p->next=NULL;
p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=L->instance)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=L->instance)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p->next!=L->instance)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L))
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->next->elem==elem)
{
return p->elem;
}
else if(LocateElem(L,elem)==0)
{
while(p->next!=L->instance)
{
p=p->next;
}
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(ListLength(L)==(LocateElem(L,elem)+1))
{
return p->next->next->elem;
}
else
{
return p->next->elem;
}
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
int counter=0;
LNode* p=L->instance;
LNode* summon;
if(index<0||index>ListLength(L))
{
return FALSE;
}
else{
while(counter<index)
{
counter++;
p=p->next;
}
summon=(LNode*)malloc(sizeof(LNode));
if(summon==NULL)
{
return FALSE;
}
else
{
summon->elem=elem;
summon->next=p->next;
p->next=summon;
return TRUE;
}
}
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
CircularSingleLinkedList_char.h
#ifndef CIRCULARSINGLELINKEDLIST_CHAR_H_INCLUDED
#define CIRCULARSINGLELINKEDLIST_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // CIRCULARSINGLELINKEDLIST_CHAR_H_INCLUDED
CircularSingleLinkedList_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularSingleLinkedList_double.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(p!=L->instance)
{
p=p->next;
}
p->next=NULL;
p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=L->instance)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=L->instance)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p->next!=L->instance)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L))
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->next->elem==elem)
{
return p->elem;
}
else if(LocateElem(L,elem)==0)
{
while(p->next!=L->instance)
{
p=p->next;
}
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(ListLength(L)==(LocateElem(L,elem)+1))
{
return p->next->next->elem;
}
else
{
return p->next->elem;
}
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
int counter=0;
LNode* p=L->instance;
LNode* summon;
if(index<0||index>ListLength(L))
{
return FALSE;
}
else{
while(counter<index)
{
counter++;
p=p->next;
}
summon=(LNode*)malloc(sizeof(LNode));
if(summon==NULL)
{
return FALSE;
}
else
{
summon->elem=elem;
summon->next=p->next;
p->next=summon;
return TRUE;
}
}
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
CircularSingleLinkedList_double.h
#ifndef CIRCULARSINGLELINKEDLIST_DOUBLE_H_INCLUDED
#define CIRCULARSINGLELINKEDLIST_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // CIRCULARSINGLELINKEDLIST_DOUBLE_H_INCLUDED
CircularSingleLinkedList_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularSingleLinkedList_int.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(p!=L->instance)
{
p=p->next;
}
p->next=NULL;
p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=L->instance)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=L->instance)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p->next!=L->instance)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L))
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->next->elem==elem)
{
return p->elem;
}
else if(LocateElem(L,elem)==0)
{
while(p->next!=L->instance)
{
p=p->next;
}
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(ListLength(L)==(LocateElem(L,elem)+1))
{
return p->next->next->elem;
}
else
{
return p->next->elem;
}
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
int counter=0;
LNode* p=L->instance;
LNode* summon;
if(index<0||index>ListLength(L))
{
return FALSE;
}
else{
while(counter<index)
{
counter++;
p=p->next;
}
summon=(LNode*)malloc(sizeof(LNode));
if(summon==NULL)
{
return FALSE;
}
else
{
summon->elem=elem;
summon->next=p->next;
p->next=summon;
return TRUE;
}
}
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
CircularSingleLinkedList_int.h
#ifndef CIRCULARSINGLELINKEDLIST_INT_H_INCLUDED
#define CIRCULARSINGLELINKEDLIST_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // CIRCULARSINGLELINKEDLIST_INT_H_INCLUDED
CircularSingleLinkedList_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "CircularSingleLinkedList_string.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=L->instance;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(p!=L->instance)
{
p=p->next;
}
p->next=NULL;
p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=L->instance)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=L->instance)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p->next!=L->instance)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L))
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->next->elem==elem)
{
return p->elem;
}
else if(LocateElem(L,elem)==0)
{
while(p->next!=L->instance)
{
p=p->next;
}
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
if(p->elem==elem)
{
if(ListLength(L)==(LocateElem(L,elem)+1))
{
return p->next->next->elem;
}
else
{
return p->next->elem;
}
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
while(p->next!=L->instance)
{
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
int counter=0;
LNode* p=L->instance;
LNode* summon;
if(index<0||index>ListLength(L))
{
return FALSE;
}
else{
while(counter<index)
{
counter++;
p=p->next;
}
summon=(LNode*)malloc(sizeof(LNode));
if(summon==NULL)
{
return FALSE;
}
else
{
summon->elem=elem;
summon->next=p->next;
p->next=summon;
return TRUE;
}
}
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=L->instance)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
CircularSingleLinkedList_string.h
#ifndef CIRCULARSINGLELINKEDLIST_STRING_H_INCLUDED
#define CIRCULARSINGLELINKEDLIST_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // CIRCULARSINGLELINKEDLIST_STRING_H_INCLUDED
DoubleLinkedList_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedList_char.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
L->instance->prior=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
else
{
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
if(p->elem==elem&&LocateElem(L,elem)!=0)
{
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL&&LocateElem(L,elem)!=ListLength(L)-1)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
p->next=s;
s->prior=p;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
while(p->next!=NULL)
{
p=p->next;
}
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else if(index==ListLength(L)-1)
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
}
else
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
}
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
p=p->next;
}
while(p->prior!=NULL)
{
printf("%c\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedList_char.h
#ifndef DOUBLELINKEDLIST_CHAR_H_INCLUDED
#define DOUBLELINKEDLIST_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // DOUBLELINKEDLIST_CHAR_H_INCLUDED
DoubleLinkedList_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedList_double.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
L->instance->prior=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
else
{
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
if(p->elem==elem&&LocateElem(L,elem)!=0)
{
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL&&LocateElem(L,elem)!=ListLength(L)-1)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
p->next=s;
s->prior=p;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
while(p->next!=NULL)
{
p=p->next;
}
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else if(index==ListLength(L)-1)
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
}
else
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
}
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
p=p->next;
}
while(p->prior!=NULL)
{
printf("%f\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedList_double.h
#ifndef DOUBLELINKEDLIST_DOUBLE_H_INCLUDED
#define DOUBLELINKEDLIST_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // DOUBLELINKEDLIST_DOUBLE_H_INCLUDED
DoubleLinkedList_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedList_int.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
L->instance->prior=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
else
{
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
if(p->elem==elem&&LocateElem(L,elem)!=0)
{
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL&&LocateElem(L,elem)!=ListLength(L)-1)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
p->next=s;
s->prior=p;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
while(p->next!=NULL)
{
p=p->next;
}
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else if(index==ListLength(L)-1)
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
}
else
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
}
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
p=p->next;
}
while(p->prior!=NULL)
{
printf("%d\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedList_int.h
#ifndef INTLINKEDLIST_INT_H_INCLUDED
#define INTLINKEDLIST_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // INTLINKEDLIST_INT_H_INCLUDED
DoubleLinkedList_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedList_string.h"
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
L->instance->prior=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
else
{
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
if(p->elem==elem&&LocateElem(L,elem)!=0)
{
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL&&LocateElem(L,elem)!=ListLength(L)-1)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
int addFirst(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
p->next=s;
s->prior=p;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int addAfter(List *L,ElemType elem)
{
LNode *p=L->instance,*s;
while(p->next!=NULL)
{
p=p->next;
}
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
return TRUE;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=p->next;
s->prior=p;
p->next=s;
if(s->next!=NULL)
{
s->next->prior=s;
}
return TRUE;
}
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
else if(index==ListLength(L)-1)
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
}
else
{
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
del->prior->next=del->next;
del->next->prior=del->prior;
free(del);
del=NULL;
}
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
p=p->next;
}
while(p->prior!=NULL)
{
printf("%s\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedList_string.h
#ifndef STRINGLINKEDLIST_STRING_H_INCLUDED
#define STRINGLINKEDLIST_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) int addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // STRINGLINKEDLIST_STRING_H_INCLUDED
DoubleLinkedListWithoutHeadNode_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedListWithoutHeadNode_char.h"
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance;
while(i<index)
{
p=p->next;
i++;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->prior==NULL)
{
break;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->next==NULL)
{
break;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
s->prior=NULL;
L->instance->prior=s;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
s->prior=p;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else if(index==ListLength(L))
{
addAfter(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=1;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
s->prior=p;
p->next=s;
s->next->prior=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
int i=0;
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
if(ListLength(L)==1)
{
free(L->instance);
L->instance=NULL;
return TRUE;
}
LNode *del=L->instance;
L->instance=del->next;
del->next=NULL;
del->elem=NULL;
free(del);
del=NULL;
L->instance->prior=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
while(i<index&&p->next!=NULL)
{
p=p->next;
i++;
}
del=p;
if(del->next!=NULL)
{
del->next->prior=del->prior;
del->prior->next=del->next;
free(del);
del=NULL;
return TRUE;
}
else
{
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance;
while(p->next!=NULL)
{
p=p->next;
}
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedListWithoutHeadNode_char.h
#ifndef DOUBLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
#define DOUBLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // DOUBLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
DoubleLinkedListWithoutHeadNode_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedListWithoutHeadNode_double.h"
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance;
while(i<index)
{
p=p->next;
i++;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->prior==NULL)
{
break;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->next==NULL)
{
break;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
s->prior=NULL;
L->instance->prior=s;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
s->prior=p;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else if(index==ListLength(L))
{
addAfter(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=1;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
s->prior=p;
p->next=s;
s->next->prior=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
int i=0;
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
if(ListLength(L)==1)
{
free(L->instance);
L->instance=NULL;
return TRUE;
}
LNode *del=L->instance;
L->instance=del->next;
del->next=NULL;
del->elem=NULL;
free(del);
del=NULL;
L->instance->prior=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
while(i<index&&p->next!=NULL)
{
p=p->next;
i++;
}
del=p;
if(del->next!=NULL)
{
del->next->prior=del->prior;
del->prior->next=del->next;
free(del);
del=NULL;
return TRUE;
}
else
{
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance;
while(p->next!=NULL)
{
p=p->next;
}
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedListWithoutHeadNode_double.h
#ifndef DOUBLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
#define DOUBLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // DOUBLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
DoubleLinkedListWithoutHeadNode_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedListWithoutHeadNode_int.h"
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance;
while(i<index)
{
p=p->next;
i++;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->prior==NULL)
{
break;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->next==NULL)
{
break;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
s->prior=NULL;
L->instance->prior=s;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
s->prior=p;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else if(index==ListLength(L))
{
addAfter(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=1;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
s->prior=p;
p->next=s;
s->next->prior=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
int i=0;
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
if(ListLength(L)==1)
{
free(L->instance);
L->instance=NULL;
return TRUE;
}
LNode *del=L->instance;
L->instance=del->next;
del->next=NULL;
del->elem=NULL;
free(del);
del=NULL;
L->instance->prior=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
while(i<index&&p->next!=NULL)
{
p=p->next;
i++;
}
del=p;
if(del->next!=NULL)
{
del->next->prior=del->prior;
del->prior->next=del->next;
free(del);
del=NULL;
return TRUE;
}
else
{
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance;
while(p->next!=NULL)
{
p=p->next;
}
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedListWithoutHeadNode_int.h
#ifndef INTLINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
#define INTLINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // INTLINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
DoubleLinkedListWithoutHeadNode_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DoubleLinkedListWithoutHeadNode_string.h"
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
int i=0;
LNode *p=L->instance;
while(i<index)
{
p=p->next;
i++;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(p!=NULL)
{
if(p->elem==elem)
{
return i;
}
i++;
p=p->next;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->prior==NULL)
{
break;
}
return p->prior->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p!=NULL)
{
if(p->elem==elem)
{
if(p->next==NULL)
{
break;
}
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
s->prior=NULL;
L->instance->prior=s;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
L->instance->prior=NULL;
}
else
{
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
s->prior=p;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
else if(index==0)
{
addFirst(L,elem);
return TRUE;
}
else if(index==ListLength(L))
{
addAfter(L,elem);
return TRUE;
}
else
{
LNode *p=L->instance,*s;
int i=1;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
s->prior=p;
p->next=s;
s->next->prior=s;
return TRUE;
}
}
int ListDelete(List *L,int index)
{
int i=0;
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
if(ListLength(L)==1)
{
free(L->instance);
L->instance=NULL;
return TRUE;
}
LNode *del=L->instance;
L->instance=del->next;
del->next=NULL;
del->elem=NULL;
free(del);
del=NULL;
L->instance->prior=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
while(i<index&&p->next!=NULL)
{
p=p->next;
i++;
}
del=p;
if(del->next!=NULL)
{
del->next->prior=del->prior;
del->prior->next=del->next;
free(del);
del=NULL;
return TRUE;
}
else
{
del->prior->next=NULL;
del->prior=NULL;
free(del);
del=NULL;
return TRUE;
}
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
void TraverseListByReverseOrder(List *L)
{
if(ListEmpty(L)==TRUE)
{
printf("\n");
}
else
{
LNode *p=L->instance;
while(p->next!=NULL)
{
p=p->next;
}
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->prior;
}
printf("\n");
}
}
DoubleLinkedListWithoutHeadNode_string.h
#ifndef STRINGLINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
#define STRINGLINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType elem;
struct Node *prior;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
extern "C" __declspec(dllexport) void TraverseListByReverseOrder(List *L);
#endif // STRINGLINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
LinkedArray_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedArray_char.h"
int InitArray(CrossList *M,int rows,int cols,int nums)
{
M->rows=rows;
M->cols=cols;
M->nums=nums;
M->counter=0;
M->rhead=(QLNode**)malloc(sizeof(QLNode*)*(rows+1));
M->chead=(QLNode**)malloc(sizeof(QLNode*)*(cols+1));
if(M->chead==NULL||M->rhead==NULL)
{
return FALSE;
}
else
{
for(int i=0;i<=rows;i++)
{
M->rhead[i]=NULL;
}
for(int i=0;i<=cols;i++)
{
M->chead[i]=NULL;
}
return TRUE;
}
}
int Assign(CrossList *M,int row,int col,ElemType elem)
{
if(row>=M->rows||col>=M->cols||row<0||col<0)
{
return FALSE;
}
else if(M->counter<M->nums)
{
QLNode* check=M->rhead[row];
while(check!=NULL)
{
if(check->col==col)
{
return FALSE;
}
check=check->right;
}
M->counter++;
QLNode *p=(QLNode*)malloc(sizeof(QLNode));
if(p==NULL)
{
return FALSE;
}
p->col=col;
p->row=row;
p->elem=elem;
if(M->rhead[row]==NULL||M->rhead[row]->col>col)
{
p->right=M->rhead[row];
M->rhead[row]=p;
}
else
{
QLNode *q=M->rhead[row];
while(q!=NULL)
{
if((q->right!=NULL&&q->right->col>col)||q->right==NULL)
{
p->right=q->right;
q->right=p;
break;
}
q=q->right;
}
}
if(M->chead[col]==NULL||M->chead[col]->row>row)
{
p->down=M->chead[col];
M->chead[col]=p;
}
else
{
QLNode *q=M->chead[col];
while(q!=NULL)
{
if((q->down!=NULL&&q->down->row>row)||q->down==NULL)
{
p->down=q->down;
q->down=p;
break;
}
q=q->down;
}
}
return TRUE;
}
else
{
return FALSE;
}
}
ElemType Value(CrossList *M,int i,int j)
{
QLNode *p=M->rhead[i];
while(p!=NULL)
{
if(p->col==j)
{
return p->elem;
}
p=p->right;
}
return NULL;
}
void CreatMat(CrossList *M)
{
int i,j;
for(i=0;i<M->rows;i++)
{
if(M->rhead[i]==NULL)
{
for(j=0;j<M->cols;j++)
{
printf("%c\t",NULL);
}
printf("\n");
}
else
{
int n=0;
QLNode *p=M->rhead[i];
while(n<M->cols)
{
if(p==NULL||(n<p->col))
{
printf("%c\t",NULL);
}
else
{
printf("%c\t",p->elem);
p=p->right;
}
n++;
}
printf("\n");
}
}
}
void DestroyArray(CrossList *M)
{
for(int i=0;i<M->rows;i++)
{
QLNode *p=M->rhead[i],*tmp;
for(int j=0;j<M->cols;j++)
{
if(p==NULL||p->col>j)
{
break;
}
else
{
tmp=p;
p=tmp->right;
free(tmp);
tmp=NULL;
}
}
}
M=NULL;
}
int SetValue(CrossList* M,int i,int j,ElemType elem)
{
QLNode* p=NULL;
int counter=0;
if(i>M->rows||j>M->cols)
{
return FALSE;
}else
{
p=M->rhead[i];
while(p!=NULL)
{
if(p->col>j)
{
return FALSE;
}
else if(p->col==j)
{
p->elem=elem;
return TRUE;
}
p=p->right;
}
return FALSE;
}
}
int DeleteValue(CrossList* M,int i,int j)
{
if(i>=M->rows||j>=M->cols||i<0||j<0)
{
return FALSE;
}
else if(i==0&&j>0)
{
QLNode* p=M->rhead[i];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
M->chead[j]=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(j==0&&i>0)
{
QLNode* p=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->down;
}
if(temp==NULL)
{
return FALSE;
}
{
p=M->chead[j];
while(p->down!=temp)
{
p=p->down;
}
p->down=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(i==0&&j==0)
{
QLNode* temp=M->chead[i];
M->chead[j]=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
return TRUE;
}
else
{
QLNode* p=M->rhead[i];
QLNode* q=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
}
temp=NULL;
while(q!=NULL)
{
if(q->col==j&&q->row==i)
{
temp=q;
break;
}
q=q->down;
}
if(temp==NULL)
{
return FALSE;
}
else
{
q=M->chead[j];
while(q->down!=temp)
{
q=q->down;
}
q->down=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
}
LinkedArray_char.h
#ifndef LINKEDARRAY_CHAR_H_INCLUDED
#define LINKEDARRAY_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct node
{
int row,col;
ElemType elem;
struct node *right,*down;
}QLNode;
typedef struct
{
QLNode **rhead,**chead;
int rows,cols,nums,counter;
}CrossList;
extern "C" __declspec(dllexport) int InitArray(CrossList *M,int rows,int cols,int nums);
extern "C" __declspec(dllexport) int Assign(CrossList *M,int row,int col,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(CrossList *M,int i,int j);
extern "C" __declspec(dllexport) void CreatMat(CrossList *M);
extern "C" __declspec(dllexport) void DestroyArray(CrossList *M);
extern "C" __declspec(dllexport) int SetValue(CrossList* M,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) int DeleteValue(CrossList* M,int i,int j);
#endif // LINKEDARRAY_CHAR_H_INCLUDED
LinkedArray_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedArray_double.h"
int InitArray(CrossList *M,int rows,int cols,int nums)
{
M->rows=rows;
M->cols=cols;
M->nums=nums;
M->counter=0;
M->rhead=(QLNode**)malloc(sizeof(QLNode*)*(rows+1));
M->chead=(QLNode**)malloc(sizeof(QLNode*)*(cols+1));
if(M->chead==NULL||M->rhead==NULL)
{
return FALSE;
}
else
{
for(int i=0;i<=rows;i++)
{
M->rhead[i]=NULL;
}
for(int i=0;i<=cols;i++)
{
M->chead[i]=NULL;
}
return TRUE;
}
}
int Assign(CrossList *M,int row,int col,ElemType elem)
{
if(row>=M->rows||col>=M->cols||row<0||col<0)
{
return FALSE;
}
else if(M->counter<M->nums)
{
QLNode* check=M->rhead[row];
while(check!=NULL)
{
if(check->col==col)
{
return FALSE;
}
check=check->right;
}
M->counter++;
QLNode *p=(QLNode*)malloc(sizeof(QLNode));
if(p==NULL)
{
return FALSE;
}
p->col=col;
p->row=row;
p->elem=elem;
if(M->rhead[row]==NULL||M->rhead[row]->col>col)
{
p->right=M->rhead[row];
M->rhead[row]=p;
}
else
{
QLNode *q=M->rhead[row];
while(q!=NULL)
{
if((q->right!=NULL&&q->right->col>col)||q->right==NULL)
{
p->right=q->right;
q->right=p;
break;
}
q=q->right;
}
}
if(M->chead[col]==NULL||M->chead[col]->row>row)
{
p->down=M->chead[col];
M->chead[col]=p;
}
else
{
QLNode *q=M->chead[col];
while(q!=NULL)
{
if((q->down!=NULL&&q->down->row>row)||q->down==NULL)
{
p->down=q->down;
q->down=p;
break;
}
q=q->down;
}
}
return TRUE;
}
else
{
return FALSE;
}
}
ElemType Value(CrossList *M,int i,int j)
{
QLNode *p=M->rhead[i];
while(p!=NULL)
{
if(p->col==j)
{
return p->elem;
}
p=p->right;
}
return NULL;
}
void CreatMat(CrossList *M)
{
int i,j;
for(i=0;i<M->rows;i++)
{
if(M->rhead[i]==NULL)
{
for(j=0;j<M->cols;j++)
{
printf("%f\t",NULL);
}
printf("\n");
}
else
{
int n=0;
QLNode *p=M->rhead[i];
while(n<M->cols)
{
if(p==NULL||(n<p->col))
{
printf("%f\t",NULL);
}
else
{
printf("%f\t",p->elem);
p=p->right;
}
n++;
}
printf("\n");
}
}
}
void DestroyArray(CrossList *M)
{
for(int i=0;i<M->rows;i++)
{
QLNode *p=M->rhead[i],*tmp;
for(int j=0;j<M->cols;j++)
{
if(p==NULL||p->col>j)
{
break;
}
else
{
tmp=p;
p=tmp->right;
free(tmp);
tmp=NULL;
}
}
}
M=NULL;
}
int SetValue(CrossList* M,int i,int j,ElemType elem)
{
QLNode* p=NULL;
int counter=0;
if(i>M->rows||j>M->cols)
{
return FALSE;
}else
{
p=M->rhead[i];
while(p!=NULL)
{
if(p->col>j)
{
return FALSE;
}
else if(p->col==j)
{
p->elem=elem;
return TRUE;
}
p=p->right;
}
return FALSE;
}
}
int DeleteValue(CrossList* M,int i,int j)
{
if(i>=M->rows||j>=M->cols||i<0||j<0)
{
return FALSE;
}
else if(i==0&&j>0)
{
QLNode* p=M->rhead[i];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
M->chead[j]=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(j==0&&i>0)
{
QLNode* p=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->down;
}
if(temp==NULL)
{
return FALSE;
}
{
p=M->chead[j];
while(p->down!=temp)
{
p=p->down;
}
p->down=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(i==0&&j==0)
{
QLNode* temp=M->chead[i];
M->chead[j]=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
return TRUE;
}
else
{
QLNode* p=M->rhead[i];
QLNode* q=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
}
temp=NULL;
while(q!=NULL)
{
if(q->col==j&&q->row==i)
{
temp=q;
break;
}
q=q->down;
}
if(temp==NULL)
{
return FALSE;
}
else
{
q=M->chead[j];
while(q->down!=temp)
{
q=q->down;
}
q->down=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
}
LinkedArray_double.h
#ifndef LINKEDARRAY_DOUBLE_H_INCLUDED
#define LINKEDARRAY_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct node
{
int row,col;
ElemType elem;
struct node *right,*down;
}QLNode;
typedef struct
{
QLNode **rhead,**chead;
int rows,cols,nums,counter;
}CrossList;
extern "C" __declspec(dllexport) int InitArray(CrossList *M,int rows,int cols,int nums);
extern "C" __declspec(dllexport) int Assign(CrossList *M,int row,int col,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(CrossList *M,int i,int j);
extern "C" __declspec(dllexport) void CreatMat(CrossList *M);
extern "C" __declspec(dllexport) void DestroyArray(CrossList *M);
extern "C" __declspec(dllexport) int SetValue(CrossList* M,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) int DeleteValue(CrossList* M,int i,int j);
#endif // LINKEDARRAY_DOUBLE_H_INCLUDED
LinkedArray_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedArray_int.h"
int InitArray(CrossList *M,int rows,int cols,int nums)
{
M->rows=rows;
M->cols=cols;
M->nums=nums;
M->counter=0;
M->rhead=(QLNode**)malloc(sizeof(QLNode*)*(rows+1));
M->chead=(QLNode**)malloc(sizeof(QLNode*)*(cols+1));
if(M->chead==NULL||M->rhead==NULL)
{
return FALSE;
}
else
{
for(int i=0;i<=rows;i++)
{
M->rhead[i]=NULL;
}
for(int i=0;i<=cols;i++)
{
M->chead[i]=NULL;
}
return TRUE;
}
}
int Assign(CrossList *M,int row,int col,ElemType elem)
{
if(row>=M->rows||col>=M->cols||row<0||col<0)
{
return FALSE;
}
else if(M->counter<M->nums)
{
QLNode* check=M->rhead[row];
while(check!=NULL)
{
if(check->col==col)
{
return FALSE;
}
check=check->right;
}
M->counter++;
QLNode *p=(QLNode*)malloc(sizeof(QLNode));
if(p==NULL)
{
return FALSE;
}
p->col=col;
p->row=row;
p->elem=elem;
if(M->rhead[row]==NULL||M->rhead[row]->col>col)
{
p->right=M->rhead[row];
M->rhead[row]=p;
}
else
{
QLNode *q=M->rhead[row];
while(q!=NULL)
{
if((q->right!=NULL&&q->right->col>col)||q->right==NULL)
{
p->right=q->right;
q->right=p;
break;
}
q=q->right;
}
}
if(M->chead[col]==NULL||M->chead[col]->row>row)
{
p->down=M->chead[col];
M->chead[col]=p;
}
else
{
QLNode *q=M->chead[col];
while(q!=NULL)
{
if((q->down!=NULL&&q->down->row>row)||q->down==NULL)
{
p->down=q->down;
q->down=p;
break;
}
q=q->down;
}
}
return TRUE;
}
else
{
return FALSE;
}
}
ElemType Value(CrossList *M,int i,int j)
{
QLNode *p=M->rhead[i];
while(p!=NULL)
{
if(p->col==j)
{
return p->elem;
}
p=p->right;
}
return NULL;
}
void CreatMat(CrossList *M)
{
int i,j;
for(i=0;i<M->rows;i++)
{
if(M->rhead[i]==NULL)
{
for(j=0;j<M->cols;j++)
{
printf("%d\t",NULL);
}
printf("\n");
}
else
{
int n=0;
QLNode *p=M->rhead[i];
while(n<M->cols)
{
if(p==NULL||(n<p->col))
{
printf("%d\t",NULL);
}
else
{
printf("%d\t",p->elem);
p=p->right;
}
n++;
}
printf("\n");
}
}
}
void DestroyArray(CrossList *M)
{
for(int i=0;i<M->rows;i++)
{
QLNode *p=M->rhead[i],*tmp;
for(int j=0;j<M->cols;j++)
{
if(p==NULL||p->col>j)
{
break;
}
else
{
tmp=p;
p=tmp->right;
free(tmp);
tmp=NULL;
}
}
}
M=NULL;
}
int SetValue(CrossList* M,int i,int j,ElemType elem)
{
QLNode* p=NULL;
int counter=0;
if(i>M->rows||j>M->cols)
{
return FALSE;
}else
{
p=M->rhead[i];
while(p!=NULL)
{
if(p->col>j)
{
return FALSE;
}
else if(p->col==j)
{
p->elem=elem;
return TRUE;
}
p=p->right;
}
return FALSE;
}
}
int DeleteValue(CrossList* M,int i,int j)
{
if(i>=M->rows||j>=M->cols||i<0||j<0)
{
return FALSE;
}
else if(i==0&&j>0)
{
QLNode* p=M->rhead[i];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
M->chead[j]=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(j==0&&i>0)
{
QLNode* p=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->down;
}
if(temp==NULL)
{
return FALSE;
}
{
p=M->chead[j];
while(p->down!=temp)
{
p=p->down;
}
p->down=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(i==0&&j==0)
{
QLNode* temp=M->chead[i];
M->chead[j]=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
return TRUE;
}
else
{
QLNode* p=M->rhead[i];
QLNode* q=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
}
temp=NULL;
while(q!=NULL)
{
if(q->col==j&&q->row==i)
{
temp=q;
break;
}
q=q->down;
}
if(temp==NULL)
{
return FALSE;
}
else
{
q=M->chead[j];
while(q->down!=temp)
{
q=q->down;
}
q->down=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
}
LinkedArray_int.h
#ifndef LINKEDARRAY_INT_H_INCLUDED
#define LINKEDARRAY_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct node
{
int row,col;
ElemType elem;
struct node *right,*down;
}QLNode;
typedef struct
{
QLNode **rhead,**chead;
int rows,cols,nums,counter;
}CrossList;
extern "C" __declspec(dllexport) int InitArray(CrossList *M,int rows,int cols,int nums);
extern "C" __declspec(dllexport) int Assign(CrossList *M,int row,int col,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(CrossList *M,int i,int j);
extern "C" __declspec(dllexport) void CreatMat(CrossList *M);
extern "C" __declspec(dllexport) void DestroyArray(CrossList *M);
extern "C" __declspec(dllexport) int SetValue(CrossList* M,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) int DeleteValue(CrossList* M,int i,int j);
#endif // LINKEDARRAY_INT_H_INCLUDED
LinkedArray_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedArray_string.h"
int InitArray(CrossList *M,int rows,int cols,int nums)
{
M->rows=rows;
M->cols=cols;
M->nums=nums;
M->counter=0;
M->rhead=(QLNode**)malloc(sizeof(QLNode*)*(rows+1));
M->chead=(QLNode**)malloc(sizeof(QLNode*)*(cols+1));
if(M->chead==NULL||M->rhead==NULL)
{
return FALSE;
}
else
{
for(int i=0;i<=rows;i++)
{
M->rhead[i]=NULL;
}
for(int i=0;i<=cols;i++)
{
M->chead[i]=NULL;
}
return TRUE;
}
}
int Assign(CrossList *M,int row,int col,ElemType elem)
{
if(row>=M->rows||col>=M->cols||row<0||col<0)
{
return FALSE;
}
else if(M->counter<M->nums)
{
QLNode* check=M->rhead[row];
while(check!=NULL)
{
if(check->col==col)
{
return FALSE;
}
check=check->right;
}
M->counter++;
QLNode *p=(QLNode*)malloc(sizeof(QLNode));
if(p==NULL)
{
return FALSE;
}
p->col=col;
p->row=row;
p->elem=elem;
if(M->rhead[row]==NULL||M->rhead[row]->col>col)
{
p->right=M->rhead[row];
M->rhead[row]=p;
}
else
{
QLNode *q=M->rhead[row];
while(q!=NULL)
{
if((q->right!=NULL&&q->right->col>col)||q->right==NULL)
{
p->right=q->right;
q->right=p;
break;
}
q=q->right;
}
}
if(M->chead[col]==NULL||M->chead[col]->row>row)
{
p->down=M->chead[col];
M->chead[col]=p;
}
else
{
QLNode *q=M->chead[col];
while(q!=NULL)
{
if((q->down!=NULL&&q->down->row>row)||q->down==NULL)
{
p->down=q->down;
q->down=p;
break;
}
q=q->down;
}
}
return TRUE;
}
else
{
return FALSE;
}
}
ElemType Value(CrossList *M,int i,int j)
{
QLNode *p=M->rhead[i];
while(p!=NULL)
{
if(p->col==j)
{
return p->elem;
}
p=p->right;
}
return NULL;
}
void CreatMat(CrossList *M)
{
int i,j;
for(i=0;i<M->rows;i++)
{
if(M->rhead[i]==NULL)
{
for(j=0;j<M->cols;j++)
{
printf("%s\t",NULL);
}
printf("\n");
}
else
{
int n=0;
QLNode *p=M->rhead[i];
while(n<M->cols)
{
if(p==NULL||(n<p->col))
{
printf("%s\t",NULL);
}
else
{
printf("%s\t",p->elem);
p=p->right;
}
n++;
}
printf("\n");
}
}
}
void DestroyArray(CrossList *M)
{
for(int i=0;i<M->rows;i++)
{
QLNode *p=M->rhead[i],*tmp;
for(int j=0;j<M->cols;j++)
{
if(p==NULL||p->col>j)
{
break;
}
else
{
tmp=p;
p=tmp->right;
free(tmp);
tmp=NULL;
}
}
}
M=NULL;
}
int SetValue(CrossList* M,int i,int j,ElemType elem)
{
QLNode* p=NULL;
int counter=0;
if(i>M->rows||j>M->cols)
{
return FALSE;
}else
{
p=M->rhead[i];
while(p!=NULL)
{
if(p->col>j)
{
return FALSE;
}
else if(p->col==j)
{
p->elem=elem;
return TRUE;
}
p=p->right;
}
return FALSE;
}
}
int DeleteValue(CrossList* M,int i,int j)
{
if(i>=M->rows||j>=M->cols||i<0||j<0)
{
return FALSE;
}
else if(i==0&&j>0)
{
QLNode* p=M->rhead[i];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
M->chead[j]=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(j==0&&i>0)
{
QLNode* p=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->down;
}
if(temp==NULL)
{
return FALSE;
}
{
p=M->chead[j];
while(p->down!=temp)
{
p=p->down;
}
p->down=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
}
return TRUE;
}
else if(i==0&&j==0)
{
QLNode* temp=M->chead[i];
M->chead[j]=temp->down;
M->rhead[i]=temp->right;
free(temp);
temp=NULL;
return TRUE;
}
else
{
QLNode* p=M->rhead[i];
QLNode* q=M->chead[j];
QLNode* temp=NULL;
while(p!=NULL)
{
if(p->col==j&&p->row==i)
{
temp=p;
break;
}
p=p->right;
}
if(temp==NULL)
{
return FALSE;
}
else
{
p=M->rhead[i];
while(p->right!=temp)
{
p=p->right;
}
p->right=temp->right;
}
temp=NULL;
while(q!=NULL)
{
if(q->col==j&&q->row==i)
{
temp=q;
break;
}
q=q->down;
}
if(temp==NULL)
{
return FALSE;
}
else
{
q=M->chead[j];
while(q->down!=temp)
{
q=q->down;
}
q->down=temp->down;
free(temp);
temp=NULL;
}
return TRUE;
}
}
LinkedArray_string.h
#ifndef LINKEDARRAY_STRING_H_INCLUDED
#define LINKEDARRAY_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct node
{
int row,col;
ElemType elem;
struct node *right,*down;
}QLNode;
typedef struct
{
QLNode **rhead,**chead;
int rows,cols,nums,counter;
}CrossList;
extern "C" __declspec(dllexport) int InitArray(CrossList *M,int rows,int cols,int nums);
extern "C" __declspec(dllexport) int Assign(CrossList *M,int row,int col,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(CrossList *M,int i,int j);
extern "C" __declspec(dllexport) void CreatMat(CrossList *M);
extern "C" __declspec(dllexport) void DestroyArray(CrossList *M);
extern "C" __declspec(dllexport) int SetValue(CrossList* M,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) int DeleteValue(CrossList* M,int i,int j);
#endif // LINKEDARRAY_STRING_H_INCLUDED
LinkedQueue_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue_char.h"
int InitQueue(LinkedQueue *Q)
{
Q->front=(QNode*)malloc(sizeof(QNode));
if(Q->front!=NULL)
{
Q->front->next=NULL;
Q->rear=Q->front;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(LinkedQueue *Q)
{
QNode *p=(Q->front);
while(Q->front!=NULL)
{
p=Q->front;
Q->front=p->next;
free(p);
p=NULL;
}
Q->front=NULL;
Q->rear=NULL;
}
void ClearQueue(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(Q->front->next!=NULL)
{
p=Q->front->next;
Q->front->next=p->next;
free(p);
p=NULL;
}
Q->rear=Q->front;
}
int QueueEmpty(LinkedQueue *Q)
{
if(Q->front->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(LinkedQueue *Q)
{
QNode *p=Q->front;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetHead(LinkedQueue *Q)
{
if(Q->front==Q->rear)
{
return NULL;
}
else
{
return Q->front->next->elem;
}
}
int EnQueue(LinkedQueue *Q,ElemType elem)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=Q->rear->next;
Q->rear->next=s;
Q->rear=s;
return TRUE;
}
}
ElemType DeQueue(LinkedQueue *Q)
{
ElemType result=NULL;
QNode* del=NULL;
if(Q->front==Q->rear)
{
return NULL;
}
else
{
if(Q->front->next==Q->rear)
{
result=Q->rear->elem;
free(Q->rear);
Q->rear=Q->front;
Q->front->next=NULL;
return result;
}
else
{
del=Q->front->next;
result=del->elem;
Q->front->next=del->next;
free(del);
del=NULL;
return result;
}
}
}
void QueueTraverse(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedQueue_char.h
#ifndef LINKEDQUEUE_CHAR_H_INCLUDED
#define LINKEDQUEUE_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}QNode;
typedef struct
{
QNode* front;
QNode* rear;
}LinkedQueue;
extern "C" __declspec(dllexport) int InitQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void DestroyQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void ClearQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueEmpty(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueLength(LinkedQueue *Q);
extern "C" __declspec(dllexport) ElemType GetHead(LinkedQueue *Q);
extern "C" __declspec(dllexport) int EnQueue(LinkedQueue *Q,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void QueueTraverse(LinkedQueue *Q);
#endif // LINKEDQUEUE_CHAR_H_INCLUDED
LinkedQueue_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue_double.h"
int InitQueue(LinkedQueue *Q)
{
Q->front=(QNode*)malloc(sizeof(QNode));
if(Q->front!=NULL)
{
Q->front->next=NULL;
Q->rear=Q->front;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(LinkedQueue *Q)
{
QNode *p=(Q->front);
while(Q->front!=NULL)
{
p=Q->front;
Q->front=p->next;
free(p);
p=NULL;
}
Q->front=NULL;
Q->rear=NULL;
}
void ClearQueue(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(Q->front->next!=NULL)
{
p=Q->front->next;
Q->front->next=p->next;
free(p);
p=NULL;
}
Q->rear=Q->front;
}
int QueueEmpty(LinkedQueue *Q)
{
if(Q->front->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(LinkedQueue *Q)
{
QNode *p=Q->front;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetHead(LinkedQueue *Q)
{
if(Q->front==Q->rear)
{
return NULL;
}
else
{
return Q->front->next->elem;
}
}
int EnQueue(LinkedQueue *Q,ElemType elem)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=Q->rear->next;
Q->rear->next=s;
Q->rear=s;
return TRUE;
}
}
ElemType DeQueue(LinkedQueue *Q)
{
ElemType result=NULL;
QNode* del=NULL;
if(Q->front==Q->rear)
{
return NULL;
}
else
{
if(Q->front->next==Q->rear)
{
result=Q->rear->elem;
free(Q->rear);
Q->rear=Q->front;
Q->front->next=NULL;
return result;
}
else
{
del=Q->front->next;
result=del->elem;
Q->front->next=del->next;
free(del);
del=NULL;
return result;
}
}
}
void QueueTraverse(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedQueue_double.h
#ifndef LINKEDQUEUE_DOUBLE_H_INCLUDED
#define LINKEDQUEUE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}QNode;
typedef struct
{
QNode* front;
QNode* rear;
}LinkedQueue;
extern "C" __declspec(dllexport) int InitQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void DestroyQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void ClearQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueEmpty(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueLength(LinkedQueue *Q);
extern "C" __declspec(dllexport) ElemType GetHead(LinkedQueue *Q);
extern "C" __declspec(dllexport) int EnQueue(LinkedQueue *Q,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void QueueTraverse(LinkedQueue *Q);
#endif // LINKEDQUEUE_DOUBLE_H_INCLUDED
LinkedQueue_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue_int.h"
int InitQueue(LinkedQueue *Q)
{
Q->front=(QNode*)malloc(sizeof(QNode));
if(Q->front!=NULL)
{
Q->front->next=NULL;
Q->rear=Q->front;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(LinkedQueue *Q)
{
QNode *p=(Q->front);
while(Q->front!=NULL)
{
p=Q->front;
Q->front=p->next;
free(p);
p=NULL;
}
Q->front=NULL;
Q->rear=NULL;
}
void ClearQueue(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(Q->front->next!=NULL)
{
p=Q->front->next;
Q->front->next=p->next;
free(p);
p=NULL;
}
Q->rear=Q->front;
}
int QueueEmpty(LinkedQueue *Q)
{
if(Q->front->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(LinkedQueue *Q)
{
QNode *p=Q->front;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetHead(LinkedQueue *Q)
{
if(Q->front==Q->rear)
{
return NULL;
}
else
{
return Q->front->next->elem;
}
}
int EnQueue(LinkedQueue *Q,ElemType elem)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=Q->rear->next;
Q->rear->next=s;
Q->rear=s;
return TRUE;
}
}
ElemType DeQueue(LinkedQueue *Q)
{
ElemType result=NULL;
QNode* del=NULL;
if(Q->front==Q->rear)
{
return NULL;
}
else
{
if(Q->front->next==Q->rear)
{
result=Q->rear->elem;
free(Q->rear);
Q->rear=Q->front;
Q->front->next=NULL;
return result;
}
else
{
del=Q->front->next;
result=del->elem;
Q->front->next=del->next;
free(del);
del=NULL;
return result;
}
}
}
void QueueTraverse(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedQueue_int.h
#ifndef LINKEDQUEUE_INT_H_INCLUDED
#define LINKEDQUEUE_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}QNode;
typedef struct
{
QNode* front;
QNode* rear;
}LinkedQueue;
extern "C" __declspec(dllexport) int InitQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void DestroyQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void ClearQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueEmpty(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueLength(LinkedQueue *Q);
extern "C" __declspec(dllexport) ElemType GetHead(LinkedQueue *Q);
extern "C" __declspec(dllexport) int EnQueue(LinkedQueue *Q,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void QueueTraverse(LinkedQueue *Q);
#endif // LINKEDQUEUE_INT_H_INCLUDED
LinkedQueue_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedQueue_string.h"
int InitQueue(LinkedQueue *Q)
{
Q->front=(QNode*)malloc(sizeof(QNode));
if(Q->front!=NULL)
{
Q->front->next=NULL;
Q->rear=Q->front;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(LinkedQueue *Q)
{
QNode *p=(Q->front);
while(Q->front!=NULL)
{
p=Q->front;
Q->front=p->next;
free(p);
p=NULL;
}
Q->front=NULL;
Q->rear=NULL;
}
void ClearQueue(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(Q->front->next!=NULL)
{
p=Q->front->next;
Q->front->next=p->next;
free(p);
p=NULL;
}
Q->rear=Q->front;
}
int QueueEmpty(LinkedQueue *Q)
{
if(Q->front->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(LinkedQueue *Q)
{
QNode *p=Q->front;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetHead(LinkedQueue *Q)
{
if(Q->front==Q->rear)
{
return NULL;
}
else
{
return Q->front->next->elem;
}
}
int EnQueue(LinkedQueue *Q,ElemType elem)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
if(s==NULL)
{
return FALSE;
}
else
{
s->elem=elem;
s->next=Q->rear->next;
Q->rear->next=s;
Q->rear=s;
return TRUE;
}
}
ElemType DeQueue(LinkedQueue *Q)
{
ElemType result=NULL;
QNode* del=NULL;
if(Q->front==Q->rear)
{
return NULL;
}
else
{
if(Q->front->next==Q->rear)
{
result=Q->rear->elem;
free(Q->rear);
Q->rear=Q->front;
Q->front->next=NULL;
return result;
}
else
{
del=Q->front->next;
result=del->elem;
Q->front->next=del->next;
free(del);
del=NULL;
return result;
}
}
}
void QueueTraverse(LinkedQueue *Q)
{
QNode *p=Q->front->next;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedQueue_string.h
#ifndef LINKEDQUEUE_STRING_H_INCLUDED
#define LINKEDQUEUE_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}QNode;
typedef struct
{
QNode* front;
QNode* rear;
}LinkedQueue;
extern "C" __declspec(dllexport) int InitQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void DestroyQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void ClearQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueEmpty(LinkedQueue *Q);
extern "C" __declspec(dllexport) int QueueLength(LinkedQueue *Q);
extern "C" __declspec(dllexport) ElemType GetHead(LinkedQueue *Q);
extern "C" __declspec(dllexport) int EnQueue(LinkedQueue *Q,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(LinkedQueue *Q);
extern "C" __declspec(dllexport) void QueueTraverse(LinkedQueue *Q);
#endif // LINKEDQUEUE_STRING_H_INCLUDED
LinkedStack_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedStack_char.h"
int InitStack(Stack *S,int length)
{
S->instance=(StackNode*)malloc(sizeof(StackNode));
if(S->instance!=NULL)
{
S->instance->next=NULL;
S->length=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(Stack *S)
{
StackNode *p=S->instance;
while(S->instance!=NULL)
{
p=S->instance;
S->instance=p->next;
free(p);
p=NULL;
}
}
void ClearStack(Stack *S)
{
StackNode *p=S->instance->next;
while(S->instance->next!=NULL)
{
p=S->instance->next;
S->instance->next=p->next;
free(p);
p=NULL;
}
}
int StackEmpty(Stack *S)
{
if(S->instance->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(Stack *S)
{
StackNode *p=S->instance;
int i=0;
while(i<S->length&&p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetTop(Stack *S)
{
if(S->instance->next!=NULL)
{
return S->instance->next->elem;
}
else
{
return NULL;
}
}
int Push(Stack *S,ElemType elem)
{
if(StackLength(S)==S->length)
{
return FALSE;
}
else
{
StackNode *s;
s=(StackNode*)malloc(sizeof(StackNode));
s->elem=elem;
s->next=S->instance->next;
S->instance->next=s;
return TRUE;
}
}
ElemType Pop(Stack *S)
{
if(S->instance->next==NULL)
{
return NULL;
}
else
{
StackNode *p=S->instance->next;
S->instance->next=p->next;
ElemType tmp=p->elem;
free(p);
p=NULL;
return tmp;
}
}
void StackTraverse(Stack *S)
{
StackNode *p=S->instance->next;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedStack_char.h
#ifndef LINKEDSTACK_CHAR_H_INCLUDED
#define LINKEDSTACK_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}StackNode;
typedef struct{
StackNode* instance;
int length;
}Stack;
extern "C" __declspec(dllexport) int InitStack(Stack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(Stack *S);
extern "C" __declspec(dllexport) void ClearStack(Stack *S);
extern "C" __declspec(dllexport) int StackEmpty(Stack *S);
extern "C" __declspec(dllexport) int StackLength(Stack *S);
extern "C" __declspec(dllexport) ElemType GetTop(Stack *S);
extern "C" __declspec(dllexport) int Push(Stack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(Stack *S);
extern "C" __declspec(dllexport) void StackTraverse(Stack *S);
#endif // LINKEDSTACK_CHAR_H_INCLUDED
LinkedStack_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedStack_double.h"
int InitStack(Stack *S,int length)
{
S->instance=(StackNode*)malloc(sizeof(StackNode));
if(S->instance!=NULL)
{
S->instance->next=NULL;
S->length=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(Stack *S)
{
StackNode *p=S->instance;
while(S->instance!=NULL)
{
p=S->instance;
S->instance=p->next;
free(p);
p=NULL;
}
}
void ClearStack(Stack *S)
{
StackNode *p=S->instance->next;
while(S->instance->next!=NULL)
{
p=S->instance->next;
S->instance->next=p->next;
free(p);
p=NULL;
}
}
int StackEmpty(Stack *S)
{
if(S->instance->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(Stack *S)
{
StackNode *p=S->instance;
int i=0;
while(i<S->length&&p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetTop(Stack *S)
{
if(S->instance->next!=NULL)
{
return S->instance->next->elem;
}
else
{
return NULL;
}
}
int Push(Stack *S,ElemType elem)
{
if(StackLength(S)==S->length)
{
return FALSE;
}
else
{
StackNode *s;
s=(StackNode*)malloc(sizeof(StackNode));
s->elem=elem;
s->next=S->instance->next;
S->instance->next=s;
return TRUE;
}
}
ElemType Pop(Stack *S)
{
if(S->instance->next==NULL)
{
return NULL;
}
else
{
StackNode *p=S->instance->next;
S->instance->next=p->next;
ElemType tmp=p->elem;
free(p);
p=NULL;
return tmp;
}
}
void StackTraverse(Stack *S)
{
StackNode *p=S->instance->next;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedStack_double.h
#ifndef LINKEDSTACK_DOUBLE_H_INCLUDED
#define LINKEDSTACK_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}StackNode;
typedef struct{
StackNode* instance;
int length;
}Stack;
extern "C" __declspec(dllexport) int InitStack(Stack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(Stack *S);
extern "C" __declspec(dllexport) void ClearStack(Stack *S);
extern "C" __declspec(dllexport) int StackEmpty(Stack *S);
extern "C" __declspec(dllexport) int StackLength(Stack *S);
extern "C" __declspec(dllexport) ElemType GetTop(Stack *S);
extern "C" __declspec(dllexport) int Push(Stack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(Stack *S);
extern "C" __declspec(dllexport) void StackTraverse(Stack *S);
#endif // LINKEDSTACK_DOUBLE_H_INCLUDED
LinkedStack_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedStack_int.h"
int InitStack(Stack *S,int length)
{
S->instance=(StackNode*)malloc(sizeof(StackNode));
if(S->instance!=NULL)
{
S->instance->next=NULL;
S->length=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(Stack *S)
{
StackNode *p=S->instance;
while(S->instance!=NULL)
{
p=S->instance;
S->instance=p->next;
free(p);
p=NULL;
}
}
void ClearStack(Stack *S)
{
StackNode *p=S->instance->next;
while(S->instance->next!=NULL)
{
p=S->instance->next;
S->instance->next=p->next;
free(p);
p=NULL;
}
}
int StackEmpty(Stack *S)
{
if(S->instance->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(Stack *S)
{
StackNode *p=S->instance;
int i=0;
while(i<S->length&&p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetTop(Stack *S)
{
if(S->instance->next!=NULL)
{
return S->instance->next->elem;
}
else
{
return NULL;
}
}
int Push(Stack *S,ElemType elem)
{
if(StackLength(S)==S->length)
{
return FALSE;
}
else
{
StackNode *s;
s=(StackNode*)malloc(sizeof(StackNode));
s->elem=elem;
s->next=S->instance->next;
S->instance->next=s;
return TRUE;
}
}
ElemType Pop(Stack *S)
{
if(S->instance->next==NULL)
{
return NULL;
}
else
{
StackNode *p=S->instance->next;
S->instance->next=p->next;
ElemType tmp=p->elem;
free(p);
p=NULL;
return tmp;
}
}
void StackTraverse(Stack *S)
{
StackNode *p=S->instance->next;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedStack_int.h
#ifndef LINKEDSTACK_INT_H_INCLUDED
#define LINKEDSTACK_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}StackNode;
typedef struct{
StackNode* instance;
int length;
}Stack;
extern "C" __declspec(dllexport) int InitStack(Stack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(Stack *S);
extern "C" __declspec(dllexport) void ClearStack(Stack *S);
extern "C" __declspec(dllexport) int StackEmpty(Stack *S);
extern "C" __declspec(dllexport) int StackLength(Stack *S);
extern "C" __declspec(dllexport) ElemType GetTop(Stack *S);
extern "C" __declspec(dllexport) int Push(Stack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(Stack *S);
extern "C" __declspec(dllexport) void StackTraverse(Stack *S);
#endif // LINKEDSTACK_INT_H_INCLUDED
LinkedStack_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "LinkedStack_string.h"
int InitStack(Stack *S,int length)
{
S->instance=(StackNode*)malloc(sizeof(StackNode));
if(S->instance!=NULL)
{
S->instance->next=NULL;
S->length=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(Stack *S)
{
StackNode *p=S->instance;
while(S->instance!=NULL)
{
p=S->instance;
S->instance=p->next;
free(p);
p=NULL;
}
}
void ClearStack(Stack *S)
{
StackNode *p=S->instance->next;
while(S->instance->next!=NULL)
{
p=S->instance->next;
S->instance->next=p->next;
free(p);
p=NULL;
}
}
int StackEmpty(Stack *S)
{
if(S->instance->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(Stack *S)
{
StackNode *p=S->instance;
int i=0;
while(i<S->length&&p->next!=NULL)
{
i++;
p=p->next;
}
return i;
}
ElemType GetTop(Stack *S)
{
if(S->instance->next!=NULL)
{
return S->instance->next->elem;
}
else
{
return NULL;
}
}
int Push(Stack *S,ElemType elem)
{
if(StackLength(S)==S->length)
{
return FALSE;
}
else
{
StackNode *s;
s=(StackNode*)malloc(sizeof(StackNode));
s->elem=elem;
s->next=S->instance->next;
S->instance->next=s;
return TRUE;
}
}
ElemType Pop(Stack *S)
{
if(S->instance->next==NULL)
{
return NULL;
}
else
{
StackNode *p=S->instance->next;
S->instance->next=p->next;
ElemType tmp=p->elem;
free(p);
p=NULL;
return tmp;
}
}
void StackTraverse(Stack *S)
{
StackNode *p=S->instance->next;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
LinkedStack_string.h
#ifndef LINKEDSTACK_STRING_H_INCLUDED
#define LINKEDSTACK_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}StackNode;
typedef struct{
StackNode* instance;
int length;
}Stack;
extern "C" __declspec(dllexport) int InitStack(Stack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(Stack *S);
extern "C" __declspec(dllexport) void ClearStack(Stack *S);
extern "C" __declspec(dllexport) int StackEmpty(Stack *S);
extern "C" __declspec(dllexport) int StackLength(Stack *S);
extern "C" __declspec(dllexport) ElemType GetTop(Stack *S);
extern "C" __declspec(dllexport) int Push(Stack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(Stack *S);
extern "C" __declspec(dllexport) void StackTraverse(Stack *S);
#endif // LINKEDSTACK_STRING_H_INCLUDED
SequentialArray_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialArray_char.h"
int InitArray(TSMatrix *t,int rows,int cols,int capacity)
{
if(rows*cols<capacity)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
t->data=(TupNode*)malloc(sizeof(TupNode)*capacity);
if(t->data==NULL)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
else
{
t->capacity=capacity;
t->rows=rows;
t->cols=cols;
t->nums=0;
for(int i=0;i<t->capacity;i++)
{
t->data[i].d=0;
t->data[i].c=0;
t->data[i].r=0;
}
return TRUE;
}
}
void DestroyArray(TSMatrix *t)
{
free(t->data);
t->data=NULL;
t->capacity=0;
t->cols=0;
t->nums=0;
t->rows=0;
}
int Assign(TSMatrix *t,ElemType elem,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
else
{
t->data[t->nums].d=elem;
t->data[t->nums].c=j;
t->data[t->nums].r=i;
++t->nums;
return TRUE;
}
}
int DeleteValue(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
for(int tmp_j=tmp_i;tmp_j<t->nums;tmp_j++)
{
t->data[tmp_j].c=t->data[tmp_j+1].c;
t->data[tmp_j].d=t->data[tmp_j+1].d;
t->data[tmp_j].r=t->data[tmp_j+1].r;
}
t->nums--;
return TRUE;
}
}
return FALSE;
}
int SetValue(TSMatrix *t,int i,int j,ElemType elem)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
t->data[tmp_i].d=elem;
return TRUE;
}
}
return FALSE;
}
ElemType Value(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return NULL;
}
for(int tmp=0;tmp<t->nums;tmp++)
{
if(t->data[tmp].c==j&&t->data[tmp].r==i)
{
return t->data[tmp].d;
}
}
return NULL;
}
void CreateMat(TSMatrix *t)
{
for(int i=0;i<t->rows;i++)
{
for(int j=0;j<t->cols;j++)
{
printf("%c\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
void CreateMatByReverseOrder(TSMatrix *t)
{
for(int j=0;j<t->cols;j++)
{
for(int i=0;i<t->rows;i++)
{
printf("%c\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
SequentialArray_char.h
#ifndef SEQUENTIALARRAY_CHAR_H_INCLUDED
#define SEQUENTIALARRAY_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct
{
int r;
int c;
ElemType d;
}TupNode;
typedef struct
{
int rows;
int cols;
int nums;
int capacity;
TupNode* data;
}TSMatrix;
extern "C" __declspec(dllexport) int InitArray(TSMatrix *t,int rows,int cols,int capacity);
extern "C" __declspec(dllexport) void DestroyArray(TSMatrix *t);
extern "C" __declspec(dllexport) int Assign(TSMatrix *t,ElemType elem,int i,int j);
extern "C" __declspec(dllexport) int DeleteValue(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) int SetValue(TSMatrix *t,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) void CreateMat(TSMatrix *t);
extern "C" __declspec(dllexport) void CreateMatByReverseOrder(TSMatrix *t);
#endif // SEQUENTIALARRAY_CHAR_H_INCLUDED
SequentialArray_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialArray_double.h"
int InitArray(TSMatrix *t,int rows,int cols,int capacity)
{
if(rows*cols<capacity)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
t->data=(TupNode*)malloc(sizeof(TupNode)*capacity);
if(t->data==NULL)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
else
{
t->capacity=capacity;
t->rows=rows;
t->cols=cols;
t->nums=0;
for(int i=0;i<t->capacity;i++)
{
t->data[i].d=0;
t->data[i].c=0;
t->data[i].r=0;
}
return TRUE;
}
}
void DestroyArray(TSMatrix *t)
{
free(t->data);
t->data=NULL;
t->capacity=0;
t->cols=0;
t->nums=0;
t->rows=0;
}
int Assign(TSMatrix *t,ElemType elem,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
else
{
t->data[t->nums].d=elem;
t->data[t->nums].c=j;
t->data[t->nums].r=i;
++t->nums;
return TRUE;
}
}
int DeleteValue(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
for(int tmp_j=tmp_i;tmp_j<t->nums;tmp_j++)
{
t->data[tmp_j].c=t->data[tmp_j+1].c;
t->data[tmp_j].d=t->data[tmp_j+1].d;
t->data[tmp_j].r=t->data[tmp_j+1].r;
}
t->nums--;
return TRUE;
}
}
return FALSE;
}
int SetValue(TSMatrix *t,int i,int j,ElemType elem)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
t->data[tmp_i].d=elem;
return TRUE;
}
}
return FALSE;
}
ElemType Value(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return NULL;
}
for(int tmp=0;tmp<t->nums;tmp++)
{
if(t->data[tmp].c==j&&t->data[tmp].r==i)
{
return t->data[tmp].d;
}
}
return NULL;
}
void CreateMat(TSMatrix *t)
{
for(int i=0;i<t->rows;i++)
{
for(int j=0;j<t->cols;j++)
{
printf("%f\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
void CreateMatByReverseOrder(TSMatrix *t)
{
for(int j=0;j<t->cols;j++)
{
for(int i=0;i<t->rows;i++)
{
printf("%f\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
SequentialArray_double.h
#ifndef SEQUENTIALARRAY_DOUBLE_H_INCLUDED
#define SEQUENTIALARRAY_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct
{
int r;
int c;
ElemType d;
}TupNode;
typedef struct
{
int rows;
int cols;
int nums;
int capacity;
TupNode* data;
}TSMatrix;
extern "C" __declspec(dllexport) int InitArray(TSMatrix *t,int rows,int cols,int capacity);
extern "C" __declspec(dllexport) void DestroyArray(TSMatrix *t);
extern "C" __declspec(dllexport) int Assign(TSMatrix *t,ElemType elem,int i,int j);
extern "C" __declspec(dllexport) int DeleteValue(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) int SetValue(TSMatrix *t,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) void CreateMat(TSMatrix *t);
extern "C" __declspec(dllexport) void CreateMatByReverseOrder(TSMatrix *t);
#endif // SEQUENTIALARRAY_DOUBLE_H_INCLUDED
SequentialArray_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialArray_int.h"
int InitArray(TSMatrix *t,int rows,int cols,int capacity)
{
if(rows*cols<capacity)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
t->data=(TupNode*)malloc(sizeof(TupNode)*capacity);
if(t->data==NULL)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
else
{
t->capacity=capacity;
t->rows=rows;
t->cols=cols;
t->nums=0;
for(int i=0;i<t->capacity;i++)
{
t->data[i].d=0;
t->data[i].c=0;
t->data[i].r=0;
}
return TRUE;
}
}
void DestroyArray(TSMatrix *t)
{
free(t->data);
t->data=NULL;
t->capacity=0;
t->cols=0;
t->nums=0;
t->rows=0;
}
int Assign(TSMatrix *t,ElemType elem,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
else
{
t->data[t->nums].d=elem;
t->data[t->nums].c=j;
t->data[t->nums].r=i;
++t->nums;
return TRUE;
}
}
int DeleteValue(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
for(int tmp_j=tmp_i;tmp_j<t->nums;tmp_j++)
{
t->data[tmp_j].c=t->data[tmp_j+1].c;
t->data[tmp_j].d=t->data[tmp_j+1].d;
t->data[tmp_j].r=t->data[tmp_j+1].r;
}
t->nums--;
return TRUE;
}
}
return FALSE;
}
int SetValue(TSMatrix *t,int i,int j,ElemType elem)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
t->data[tmp_i].d=elem;
return TRUE;
}
}
return FALSE;
}
ElemType Value(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return NULL;
}
for(int tmp=0;tmp<t->nums;tmp++)
{
if(t->data[tmp].c==j&&t->data[tmp].r==i)
{
return t->data[tmp].d;
}
}
return NULL;
}
void CreateMat(TSMatrix *t)
{
for(int i=0;i<t->rows;i++)
{
for(int j=0;j<t->cols;j++)
{
printf("%d\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
void CreateMatByReverseOrder(TSMatrix *t)
{
for(int j=0;j<t->cols;j++)
{
for(int i=0;i<t->rows;i++)
{
printf("%d\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
SequentialArray_int.h
#ifndef SEQUENTIALARRAY_INT_H_INCLUDED
#define SEQUENTIALARRAY_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct
{
int r;
int c;
ElemType d;
}TupNode;
typedef struct
{
int rows;
int cols;
int nums;
int capacity;
TupNode* data;
}TSMatrix;
extern "C" __declspec(dllexport) int InitArray(TSMatrix *t,int rows,int cols,int capacity);
extern "C" __declspec(dllexport) void DestroyArray(TSMatrix *t);
extern "C" __declspec(dllexport) int Assign(TSMatrix *t,ElemType elem,int i,int j);
extern "C" __declspec(dllexport) int DeleteValue(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) int SetValue(TSMatrix *t,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) void CreateMat(TSMatrix *t);
extern "C" __declspec(dllexport) void CreateMatByReverseOrder(TSMatrix *t);
#endif // SEQUENTIALARRAY_INT_H_INCLUDED
SequentialArray_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialArray_string.h"
int InitArray(TSMatrix *t,int rows,int cols,int capacity)
{
if(rows*cols<capacity)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
t->data=(TupNode*)malloc(sizeof(TupNode)*capacity);
if(t->data==NULL)
{
t->cols=-1;
t->rows=-1;
return FALSE;
}
else
{
t->capacity=capacity;
t->rows=rows;
t->cols=cols;
t->nums=0;
for(int i=0;i<t->capacity;i++)
{
t->data[i].d=0;
t->data[i].c=0;
t->data[i].r=0;
}
return TRUE;
}
}
void DestroyArray(TSMatrix *t)
{
free(t->data);
t->data=NULL;
t->capacity=0;
t->cols=0;
t->nums=0;
t->rows=0;
}
int Assign(TSMatrix *t,ElemType elem,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
else
{
t->data[t->nums].d=elem;
t->data[t->nums].c=j;
t->data[t->nums].r=i;
++t->nums;
return TRUE;
}
}
int DeleteValue(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
for(int tmp_j=tmp_i;tmp_j<t->nums;tmp_j++)
{
t->data[tmp_j].c=t->data[tmp_j+1].c;
t->data[tmp_j].d=t->data[tmp_j+1].d;
t->data[tmp_j].r=t->data[tmp_j+1].r;
}
t->nums--;
return TRUE;
}
}
return FALSE;
}
int SetValue(TSMatrix *t,int i,int j,ElemType elem)
{
if(i>=t->rows||j>=t->cols)
{
return FALSE;
}
for(int tmp_i=0;tmp_i<t->nums;tmp_i++)
{
if(t->data[tmp_i].c==j&&t->data[tmp_i].r==i)
{
t->data[tmp_i].d=elem;
return TRUE;
}
}
return FALSE;
}
ElemType Value(TSMatrix *t,int i,int j)
{
if(i>=t->rows||j>=t->cols)
{
return NULL;
}
for(int tmp=0;tmp<t->nums;tmp++)
{
if(t->data[tmp].c==j&&t->data[tmp].r==i)
{
return t->data[tmp].d;
}
}
return NULL;
}
void CreateMat(TSMatrix *t)
{
for(int i=0;i<t->rows;i++)
{
for(int j=0;j<t->cols;j++)
{
printf("%s\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
void CreateMatByReverseOrder(TSMatrix *t)
{
for(int j=0;j<t->cols;j++)
{
for(int i=0;i<t->rows;i++)
{
printf("%s\t",Value(t,i,j));
}
printf("\n");
}
printf("\n");
}
SequentialArray_string.h
#ifndef SEQUENTIALARRAY_STRING_H_INCLUDED
#define SEQUENTIALARRAY_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct
{
int r;
int c;
ElemType d;
}TupNode;
typedef struct
{
int rows;
int cols;
int nums;
int capacity;
TupNode* data;
}TSMatrix;
extern "C" __declspec(dllexport) int InitArray(TSMatrix *t,int rows,int cols,int capacity);
extern "C" __declspec(dllexport) void DestroyArray(TSMatrix *t);
extern "C" __declspec(dllexport) int Assign(TSMatrix *t,ElemType elem,int i,int j);
extern "C" __declspec(dllexport) int DeleteValue(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) int SetValue(TSMatrix *t,int i,int j,ElemType elem);
extern "C" __declspec(dllexport) ElemType Value(TSMatrix *t,int i,int j);
extern "C" __declspec(dllexport) void CreateMat(TSMatrix *t);
extern "C" __declspec(dllexport) void CreateMatByReverseOrder(TSMatrix *t);
#endif // SEQUENTIALARRAY_STRING_H_INCLUDED
SequentialList_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialList_char.h"
#define TRUE 1
#define FALSE -1
void InitList(SeqList *L,int size)
{
L->elem=(ElemType*)calloc(size,sizeof(ElemType));
L->length=0;
}
void DestroyList(SeqList *L)
{
free(L->elem);
L->elem=NULL;
L->length=NULL;
}
void ClearList(SeqList *L)
{
for(int i=0;i<L->length;i++)
{
L->elem[i]=NULL;
}
L->length=0;
}
int ListEmpty(SeqList *L)
{
if(L->elem!=NULL&&L->length!=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(SeqList *L)
{
return L->length;
}
ElemType GetElem(SeqList *L,int index)
{
if(index<0||index>L->length)
{
return NULL;
}
else
{
return L->elem[index];
}
}
int LocateElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem)
{
return i;
}
i++;
}
return -1;
}
ElemType PriorElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=0)
{
return L->elem[i-1];
}
i++;
}
return NULL;
}
ElemType NextElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=L->length-1)
{
return L->elem[i+1];
}
i++;
}
return NULL;
}
int ListInsert(SeqList *L,int index,ElemType elem)
{
if(index<0||index>L->length)
{
return FALSE;
}
for(int i=L->length;i>=index;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[index]=elem;
L->length++;
return TRUE;
}
int ListDelete(SeqList *L,int index)
{
if(index<0||index>=L->length)
{
return FALSE;
}
for(int i=index;i<L->length;i++)
{
L->elem[i]=L->elem[i+1];
}
L->elem[L->length]=NULL;
L->length--;
return TRUE;
}
void TraverseList(SeqList *L)
{
int i=0;
while(i<L->length)
{
printf("%c\t",L->elem[i]);
i++;
}
printf("\n");
}
SequentialList_char.h
#ifndef SEQUENCIALLIST_CHAR_H_INCLUDED
#define SEQUENCIALLIST_CHAR_H_INCLUDED
typedef char ElemType;
typedef struct
{
ElemType *elem;
int length;
}SeqList;
extern "C" __declspec(dllexport) void InitList(SeqList *L,int size);
extern "C" __declspec(dllexport) void DestroyList(SeqList *L);
extern "C" __declspec(dllexport) void ClearList(SeqList *L);
extern "C" __declspec(dllexport) int ListEmpty(SeqList *L);
extern "C" __declspec(dllexport) int ListLength(SeqList *L);
extern "C" __declspec(dllexport) ElemType GetElem(SeqList *L,int index);
extern "C" __declspec(dllexport) int LocateElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(SeqList *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(SeqList *L,int index);
extern "C" __declspec(dllexport) void TraverseList(SeqList *L);
#endif // SEQUENCIALLIST_CHAR_H_INCLUDED
SequentialList_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialList_double.h"
#define TRUE 1
#define FALSE -1
void InitList(SeqList *L,int size)
{
L->elem=(ElemType*)calloc(size,sizeof(ElemType));
L->length=0;
}
void DestroyList(SeqList *L)
{
free(L->elem);
L->elem=NULL;
L->length=NULL;
}
void ClearList(SeqList *L)
{
for(int i=0;i<L->length;i++)
{
L->elem[i]=NULL;
}
L->length=0;
}
int ListEmpty(SeqList *L)
{
if(L->elem!=NULL&&L->length!=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(SeqList *L)
{
return L->length;
}
ElemType GetElem(SeqList *L,int index)
{
if(index<0||index>L->length)
{
return NULL;
}
else
{
return L->elem[index];
}
}
int LocateElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem)
{
return i;
}
i++;
}
return -1;
}
ElemType PriorElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=0)
{
return L->elem[i-1];
}
i++;
}
return NULL;
}
ElemType NextElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=L->length-1)
{
return L->elem[i+1];
}
i++;
}
return NULL;
}
int ListInsert(SeqList *L,int index,ElemType elem)
{
if(index<0||index>L->length)
{
return FALSE;
}
for(int i=L->length;i>=index;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[index]=elem;
L->length++;
return TRUE;
}
int ListDelete(SeqList *L,int index)
{
if(index<0||index>=L->length)
{
return FALSE;
}
for(int i=index;i<L->length;i++)
{
L->elem[i]=L->elem[i+1];
}
L->elem[L->length]=NULL;
L->length--;
return TRUE;
}
void TraverseList(SeqList *L)
{
int i=0;
while(i<L->length)
{
printf("%f\t",L->elem[i]);
i++;
}
printf("\n");
}
SequentialList_double.h
#ifndef SEQUENCIALLIST_DOUBLE_H_INCLUDED
#define SEQUENCIALLIST_DOUBLE_H_INCLUDED
typedef double ElemType;
typedef struct
{
ElemType *elem;
int length;
}SeqList;
extern "C" __declspec(dllexport) void InitList(SeqList *L,int size);
extern "C" __declspec(dllexport) void DestroyList(SeqList *L);
extern "C" __declspec(dllexport) void ClearList(SeqList *L);
extern "C" __declspec(dllexport) int ListEmpty(SeqList *L);
extern "C" __declspec(dllexport) int ListLength(SeqList *L);
extern "C" __declspec(dllexport) ElemType GetElem(SeqList *L,int index);
extern "C" __declspec(dllexport) int LocateElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(SeqList *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(SeqList *L,int index);
extern "C" __declspec(dllexport) void TraverseList(SeqList *L);
#endif // SEQUENCIALLIST_DOUBLE_H_INCLUDED
SequentialList_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialList_int.h"
#define TRUE 1
#define FALSE -1
void InitList(SeqList *L,int size)
{
L->elem=(ElemType*)calloc(size,sizeof(ElemType));
L->length=0;
}
void DestroyList(SeqList *L)
{
free(L->elem);
L->elem=NULL;
L->length=NULL;
}
void ClearList(SeqList *L)
{
for(int i=0;i<L->length;i++)
{
L->elem[i]=NULL;
}
L->length=0;
}
int ListEmpty(SeqList *L)
{
if(L->elem!=NULL&&L->length!=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(SeqList *L)
{
return L->length;
}
ElemType GetElem(SeqList *L,int index)
{
if(index<0||index>L->length)
{
return NULL;
}
else
{
return L->elem[index];
}
}
int LocateElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem)
{
return i;
}
i++;
}
return -1;
}
ElemType PriorElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=0)
{
return L->elem[i-1];
}
i++;
}
return NULL;
}
ElemType NextElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=L->length-1)
{
return L->elem[i+1];
}
i++;
}
return NULL;
}
int ListInsert(SeqList *L,int index,ElemType elem)
{
if(index<0||index>L->length)
{
return FALSE;
}
for(int i=L->length;i>=index;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[index]=elem;
L->length++;
return TRUE;
}
int ListDelete(SeqList *L,int index)
{
if(index<0||index>=L->length)
{
return FALSE;
}
for(int i=index;i<L->length;i++)
{
L->elem[i]=L->elem[i+1];
}
L->elem[L->length]=NULL;
L->length--;
return TRUE;
}
void TraverseList(SeqList *L)
{
int i=0;
while(i<L->length)
{
printf("%d\t",L->elem[i]);
i++;
}
printf("\n");
}
SequentialList_int.h
#ifndef SEQUENCIALLIST_INT_H_INCLUDED
#define SEQUENCIALLIST_INT_H_INCLUDED
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SeqList;
extern "C" __declspec(dllexport) void InitList(SeqList *L,int size);
extern "C" __declspec(dllexport) void DestroyList(SeqList *L);
extern "C" __declspec(dllexport) void ClearList(SeqList *L);
extern "C" __declspec(dllexport) int ListEmpty(SeqList *L);
extern "C" __declspec(dllexport) int ListLength(SeqList *L);
extern "C" __declspec(dllexport) ElemType GetElem(SeqList *L,int index);
extern "C" __declspec(dllexport) int LocateElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(SeqList *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(SeqList *L,int index);
extern "C" __declspec(dllexport) void TraverseList(SeqList *L);
#endif // SEQUENCIALLIST_INT_H_INCLUDED
SequentialList_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialList_string.h"
#define TRUE 1
#define FALSE -1
void InitList(SeqList *L,int size)
{
L->elem=(ElemType*)calloc(size,sizeof(ElemType));
L->length=0;
}
void DestroyList(SeqList *L)
{
free(L->elem);
L->elem=NULL;
L->length=NULL;
}
void ClearList(SeqList *L)
{
for(int i=0;i<L->length;i++)
{
L->elem[i]=NULL;
}
L->length=0;
}
int ListEmpty(SeqList *L)
{
if(L->elem!=NULL&&L->length!=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(SeqList *L)
{
return L->length;
}
ElemType GetElem(SeqList *L,int index)
{
if(index<0||index>L->length)
{
return NULL;
}
else
{
return L->elem[index];
}
}
int LocateElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem)
{
return i;
}
i++;
}
return -1;
}
ElemType PriorElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=0)
{
return L->elem[i-1];
}
i++;
}
return NULL;
}
ElemType NextElem(SeqList *L,ElemType elem)
{
int i=0;
while(i<L->length)
{
if(L->elem[i]==elem&&i!=L->length-1)
{
return L->elem[i+1];
}
i++;
}
return NULL;
}
int ListInsert(SeqList *L,int index,ElemType elem)
{
if(index<0||index>L->length)
{
return FALSE;
}
for(int i=L->length;i>=index;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[index]=elem;
L->length++;
return TRUE;
}
int ListDelete(SeqList *L,int index)
{
if(index<0||index>=L->length)
{
return FALSE;
}
for(int i=index;i<L->length;i++)
{
L->elem[i]=L->elem[i+1];
}
L->elem[L->length]=NULL;
L->length--;
return TRUE;
}
void TraverseList(SeqList *L)
{
int i=0;
while(i<L->length)
{
printf("%s\t",L->elem[i]);
i++;
}
printf("\n");
}
SequentialList_string.h
#ifndef SEQUENCIALLIST_STRING_H_INCLUDED
#define SEQUENCIALLIST_STRING_H_INCLUDED
typedef const char* ElemType;
typedef struct
{
ElemType *elem;
int length;
}SeqList;
extern "C" __declspec(dllexport) void InitList(SeqList *L,int size);
extern "C" __declspec(dllexport) void DestroyList(SeqList *L);
extern "C" __declspec(dllexport) void ClearList(SeqList *L);
extern "C" __declspec(dllexport) int ListEmpty(SeqList *L);
extern "C" __declspec(dllexport) int ListLength(SeqList *L);
extern "C" __declspec(dllexport) ElemType GetElem(SeqList *L,int index);
extern "C" __declspec(dllexport) int LocateElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(SeqList *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(SeqList *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(SeqList *L,int index);
extern "C" __declspec(dllexport) void TraverseList(SeqList *L);
#endif // SEQUENCIALLIST_STRING_H_INCLUDED
SequentialQueue_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialQueue_char.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
S->front=0;
S->rear=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front==S->rear)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
return S->base[S->front];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear-S->front==S->size)
{
return FALSE;
}
else
{
S->base[S->rear]=elem;
S->rear++;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front];
S->front++;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=S->front;
while(i<S->rear)
{
printf("%c\t",*(S->base+i));
i++;
}
printf("\n");
}
SequentialQueue_char.h
#ifndef SEQUENTIALQUEUE_CHAR_H_INCLUDED
#define SEQUENTIALQUEUE_CHAR_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef char ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // SEQUENTIALQUEUE_CHAR_H_INCLUDED
SequentialQueue_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialQueue_double.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
S->front=0;
S->rear=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front==S->rear)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
return S->base[S->front];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear-S->front==S->size)
{
return FALSE;
}
else
{
S->base[S->rear]=elem;
S->rear++;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front];
S->front++;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=S->front;
while(i<S->rear)
{
printf("%f\t",*(S->base+i));
i++;
}
printf("\n");
}
SequentialQueue_double.h
#ifndef SEQUENTIALQUEUE_DOUBLE_H_INCLUDED
#define SEQUENTIALQUEUE_DOUBLE_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef double ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // SEQUENTIALQUEUE_DOUBLE_H_INCLUDED
SequentialQueue_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialQueue_int.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
S->front=0;
S->rear=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front==S->rear)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
return S->base[S->front];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear-S->front==S->size)
{
return FALSE;
}
else
{
S->base[S->rear]=elem;
S->rear++;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front];
S->front++;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=S->front;
while(i<S->rear)
{
printf("%d\t",*(S->base+i));
i++;
}
printf("\n");
}
SequentialQueue_int.h
#ifndef SEQUENTIALQUEUE_INT_H_INCLUDED
#define SEQUENTIALQUEUE_INT_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // SEQUENTIALQUEUE_INT_H_INCLUDED
SequentialQueue_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialQueue_string.h"
int InitQueue(SqQueue *S,int size)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*size);
if(S->base!=NULL)
{
S->front=0;
S->rear=0;
S->size=size;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyQueue(SqQueue *S)
{
free(S->base);
S->base=NULL;
S->front=0;
S->rear=0;
S->size=0;
}
void ClearQueue(SqQueue *S)
{
for(int i=0;i<S->size;i++)
{
*(S->base+i)=NULL;
}
S->front=0;
S->rear=0;
}
int QueueEmpty(SqQueue *S)
{
if(S->front==S->rear)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueLength(SqQueue *S)
{
return S->rear-S->front;
}
ElemType GetHead(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
return S->base[S->front];
}
}
int EnQueue(SqQueue *S,ElemType elem)
{
if(S->rear-S->front==S->size)
{
return FALSE;
}
else
{
S->base[S->rear]=elem;
S->rear++;
return TRUE;
}
}
ElemType DeQueue(SqQueue *S)
{
if(S->front==S->rear)
{
return NULL;
}
else
{
ElemType tmp=S->base[S->front];
S->front++;
return tmp;
}
}
void QueueTraverse(SqQueue *S)
{
int i=S->front;
while(i<S->rear)
{
printf("%s\t",*(S->base+i));
i++;
}
printf("\n");
}
SequentialQueue_string.h
#ifndef SEQUENTIALQUEUE_STRING_H_INCLUDED
#define SEQUENTIALQUEUE_STRING_H_INCLUDED
#define TRUE 1
#define FALSE -1
typedef const char* ElemType;
typedef struct Node
{
ElemType *base;
int front;
int rear;
int size;
}SqQueue;
extern "C" __declspec(dllexport) int InitQueue(SqQueue *S,int size);
extern "C" __declspec(dllexport) void DestroyQueue(SqQueue *S);
extern "C" __declspec(dllexport) void ClearQueue(SqQueue *S);
extern "C" __declspec(dllexport) int QueueEmpty(SqQueue *S);
extern "C" __declspec(dllexport) int QueueLength(SqQueue *S);
extern "C" __declspec(dllexport) ElemType GetHead(SqQueue *S);
extern "C" __declspec(dllexport) int EnQueue(SqQueue *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType DeQueue(SqQueue *S);
extern "C" __declspec(dllexport) void QueueTraverse(SqQueue *S);
#endif // SEQUENTIALQUEUE_STRING_H_INCLUDED
SequentialStack_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialStack_char.h"
int InitStack(SqStack *S,int length)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*length);
S->top=S->base;
if(S->base!=NULL&&S->top!=NULL)
{
S->stacksize=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=NULL;
}
void ClearStack(SqStack *S)
{
for(int i=0;i<S->stacksize;i++)
{
S->base[i]=NULL;
}
S->top=S->base;
}
int StackEmpty(SqStack *S)
{
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack *S)
{
return S->top-S->base;
}
ElemType GetTop(SqStack *S)
{
if(S->top!=S->base)
{
return *(S->top-1);
}
else
{
return NULL;
}
}
int Push(SqStack *S,ElemType elem)
{
if(((S->top)-(S->base))==S->stacksize)
{
return FALSE;
}
else
{
S->top[0]=elem;
S->top++;
return TRUE;
}
}
ElemType Pop(SqStack *S)
{
if(S->top==S->base)
{
return NULL;
}
else
{
--S->top;
return S->top[0];
}
}
void StackTraverse(SqStack *S)
{
int i=0;
while(i<(S->top-S->base))
{
printf("%c\t",S->base[i]);
i++;
}
printf("\n");
}
SequentialStack_char.h
#ifndef SEQUENTIALSTACK_CHAR_H_INCLUDED
#define SEQUENTIALSTACK_CHAR_H_INCLUDED
typedef char ElemType;
#define TRUE 1
#define FALSE -1
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
extern "C" __declspec(dllexport) int InitStack(SqStack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(SqStack *S);
extern "C" __declspec(dllexport) void ClearStack(SqStack *S);
extern "C" __declspec(dllexport) int StackEmpty(SqStack *S);
extern "C" __declspec(dllexport) int StackLength(SqStack *S);
extern "C" __declspec(dllexport) ElemType GetTop(SqStack *S);
extern "C" __declspec(dllexport) int Push(SqStack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(SqStack *S);
extern "C" __declspec(dllexport) void StackTraverse(SqStack *S);
#endif // SEQUENTIALSTACK_CHAR_H_INCLUDED
SequentialStack_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialStack_double.h"
int InitStack(SqStack *S,int length)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*length);
S->top=S->base;
if(S->base!=NULL&&S->top!=NULL)
{
S->stacksize=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=NULL;
}
void ClearStack(SqStack *S)
{
for(int i=0;i<S->stacksize;i++)
{
S->base[i]=NULL;
}
S->top=S->base;
}
int StackEmpty(SqStack *S)
{
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack *S)
{
return S->top-S->base;
}
ElemType GetTop(SqStack *S)
{
if(S->top!=S->base)
{
return *(S->top-1);
}
else
{
return NULL;
}
}
int Push(SqStack *S,ElemType elem)
{
if(((S->top)-(S->base))==S->stacksize)
{
return FALSE;
}
else
{
S->top[0]=elem;
S->top++;
return TRUE;
}
}
ElemType Pop(SqStack *S)
{
if(S->top==S->base)
{
return NULL;
}
else
{
--S->top;
return S->top[0];
}
}
void StackTraverse(SqStack *S)
{
int i=0;
while(i<(S->top-S->base))
{
printf("%f\t",S->base[i]);
i++;
}
printf("\n");
}
SequentialStack_double.h
#ifndef SEQUENTIALSTACK_DOUBLE_H_INCLUDED
#define SEQUENTIALSTACK_DOUBLE_H_INCLUDED
typedef double ElemType;
#define TRUE 1
#define FALSE -1
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
extern "C" __declspec(dllexport) int InitStack(SqStack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(SqStack *S);
extern "C" __declspec(dllexport) void ClearStack(SqStack *S);
extern "C" __declspec(dllexport) int StackEmpty(SqStack *S);
extern "C" __declspec(dllexport) int StackLength(SqStack *S);
extern "C" __declspec(dllexport) ElemType GetTop(SqStack *S);
extern "C" __declspec(dllexport) int Push(SqStack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(SqStack *S);
extern "C" __declspec(dllexport) void StackTraverse(SqStack *S);
#endif // SEQUENTIALSTACK_DOUBLE_H_INCLUDED
SequentialStack_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialStack_int.h"
int InitStack(SqStack *S,int length)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*length);
S->top=S->base;
if(S->base!=NULL&&S->top!=NULL)
{
S->stacksize=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=NULL;
}
void ClearStack(SqStack *S)
{
for(int i=0;i<S->stacksize;i++)
{
S->base[i]=NULL;
}
S->top=S->base;
}
int StackEmpty(SqStack *S)
{
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack *S)
{
return S->top-S->base;
}
ElemType GetTop(SqStack *S)
{
if(S->top!=S->base)
{
return *(S->top-1);
}
else
{
return NULL;
}
}
int Push(SqStack *S,ElemType elem)
{
if(((S->top)-(S->base))==S->stacksize)
{
return FALSE;
}
else
{
S->top[0]=elem;
S->top++;
return TRUE;
}
}
ElemType Pop(SqStack *S)
{
if(S->top==S->base)
{
return NULL;
}
else
{
--S->top;
return S->top[0];
}
}
void StackTraverse(SqStack *S)
{
int i=0;
while(i<(S->top-S->base))
{
printf("%d\t",S->base[i]);
i++;
}
printf("\n");
}
SequentialStack_int.h
#ifndef SEQUENTIALSTACK_INT_H_INCLUDED
#define SEQUENTIALSTACK_INT_H_INCLUDED
typedef int ElemType;
#define TRUE 1
#define FALSE -1
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
extern "C" __declspec(dllexport) int InitStack(SqStack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(SqStack *S);
extern "C" __declspec(dllexport) void ClearStack(SqStack *S);
extern "C" __declspec(dllexport) int StackEmpty(SqStack *S);
extern "C" __declspec(dllexport) int StackLength(SqStack *S);
extern "C" __declspec(dllexport) ElemType GetTop(SqStack *S);
extern "C" __declspec(dllexport) int Push(SqStack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(SqStack *S);
extern "C" __declspec(dllexport) void StackTraverse(SqStack *S);
#endif // SEQUENTIALSTACK_INT_H_INCLUDED
SequentialStack_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SequentialStack_string.h"
int InitStack(SqStack *S,int length)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*length);
S->top=S->base;
if(S->base!=NULL&&S->top!=NULL)
{
S->stacksize=length;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=NULL;
}
void ClearStack(SqStack *S)
{
for(int i=0;i<S->stacksize;i++)
{
S->base[i]=NULL;
}
S->top=S->base;
}
int StackEmpty(SqStack *S)
{
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack *S)
{
return S->top-S->base;
}
ElemType GetTop(SqStack *S)
{
if(S->top!=S->base)
{
return *(S->top-1);
}
else
{
return NULL;
}
}
int Push(SqStack *S,ElemType elem)
{
if(((S->top)-(S->base))==S->stacksize)
{
return FALSE;
}
else
{
S->top[0]=elem;
S->top++;
return TRUE;
}
}
ElemType Pop(SqStack *S)
{
if(S->top==S->base)
{
return NULL;
}
else
{
--S->top;
return S->top[0];
}
}
void StackTraverse(SqStack *S)
{
int i=0;
while(i<(S->top-S->base))
{
printf("%s\t",S->base[i]);
i++;
}
printf("\n");
}
SequentialStack_string.h
#ifndef SEQUENTIALSTACK_STRING_H_INCLUDED
#define SEQUENTIALSTACK_STRING_H_INCLUDED
typedef const char* ElemType;
#define TRUE 1
#define FALSE -1
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
extern "C" __declspec(dllexport) int InitStack(SqStack *S,int length);
extern "C" __declspec(dllexport) void DestroyStack(SqStack *S);
extern "C" __declspec(dllexport) void ClearStack(SqStack *S);
extern "C" __declspec(dllexport) int StackEmpty(SqStack *S);
extern "C" __declspec(dllexport) int StackLength(SqStack *S);
extern "C" __declspec(dllexport) ElemType GetTop(SqStack *S);
extern "C" __declspec(dllexport) int Push(SqStack *S,ElemType elem);
extern "C" __declspec(dllexport) ElemType Pop(SqStack *S);
extern "C" __declspec(dllexport) void StackTraverse(SqStack *S);
#endif // SEQUENTIALSTACK_STRING_H_INCLUDED
SingleLinkedList_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedList_char.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedList_char.h
#ifndef SINGLELINKEDLIST_CHAR_H_INCLUDED
#define SINGLELINKEDLIST_CHAR_H_INCLUDED
typedef char ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLIST_CHAR_H_INCLUDED
SingleLinkedList_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedList_double.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedList_double.h
#ifndef SINGLELINKEDLIST_DOUBLE_H_INCLUDED
#define SINGLELINKEDLIST_DOUBLE_H_INCLUDED
typedef double ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLIST_DOUBLE_H_INCLUDED
SingleLinkedList_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedList_int.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedList_int.h
#ifndef SINGLELINKEDLIST_INT_H_INCLUDED
#define SINGLELINKEDLIST_INT_H_INCLUDED
typedef int ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLIST_INT_H_INCLUDED
SingleLinkedList_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedList_string.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->next=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
p=NULL;
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance->next!=NULL)
{
p=L->instance->next;
L->instance->next=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
LNode *p=L->instance;
if(p->next!=NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListLength(List *L)
{
LNode *p=L->instance;
int i=0;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance->next;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance->next;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance->next;
L->instance->next=s;
}
void addAfter(List *L,ElemType elem)
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
LNode *p=L->instance,*del;
int i=0;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance->next;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedList_string.h
#ifndef SINGLELINKEDLIST_STRING_H_INCLUDED
#define SINGLELINKEDLIST_STRING_H_INCLUDED
typedef const char* ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLIST_STRING_H_INCLUDED
SingleLinkedListWithoutHeadNode_char.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedListWithoutHeadNode_char.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
if(index==0)
{
addFirst(L,elem);
return TRUE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=1;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
LNode *temp=L->instance;
L->instance=temp->next;
free(temp);
temp=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
int i=1;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
del=NULL;
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%c\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedListWithoutHeadNode_char.h
#ifndef SINGLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
#define SINGLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
typedef char ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLISTWITHOUTHEADNODE_CHAR_H_INCLUDED
SingleLinkedListWithoutHeadNode_double.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedListWithoutHeadNode_double.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
if(index==0)
{
addFirst(L,elem);
return TRUE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=1;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
LNode *temp=L->instance;
L->instance=temp->next;
free(temp);
temp=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
int i=1;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
del=NULL;
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%f\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedListWithoutHeadNode_double.h
#ifndef SINGLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
#define SINGLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
typedef double ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLISTWITHOUTHEADNODE_DOUBLE_H_INCLUDED
SingleLinkedListWithoutHeadNode_int.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedListWithoutHeadNode_int.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
if(index==0)
{
addFirst(L,elem);
return TRUE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=1;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
LNode *temp=L->instance;
L->instance=temp->next;
free(temp);
temp=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
int i=1;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
del=NULL;
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%d\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedListWithoutHeadNode_int.h
#ifndef SINGLELINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
#define SINGLELINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
typedef int ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLISTWITHOUTHEADNODE_INT_H_INCLUDED
SingleLinkedListWithoutHeadNode_string.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SingleLinkedListWithoutHeadNode_string.h"
#define TRUE 1
#define FALSE -1
void InitList(List *L)
{
L->instance=NULL;
}
void DestroyList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
L->instance=NULL;
}
void ClearList(List *L)
{
LNode *p=L->instance;
while(L->instance!=NULL)
{
p=L->instance;
L->instance=p->next;
free(p);
}
}
int ListEmpty(List *L)
{
if(L->instance==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
int ListLength(List *L)
{
int i=0;
LNode *p=L->instance;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
ElemType GetElem(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return NULL;
}
LNode *p=L->instance;
int i=0;
while(i<index)
{
i++;
p=p->next;
}
return p->elem;
}
int LocateElem(List *L,ElemType elem)
{
LNode *p=L->instance;
int i=0;
while(i<ListLength(L)&&p!=NULL)
{
if(p->elem==elem)
{
return i;
}
p=p->next;
i++;
}
return -1;
}
ElemType PriorElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->next->elem==elem)
{
return p->elem;
}
p=p->next;
}
return NULL;
}
ElemType NextElem(List *L,ElemType elem)
{
LNode *p=L->instance;
while(p->next!=NULL)
{
if(p->elem==elem)
{
return p->next->elem;
}
p=p->next;
}
return NULL;
}
void addFirst(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
s->next=L->instance;
L->instance=s;
}
}
void addAfter(List *L,ElemType elem)
{
if(L->instance==NULL)
{
L->instance=(LNode*)malloc(sizeof(LNode));
L->instance->elem=elem;
L->instance->next=NULL;
}
else
{
LNode *s,*p=L->instance;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
while(p->next!=NULL)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
}
int ListInsert(List *L,int index,ElemType elem)
{
if(index<0||index>ListLength(L))
{
return FALSE;
}
if(index==0)
{
addFirst(L,elem);
return TRUE;
}
LNode *p=L->instance,*s;
s=(LNode*)malloc(sizeof(LNode));
s->elem=elem;
int i=1;
while(i<index)
{
i++;
p=p->next;
}
s->next=p->next;
p->next=s;
return TRUE;
}
int ListDelete(List *L,int index)
{
if(index<0||index>=ListLength(L))
{
return FALSE;
}
if(index==0)
{
LNode *temp=L->instance;
L->instance=temp->next;
free(temp);
temp=NULL;
return TRUE;
}
LNode *p=L->instance,*del;
int i=1;
while(i<index)
{
p=p->next;
i++;
}
del=p->next;
p->next=del->next;
free(del);
del=NULL;
return TRUE;
}
void TraverseList(List *L)
{
LNode *p=L->instance;
while(p!=NULL)
{
printf("%s\t",p->elem);
p=p->next;
}
printf("\n");
}
SingleLinkedListWithoutHeadNode_string.h
#ifndef SINGLELINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
#define SINGLELINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
typedef const char* ElemType; //It is changeble to change this data type.
typedef struct Node
{
ElemType elem;
struct Node *next;
}LNode;
typedef struct{
LNode* instance;
}List;
extern "C" __declspec(dllexport) void InitList(List *L);
extern "C" __declspec(dllexport) void DestroyList(List *L);
extern "C" __declspec(dllexport) void ClearList(List *L);
extern "C" __declspec(dllexport) int ListEmpty(List *L);
extern "C" __declspec(dllexport) int ListLength(List *L);
extern "C" __declspec(dllexport) ElemType GetElem(List *L,int index);
extern "C" __declspec(dllexport) int LocateElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType PriorElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) ElemType NextElem(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addFirst(List *L,ElemType elem);
extern "C" __declspec(dllexport) void addAfter(List *L,ElemType elem);
extern "C" __declspec(dllexport) int ListInsert(List *L,int index,ElemType elem);
extern "C" __declspec(dllexport) int ListDelete(List *L,int index);
extern "C" __declspec(dllexport) void TraverseList(List *L);
#endif // SINGLELINKEDLISTWITHOUTHEADNODE_STRING_H_INCLUDED
java转译代码见下篇。