C++控制台界面库_字符串单向链表

代码如下:

// LinkedList_string.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <windows.h>
#include <conio.h>

// 字符串链表类

typedef struct Node_string
{
char name[MAX_PATH];
struct Node_string* next;
}*PNode_string;

class LinkedList_string
{
public:

PNode_string head;

LinkedList_string(void);

void CreateFromDirectory(const char* directory, char file_leibie[], bool full_name);
void CreateFromFile(char file[]);
PNode_string FindNode(int ip);
int Length();
void Print();
char* NodeString(int ip);
void AddNodeAtTail(char* str);
void DeleteNode(int i);
void Combine(LinkedList_string temp);
void DeleteLb();
void SaveToFile(char* filename);
bool StringExsit(char* str);

};

LinkedList_string::LinkedList_string()
{
head = NULL;
}

void LinkedList_string::CreateFromDirectory(const char* directory, char file_leibie[], bool full_name)
{
DeleteLb();

Node_string* tail, * newnode;

tail = NULL;

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char pattern[MAX_PATH];

char p0[256];
strcpy_s(p0, 256, file_leibie);

char* p;
char* next_token = NULL;
p = strtok_s(p0, "| ", &next_token);

while (p != NULL)
{
	strcpy_s(pattern, MAX_PATH, directory);
	strcat_s(pattern, MAX_PATH, p);

	// 开始查找
	hFind = FindFirstFile(pattern, &FindFileData);

	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			newnode = (Node_string*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Node_string));
			if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
			{
				if (full_name)sprintf_s(newnode->name, MAX_PATH, "%s\\%s", directory, FindFileData.cFileName);
				else strcpy_s(newnode->name, MAX_PATH, FindFileData.cFileName);
			}

			if (head == NULL)
			{
				head = newnode;
				tail = newnode;
			}
			else
			{
				tail->next = newnode;
				tail = newnode;
			}
		} while (FindNextFile(hFind, &FindFileData) != 0);
	}

	// 查找结束
	FindClose(hFind);

	p = strtok_s(NULL, "| ", &next_token);
}

if (tail != NULL)tail->next = NULL;

}

void LinkedList_string::CreateFromFile(char file[])
{
DeleteLb();

FILE* fp;
errno_t err;
err = fopen_s(&fp, file, "r");
if (err != 0)return;
char str[256];

Node_string* tail, * newnode;
tail = NULL;

while (1)
{
	if (fgets(str, 256, fp) == NULL)break;
	str[strlen(str) - 1] = '\0';

	newnode = (Node_string*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Node_string));
	strcpy_s(newnode->name, MAX_PATH, str);

	if (head == NULL)
	{
		head = newnode;
		tail = newnode;
	}
	else
	{
		tail->next = newnode;
		tail = newnode;
	}
}

fclose(fp);

if (tail != NULL)tail->next = NULL;

}

Node_string* LinkedList_string::FindNode(int ip) //单链表的查找 ip为链表的查找位置
{
if (head == NULL) return NULL;

int count = 0;
Node_string* pNext = head;
while (count < ip && pNext != NULL)
{
	pNext = pNext->next;
	count++;
}
return pNext;

}

int LinkedList_string::Length()
{
int n = 0;
Node_string* temp;

temp = head;
while (temp != NULL)
{
	n++;
	temp = temp->next;
}
return n;

}

void LinkedList_string::Print()
{
Node_string* temp;

temp = head;
while (temp != NULL)
{
	//WriteConsole(hOut, temp->name, strlen(temp->name), NULL, NULL);
	//WriteConsole(hOut, "\n", 1, NULL, NULL);
	printf("%s\n", temp->name);
	temp = temp->next;
}

}

char* LinkedList_string::NodeString(int ip)
{
if (head == NULL)return NULL;

Node_string* pp;
pp = FindNode(ip);

return pp->name;

}

void LinkedList_string::AddNodeAtTail(char* str)
{
Node_string* temp, * newnode;

newnode = (Node_string*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Node_string));
strcpy_s(newnode->name, MAX_PATH, str);

if (head == NULL)
{
	head = newnode;
}
else
{
	temp = head;
	while (temp->next != NULL)
	{
		temp = temp->next;
	}

	temp->next = newnode;
}
newnode->next = NULL;

}

void LinkedList_string::DeleteNode(int i) //删除位置i的节点
{
Node_string* p = NULL;
Node_string* p1 = NULL;
Node_string* p2 = NULL;

if (head == NULL)  return;
if (i == 0)
{
	p = head;
	head = head->next;
	HeapFree(GetProcessHeap(), 0, p);
}
else
{
	p1 = FindNode(i - 1);         //上一个节点
	p = FindNode(i);
	p2 = FindNode(i + 1);         //下一个节点
	HeapFree(GetProcessHeap(), 0, p);
	p1->next = p2;
}

}

void LinkedList_string::Combine(LinkedList_string temp)
{
if (head == NULL)
{
head = temp.head;
return;
}

Node_string* pp;
pp = head;
while (pp->next != NULL)
	pp = pp->next;
pp->next = temp.head;

}

void LinkedList_string::DeleteLb()
{
Node_string* temp;

while (head != NULL)
{
	temp = head->next;
	HeapFree(GetProcessHeap(), 0, head);
	head = temp;
}

}

void LinkedList_string::SaveToFile(char* filename)
{
FILE* fp;
errno_t err;

err = fopen_s(&fp, filename, "w");
if (err != 0)
{
	return;
}

Node_string* temp;
temp = head;
while (temp != NULL)
{
	fprintf_s(fp, "%s\n", temp->name);
	temp = temp->next;
}
fclose(fp);

return;

}

bool LinkedList_string::StringExsit(char* str)
{
Node_string* temp;

temp = head;
while (temp != NULL)
{
	if (strcmp(temp->name, str) == 0)return true;
	temp = temp->next;
}
return false;

}

int main()
{
LinkedList_string string;
string.CreateFromDirectory(“D:\Kugou”, “\*.mp3”, false);
string.Print();
_getch();

return 0;

}

运行效果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/aier_wl/article/details/107696515