#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
char data[64];
Node* next;
}Node;
int createList(Node** list) {
int ret = 0;
Node* pHead = NULL;
Node* pCur = NULL;
Node* pMalloc = NULL;
char data[64] = { 0 };
printf("请输入数据,以-1结束:\n");
scanf("%s", data);
if (strcmp(data, "-1") == 0)
{
ret = -1;
goto END;
}
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data, data);
pMalloc->next = NULL;
pHead = pMalloc;
pCur = pHead;
while (1) {
scanf("%s", data);
if (strcmp(data, "-1") == 0)
break;
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data, data);
pMalloc->next = NULL;
pCur->next = pMalloc;
pCur = pMalloc;
}
END:
*list = pHead;
return ret;
}
int printList(Node* list) {
while (list) {
puts(list->data);
list = list->next;
}
printf("*****************\n");
return 0;
}
int insertList(Node** list, int pos) {
int ret = 0;
Node* pMalloc = NULL;
Node* pCur = *list;
char data[64] = { 0 };
if (*list == NULL) {
printf("空链表!\n");
ret = -1;
goto END;
}
if (pos == 0) {
printf("请输入插入数据:");
scanf("%s", data);
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data, data);
pMalloc->next = *list;
*list = pMalloc;
goto END;
}
for (int i = 0; i < pos - 1; i++) {
pCur = pCur->next;
}
printf("请输入插入数据:");
scanf("%s", data);
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data, data);
pMalloc->next = pCur->next;
pCur->next = pMalloc;
END:
return ret;
}
int deleteList(Node** list, int pos) {
int ret = 0;
Node* pCur = *list;
if (*list == NULL) {
printf("空链表!\n");
ret = -1;
goto END;
}
else if (pos == 0)
{
*list = pCur->next;
goto END;
}
for (int i = 0; i < pos - 1; i++) {
pCur = pCur->next;
}
pCur->next = (pCur->next)->next;
END:
return ret;
}
int reverseList(Node** list) {
Node* pCur = *list;
Node* pNext = pCur->next;
Node* pTmp = NULL;
pCur->next = NULL;//链表头变为链表尾
while (pNext) {
pTmp = pNext->next;
pNext->next = pCur;
pCur = pNext;
pNext = pTmp;
}
*list = pCur;//链表尾变为链表头
return 0;
}
int destroyList(Node* list) {
Node* pNext = NULL;
while (list) {
pNext = list->next;
free(list);
list = pNext;
}
return 0;
}
//替换节点元素
int replaceList(Node** list,int pos) {
int ret = 0;
Node* pMalloc = NULL;
Node* pCur = *list;
char data[64] = {0};
if (pos==0)
{
printf("请输入数据:");
scanf("%s", data);
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data,data);
pMalloc->next = (*list)->next;
*list = pMalloc;
goto END;
}
for (int i = 0; i < pos -1; i++) {
pCur = pCur->next;
}
printf("请输入数据:");
scanf("%s", data);
pMalloc = (Node*)malloc(sizeof(Node));
strcpy(pMalloc->data, data);
pMalloc->next = (pCur->next)->next;
pCur->next = pMalloc;
END:
return ret;
}
//查找链表中target第一次出现的位置(下标)
int findList(Node* list,const char* target,int *pos) {
Node* pCur = list;
*pos = 0;
while (pCur)
{
if (strcmp(pCur->data,target)==0)//找到了
{
break;
}
pCur = pCur->next;
(*pos)++;
}
return 0;
}
int main() {
Node* list = NULL;
int pos;
char data[64];
createList(&list);
printList(list);
printf("请输入插入位置:");
scanf("%d", &pos);
insertList(&list, pos);
printList(list);
printf("请输入删除位置:");
scanf("%d", &pos);
deleteList(&list, pos);
printList(list);
printf("请输入修改位置:");
scanf("%d", &pos);
replaceList(&list, pos);
printList(list);
printf("请输入查找数据:");
scanf("%s", data);
findList(list, data, &pos);
printf("目标首次位置:%d\n",pos);
printf("链表逆序:\n");
reverseList(&list);
printList(list);
destroyList(list);
system("pause");
return 0;
}