linkedlist1
来源:互联网 发布:最全大众网络理财产品 编辑:程序博客网 时间:2024/06/13 03:24
linked_list.c
#include "linked_list.h"
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
void printInfo(Info* pInfo)
{
printf("%04d-%02d-%02d:%s", pInfo->date.year, pInfo->date.month, pInfo->date.day, pInfo->text);
}
void freeInfo(Info* pInfo)
{
if (pInfo != NULL)
{
free(pInfo->text);
free(pInfo);
}
}
/*创建一个空的节点*/
LinkedListNode* createNode()
{
LinkedListNode *pNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
pNode->data = NULL;
pNode->next = NULL;
return pNode;
}
/*创建一个链表*/
LinkedList* createList()
{
LinkedList* pList = NULL;
pList = (LinkedList*)malloc(sizeof(LinkedList));
pList->head = pList->tail = NULL;
pList->size = 0;
return pList;
}
/*从头部插入一个节点*/
void pushFront(LinkedList* pList, void* pData)
{
LinkedListNode* pNode = createNode();
pNode->data = pData;
if (NULL == pList->head && NULL == pList->tail)
{
pList->head = pList->tail = pNode;
pList->size++;
}
else
{
pNode->next = pList->head;
pList->head = pNode;
pList->size++;
}
}
/*从文件初始化链表*/
int initList(LinkedList* pList, char* pPath, int nInsertFlag)
{
FILE* fp = NULL;
Info* pInfo = NULL;
int size = 0;
int i = 0;
fp = fopen(pPath, "r");
if (NULL == fp)
{
return -1;
}
fscanf(fp, "%d", &size);
for (i=0; i<size; ++i)
{
pInfo = (Info*)malloc(sizeof(Info));
fscanf(fp, "%d/%d/%d\n", &pInfo->date.day, &pInfo->date.month, &pInfo->date.year);
pInfo->text = (char*)malloc(sizeof(char)*101);
fgets(pInfo->text, 101, fp);
if (0 == nInsertFlag)
{
pushFront(pList, (void*)pInfo);
}
else
{
pushBack(pList, (void*)pInfo);
}
}
fclose(fp);
return 0;
}
/*从尾部插入一个节点*/
void pushBack(LinkedList* pList, void* pData)
{
LinkedListNode* pNode = createNode();
pNode->data = pData;
if (NULL==pList->head && NULL==pList->tail)
{
pList->head = pList->tail = pNode;
pList->size++;
}
else
{
pList->tail->next = pNode;
pList->tail = pNode;
pList->size++;
}
}
/*从头部删除一个节点*/
LinkedListNode* popFront(LinkedList* pList)
{
LinkedListNode* pNode = NULL;
if (NULL==pList || 0==pList->size)
{
return NULL;
}
if (pList->head == pList->tail)
{
pNode = pList->head;
pList->head = pList->tail = NULL;
pList->size--;
}
else
{
pNode = pList->head;
pList->head = pList->head->next;
pList->size--;
}
return pNode;
}
/*从尾部删除一个节点*/
LinkedListNode* popBack(LinkedList* pList)
{
LinkedListNode* pTemp = NULL;
LinkedListNode* pNode = NULL;
if (NULL==pList || 0==pList->size)
{
return NULL;
}
if (pList->head == pList->tail)
{
pNode = pList->head;
pList->head = pList->tail = NULL;
pList->size--;
return pNode;
}
pTemp = pNode;
while (pTemp->next != pList->tail)
{
pTemp = pTemp->next;
}
pNode = pList->tail;
pList->tail = pTemp;
pList->size--;
return pNode;
}
/*查找位置为i的节点*/
LinkedListNode* FindNode(LinkedList* pList, int i)
{
LinkedListNode* pTemp = pList->head;
int j = 0;
for (j=0; j<i; j++)
{
if (pTemp == NULL)
{
break;
}
pTemp = pTemp->next;
}
return pTemp;
}
/*打印这个链表*/
void print(LinkedList* pList)
{
int i=0;
LinkedListNode* pTemp = pList->head;
for (i=0; i<pList->size; ++i)
{
if (pTemp == NULL)
{
break;
}
printInfo((Info*)(pTemp->data));
pTemp = pTemp->next;
}
}
/*销毁这个链表*/
void destroy(LinkedList* pList)
{
LinkedListNode* pTemp = pList->head;
LinkedListNode* pNext = NULL;
while (pTemp != NULL)
{
pNext = pTemp->next;
freeInfo((Info*)pTemp->data);
free(pTemp);
pTemp = pNext;
}
free(pList);
}
linked_list.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
/*定义时间*/
typedef struct {
int day;
int month;
int year;
}Date;
typedef struct {
Date date;
char *text;
}Info;
void printInfo(Info* pInfo);
void freeInfo(Info* pInfo);
typedef struct LinkedListNode {
void* data;
struct LinkedListNode* next;
} LinkedListNode;
typedef struct {
LinkedListNode* head;
LinkedListNode* tail;
int size;
}LinkedList;
/*创建一个空的节点*/
LinkedListNode* createNode();
/*创建一个链表*/
LinkedList* createList();
/*从文件初始化链表, nInsertFlag: 0表示从头部插入,1表示从尾部插入s*/
int initList(LinkedList* pList, char* pPath, int nInsertFlag);
/*从头部插入一个节点*/
void pushFront(LinkedList* pList, void* pData);
/*从尾部插入一个节点*/
void pushBack(LinkedList* pList, void* pData);
/*从头部删除一个节点*/
LinkedListNode* popFront(LinkedList* pList);
/*从尾部删除一个节点*/
LinkedListNode* popBack(LinkedList* pList);
/*查找位置为i的节点*/
LinkedListNode* FindNode(LinkedList* pList, int i);
/*打印这个链表*/
void print(LinkedList* pList);
/*销毁这个链表*/
void destroy(LinkedList* pList);
#endif
main.c
#include "linked_list.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int cmdParam = 0;
LinkedList* pList = NULL;
LinkedListNode* pNode = NULL;
if (argc != 2)
{
printf("error, 2 parameters");
return -1;
}
cmdParam = atoi(argv[1]);
pList = createList();
/*从头部插入测试*/
initList(pList, "./test.txt", 0);
pNode = FindNode(pList, cmdParam);
if (NULL == pNode)
printf("the index:%d is out of rangle\n", cmdParam);
else
{
printInfo((Info*)pNode->data);
}
destroy(pList);
return 0;
}
- linkedlist1
- Linux中目录可执行权限的含义
- 理解Python的With语句
- 二叉树常见非递归操作
- python虚拟环境安装和配置
- java LIST 与 SET 判断是否为空
- linkedlist1
- stm32两个看门狗
- 第七周项目二:周薪发放
- 解决a different object with the same identifier value was already associated with the session错误
- view的dispatchTouchEvent方法
- Jaxb2 实现JavaBean与xml互转
- stm32 DMA
- 浅析.NET中的引用类型和值类型(上)
- JDK环境变量配置