有用的网站整理
实战题目2:
百度盘地址:http://www.itjiaocheng.com/coin/download.php?open=2&id=14301&uhash=522f05d911a99c52d3944f6e
提取码:mhwx
//
// main.c
// c_test
//
// Created by mac on 2021/2/9.
// Copyright © 2021年 mac. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//typedef char int8_t;
//typedef unsigned char uint8_t;
typedef struct _NODE_
{
int data;
struct _NODE_ *next;
struct _NODE_ *pre;
} NODE;
typedef struct
{
int listLen;
NODE *pNode;
} List;
void ListInit(List *list)
{
list->listLen = 0;
list->pNode = NULL;
}
void ListAddDataToTail(List *pList, int data)
{
NODE *pNode = NULL;
NODE *pTemData = NULL;
NODE *preNode = NULL;
if (NULL == pList)
{
printf("list add data err \r\n");
return;
}
// find the end
pNode = pList->pNode;
if (NULL == pNode)
{
}
else
{
for (int i = 0; i < pList->listLen; i++)
{
if (NULL == pNode->next)
{
break;
}
pNode = pNode->next;
}
}
//malloc the node
pTemData = (NODE*)malloc(sizeof(NODE));
if (NULL == pTemData)
{
printf("malloc node fail \r\n");
return;
}
//add node
pTemData->data = data;
pTemData->next = NULL;
pList->listLen ++;
//no node
if (NULL == pNode)
{
pList->pNode = pTemData;
pTemData->pre = NULL;
}
else
{
// first node
pTemData->pre = pNode;
pNode->next = pTemData;
}
printf("add node success \r\n");
}
void ListDelNodeFromTail(List *pList, int data)
{
NODE *pNode = NULL;
NODE *preNode = NULL;
NODE *nextNode = NULL;
if (NULL == pList
|| 0 == pList->listLen)
{
return;
}
//find the node
pNode = pList->pNode;
for (int i = 0; i < pList->listLen; i++)
{
if (NULL != pNode)
{
if (pNode->data == data)
{
break;
}
pNode = pNode->next;
}
else
{
pNode = NULL;
break;
}
}
if (NULL == pNode)
{
printf("del fail because no node had data :%d ", data);
return;
}
//del the node
preNode = pNode->pre;
nextNode = pNode->next;
if (NULL != preNode)
{
preNode->next = nextNode;
}
else
{
pList->pNode = nextNode;
}
if (NULL != nextNode)
{
nextNode->pre = preNode;
}
pList->listLen --;
printf("del success data :%d \r\n", pNode->data);
free(pNode);
}
void ListShowAllNode(List *pList)
{
NODE *pNode = NULL;
pNode = pList->pNode;
for (int i = 0; i < pList->listLen; i++)
{
if (NULL != pNode)
{
printf("data:%d \r\n", pNode->data);
}
else
{
printf("find list end \r\n");
break;
}
pNode = pNode->next;
}
printf("show list end \r\n");
}
void SortFunc(int *pBuf, int len)
{
int tmp = 0;
for (int i = 0; i < len; i++)
{
for (int j = i;j < len; j++)
{
if (pBuf[i] > pBuf[j])
{
tmp = pBuf[i] ;
pBuf[i] = pBuf[j];
pBuf[j] = tmp;
}
}
}
for (int i = 0; i < len; i++)
{
printf("%d,", pBuf[i]);
}
printf("\r\n");
}
int main ()
{
List list;
ListInit(&list);
/*
uint8_t num1 = 255;
uint8_t num2 = 0;
int8_t num3 = 127;
int8_t num4 = -128;
printf(" num1:%d ", ++num1);
printf(" num2:%d ", --num2);
printf(" num3:%d ", ++num3);
printf(" num4:%d ", --num4);
printf("\r\n");
*/
/*
ListAddDataToTail(&list, 1);
ListAddDataToTail(&list, 2);
ListAddDataToTail(&list, 3);
ListAddDataToTail(&list, 4);
ListAddDataToTail(&list, 5);
ListShowAllNode(&list);
ListDelNodeFromTail(&list, 3);
ListShowAllNode(&list);
*/
/*
int myBuf[] = {9, 7, 6, 2,8,5,1,0,77,67};
SortFunc(myBuf, sizeof(myBuf)/sizeof(int));
char data1[] = {"hello word\r\n"};
printf("%s", data1);
printf("%d\r\n", 3/(-2));
printf("%d\r\n", 3%(-2));
uint8_t k1 = 10;
int8_t k2 = -20;
printf("k1+k2:%d\r\n", k1+k2);
*/
char input[100] = {0};
char *pNum = NULL;
int len = 0;
while(1)
{
len = scanf("%s", input);
printf("len:%d \r\n", len);
if (len)
{
pNum = strstr(input,":")+1;
if(strstr(input, "add:"))
{
ListAddDataToTail(&list, atoi(pNum));
}
else if (strstr(input, "del:"))
{
ListDelNodeFromTail(&list, atoi(pNum));
}
else if (strstr(input, "plist"))
{
ListShowAllNode(&list);
}
else
{
printf("wrong cmd \r\n");
}
}
}
return 0;
}