最近重新领悟C语言,为了练习,简单写了一个双向链栈,目的是温习一下基本的数据结构与模型,开发环境选用最原始的linux环境下的vim编辑器,主要涉及一些比较基础的指针的操作与数据的组织,内存上的考虑可能不是特别全面,希望大家多多指正。
1. sudo touch biNode.h
sudo touch biNode.c
sudo touch main.c
2. 使用vim -O biNode.h biNode.c main.c打开vim 编辑器编写代码如下:
biNode.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//data releam defination
typedef struct DataStruct
{
char *idName;
int idNumber;
}Data;
//node struct defination
typedef struct BiNodeStruct
{
Data *data;
struct BiNodeStruct *prevNode;
struct BiNodeStruct *nextNode;
}Node;
//stack structure defination
typedef struct NodeListStruct
{
int length;
Node *currentNode;
Node *firstNode;
Node *lastNode;
}NodeList;
//init stack dataset
void initNodeList(Node *, NodeList *);
//push element to this dataset
void pushNodeListFront(Node *, NodeList *);
void pushNodeListBack(Node *, NodeList *);
//pop element from this dataset
void popNodeListFront(NodeList *);
void popNodeListBack(NodeList *);
//print the elemment in your dataset
void printNodeList(NodeList *);
//return the number of total element in your dataset
int lengthNodeList(NodeList *);
//element exsist or not
bool isExistElement(Data *, NodeList *);
//return element position
int nodePositionNodeList(Data *, NodeList *);
//find specifuc position of element
Node * findNode(int, NodeList *);
biNode.c
#include "biNode.h"
//init stack dataset
void initNodeList(Node *node, NodeList *nodeList)
{
node->prevNode = NULL;
node->nextNode = NULL;
nodeList->length = 1;
nodeList->currentNode = node;
nodeList->firstNode = node;
nodeList->lastNode = node;
}
//push element to this dataset
void pushNodeListFront(Node *node, NodeList *nodeList)
{
nodeList->currentNode = node;
if(nodeList->firstNode->prevNode == NULL)
{
nodeList->currentNode->prevNode = NULL;
nodeList->currentNode->nextNode = nodeList->firstNode;
nodeList->firstNode->prevNode = nodeList->currentNode;
nodeList->firstNode = nodeList->currentNode;
}
nodeList->length++;
}
void pushNodeListBack(Node *node, NodeList *nodeList)
{
nodeList->currentNode = node;
if(nodeList->lastNode->nextNode == NULL)
{
nodeList->currentNode->nextNode = NULL;
nodeList->lastNode->nextNode = nodeList->currentNode;
nodeList->currentNode->prevNode = nodeList->lastNode;
nodeList->lastNode = nodeList->currentNode;
}
nodeList->length++;
}
//pop element from this dataset
void popNodeListFront(NodeList *nodeList)
{
if(nodeList->currentNode != NULL)
{
nodeList->currentNode = nodeList->firstNode;
}
if(nodeList->firstNode->nextNode != NULL)
{
nodeList->firstNode = nodeList->firstNode->nextNode;
nodeList->firstNode->prevNode = NULL;
}
printf("delete element idNumber = %d, idname = %s\n",nodeList->currentNode->data->idNumber, nodeList->currentNode->data->idName);
free(nodeList->currentNode);
printf("delete address is %p\n", nodeList->currentNode);
nodeList->currentNode = nodeList->firstNode;
nodeList->length--;
}
void popNodeListBack(NodeList *nodeList)
{
if(nodeList->currentNode != NULL)
{
nodeList->currentNode = nodeList->lastNode;
}
if(nodeList->lastNode->prevNode != NULL)
{
nodeList->lastNode = nodeList->lastNode->prevNode;
nodeList->lastNode->nextNode = NULL;
}
printf("delete element idNumber = %d, idname = %s\n",nodeList->currentNode->data->idNumber, nodeList->currentNode->data->idName);
free(nodeList->currentNode);
nodeList->currentNode = NULL;
nodeList->currentNode = nodeList->lastNode;
nodeList->length--;
}
//print the elemment in your dataset
void printNodeList(NodeList *nodeList)
{
Node *tmpVp = nodeList->firstNode;
for(int i = 1; i <= nodeList->length; i++)
{
tmpVp = findNode(i, nodeList);
printf(" element idNumber = %d, idname = %s\n",tmpVp->data->idNumber, tmpVp->data->idName);
}
printf("###############total numnber is %d\n", nodeList->length);
}
//return the number of total element in your dataset
int lengthNodeList(NodeList *nodeList)
{
return nodeList->length;
}
//element exsist or not
bool isExistElement(Data *dataTmp, NodeList *nodeList)
{
Node *tmpVp = nodeList->firstNode;
while(tmpVp <= nodeList->lastNode)
{
printf("-----------------------------------\n");
if(strcmp(tmpVp->data->idName, dataTmp->idName) == 0 || tmpVp->data->idNumber == dataTmp->idNumber)
return true;
tmpVp = tmpVp->nextNode;
}
return false;
/* Node* tmpVp;
for(int i = 1; i <= nodeList->length;i++)
{
tmpVp = findNode(i, nodeList);
if(strcmp(tmpVp->data->idName, dataTmp->idName) == 0 && tmpVp->data->idNumber == dataTmp->idNumber)
return true;
}
return false;*/
}
//return element position
int nodePositionNodeList(Data *dataTmp, NodeList * nodeList)
{
Node *tmpVp;
for(int i = 1; i <= nodeList->length;i++)
{
tmpVp= findNode(i, nodeList);
if(strcmp(tmpVp->data->idName, dataTmp->idName) == 0 && tmpVp->data->idNumber == dataTmp->idNumber)
{
return i;
}
}
}
//find specifuc position of element
Node * findNode(int position, NodeList *nodeList)
{
if(position <= nodeList->length)
{
Node *tmpVp = nodeList->firstNode;
int circleNumber = position;
while(circleNumber > 1)
{
circleNumber--;
tmpVp = tmpVp->nextNode;
}
return tmpVp;
}else{
printf("-----position crossfield-----\n");
}
}
main.c
#include "biNode.h"
int main()
{
Node *testA = (Node *)malloc(sizeof(Node));
testA->data = (Data *)malloc(sizeof(Data));
testA->data->idName = "Dashan";
testA->data->idNumber = 599;
Node *testB = (Node *)malloc(sizeof(Node));
testB->data = (Data *)malloc(sizeof(Data));
testB->data->idName = "Xiaoming";
testB->data->idNumber = 377;
Node *testC = (Node *)malloc(sizeof(Node));
testC->data = (Data *)malloc(sizeof(Data));
testC->data->idName = "Liukaka";
testC->data->idNumber = 37;
NodeList *testList = (NodeList *)malloc(sizeof(NodeList));
initNodeList(testA, testList);
pushNodeListFront(testB, testList);
pushNodeListBack(testC, testList);
printf("node length number is %d\n", testList->length);
printNodeList(testList);
popNodeListFront(testList);
printNodeList(testList);
popNodeListBack(testList);
printNodeList(testList);
printf("-----%d\n", lengthNodeList(testList));
Node *testD = (Node *)malloc(sizeof(Node));
testD->data = (Data *)malloc(sizeof(Data));
testD->data->idName = "Newson";
testD->data->idNumber = 4555;
Node *testE = (Node *)malloc(sizeof(Node));
testE->data = (Data *)malloc(sizeof(Data));
testE->data->idName = "BonSB";
testE->data->idNumber = 449;
pushNodeListFront(testD, testList);
pushNodeListBack(testE, testList);
Data *testdata = (Data *)malloc(sizeof(Data));
testdata->idName = "BonSB";
testdata->idNumber = 449;
if(isExistElement(testdata, testList))
{
printf("Existalready\n");
}
printNodeList(testList);
printf("length number is %d\n", nodePositionNodeList(testdata, testList));
return 0;
}
3.使用sudo gcc biNode.h biNode.c main.c -o bi.o进行编译
4.运行./bi.o
结果如下:
node length number is 3
element idNumber = 377, idname = Xiaoming
element idNumber = 599, idname = Dashan
element idNumber = 37, idname = Liukaka
###############total numnber is 3
delete element idNumber = 377, idname = Xiaoming
delete address is 0x1839050
element idNumber = 599, idname = Dashan
element idNumber = 37, idname = Liukaka
###############total numnber is 2
delete element idNumber = 37, idname = Liukaka
element idNumber = 599, idname = Dashan
###############total numnber is 1
-----1
-----------------------------------
-----------------------------------
-----------------------------------
Existalready element idNumber = 4555, idname = Newson
element idNumber = 599, idname = Dashan
element idNumber = 449, idname = BonSB
###############total numnber is 3
length number is 3