📄 tlink.cpp
字号:
// TLink.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <afx.h>
#define MAX_LEN 10
#define NULL_CHAR 0
#define MAX_OP 3
struct TLink
{
char *content;
TLink * next;
TLink()
{
content = NULL;
next = NULL;
}
~TLink()
{
content = NULL;
next = NULL;
}
};
TLink *pHead = NULL; //链表头指针
TLink *pTail = NULL; //链表尾指针
int number = 0; //链表的节点数
int AddElementToLinkTable()
{
printf("Please input content of element:\n");
printf("Input ret means return main menu!\n");
char *leakMem=NULL;
leakMem = new char[10];
memset(leakMem, 0xFF, 10);
char *pContent = NULL;
pContent = new char[MAX_LEN*10];
if( NULL == pContent)
{
printf("Error system allocate memory fail!\n");
return -1;
}
memset((void*)pContent,NULL,MAX_LEN);
while(1)
{
fflush(stdin);
fgets((char*)pContent,MAX_LEN,stdin);
if(NULL_CHAR == memcmp((const void*)pContent,"ret",MAX_OP)
|| 0 == strlen(pContent))
{
delete pContent;
return 0;
}
else
{
if(0 == number)
{
pHead = new TLink;
if(NULL == pHead)
{
printf("Error system allocate memory fail!\n");
return -1;
}
pHead->content = pContent;
pTail = pHead;
number++;
}
else
{
TLink *pNode = new TLink;
pNode->content= pContent;
//在尾部追加
pTail->next = pNode;
pTail = pNode;
//在头部添加
//pNode->next = pHead;
//pHead = pNode;
number++;
}
return 0;
}
}
}
int ModElementToLinkTable()
{
printf("Please input old content of element:\n");
printf("Input ret means return main menu!\n");
char content[100];
char newContent[MAX_LEN];
bool found;
while(1)
{
memset((void*)content,NULL,MAX_LEN);
fflush(stdin);
fgets((char*)content, MAX_LEN, stdin);
if(NULL_CHAR == memcmp((const void*)content,"ret",MAX_OP))
{
return 0;
}
else
{
TLink *pCurr = pHead;
while(NULL!=pCurr)
{
if(NULL_CHAR==memcmp((const void*)pCurr->content,(const void*)content,MAX_LEN))
{
printf("Find element:%s and Address is:%X\n",pCurr->content,&pCurr->content);
found = true;
}
pCurr=pCurr->next;
}
if( true != found)
{
printf("Not found input element!\n");
return -1;
}
while(1)
{
printf("input new content:");
memset((void*)newContent,NULL,MAX_LEN);
fflush(stdin);
fgets((char*)newContent, MAX_LEN, stdin);
if(NULL_CHAR == memcmp((const void*)newContent,"ret",MAX_OP))
{
return 0;
}
else
{
TLink *pCurrLink = pHead;
while(NULL!=pCurrLink)
{
if(NULL_CHAR==memcmp((const void*)pCurrLink->content,(const void*)content,MAX_LEN))
{
memset((void*)pCurrLink->content, NULL, MAX_LEN);
memcpy((void*)pCurrLink->content, (const void*)newContent, MAX_LEN);
printf("Modify element:%s and Address is:%X\n",pCurrLink->content,&pCurrLink->content);
}
pCurrLink=pCurrLink->next;
}
return 0;
}
return 0;
}
return 0;
}
return 0;
}
}
int DelElementToLinkTable()
{
printf("Please input old content of element:\n");
printf("Input ret means return main menu!\n");
char content[MAX_LEN];
while(1)
{
memset((void*)content,NULL,MAX_LEN);
fflush(stdin);
fgets((char*)content, MAX_LEN, stdin);
if(NULL_CHAR == memcmp((const void*)content,"ret",MAX_OP)
|| 0 >= strlen(content))
{
return 0;
}
else
{
TLink *pDel = pHead;
TLink *pPre = pHead;
while(NULL!=pDel)
{
if(NULL_CHAR==memcmp((const void*)pDel->content,(const void*)content,MAX_LEN))
{
break;
}
pPre=pDel;
pDel=pPre->next;
}
if(pDel==NULL)
{
printf("Not found node where equal input content\n");
return -1;
}
if(pDel==pHead)
{
pHead=pHead->next;
}
else if(pDel==pTail)
{
pTail = pPre;
pTail->next = NULL;
}
else
{
pPre->next = pDel->next;
}
delete pDel->content;
delete pDel;
pDel=NULL;
number--;
return 0;
}
return 0;
}
return 0;
}
int ClnElementToLinkTable()
{
printf("Clean all element from link table!\n");
TLink *pTmp = pHead;
while(NULL != pTmp && NULL != pTmp->content)
{
delete (pTmp->content);
pTmp->content = NULL;
pTmp = pTmp->next;
}
pHead = NULL;
number = 0;
return 0;
}
int PrnElementToLinkTable()
{
TLink *pCurr = pHead;
int i=1;
while(NULL != pCurr && NULL != pCurr->content)
{
printf("The %d content is:%s and next Addr is:0x%X\n",i,pCurr->content);
pCurr=pCurr->next;
i++;
}
return 0;
}
void printMenu()
{
printf("\n\t\tadd: Add element to link table\n");
printf("\t\tmod: Modify element in link table\n");
printf("\t\tdel: Delele element from link tablde\n");
printf("\t\tcln: Clean all element from link table\n");
printf("\t\tprn: Print all element from link table\n");
printf("\t\text: Exit link table test program!\n");
return ;
}
int main(int argc, char* argv[])
{
printf("\n\t\tEnter link table test program!\n");
int ret =0;
char input[MAX_LEN];
while(1)
{
printMenu();
printf("\t\tPlease input operater:");
memset((void*)input, NULL_CHAR, MAX_LEN);
fflush(stdin);
fgets((char*)input, MAX_LEN, stdin);
if(NULL_CHAR == memcmp((const void*)input,"add",MAX_OP))
{
ret = AddElementToLinkTable();
}
else if(NULL_CHAR == memcmp((const void*)input,"mod",MAX_OP))
{
ret = ModElementToLinkTable();
}
else if(NULL_CHAR == memcmp((const void*)input,"del",MAX_OP))
{
ret = DelElementToLinkTable();
}
else if(NULL_CHAR == memcmp((const void*)input,"cln",MAX_OP))
{
ret = ClnElementToLinkTable();
}
else if(NULL_CHAR == memcmp((const void*)input,"prn",MAX_OP))
{
printf("\n");
ret = PrnElementToLinkTable();
}
else if(NULL_CHAR == memcmp((const void*)input,"ext",MAX_OP))
{
return 0;
}
else
{
printf("Error input!\n");
}
if(ret!=0)
{
printf("Error happened Link Table Operater and return:%d\n",ret);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -