jia_xue 2016-11-05 09:01 采纳率: 0%
浏览 734
已采纳

链表倒置这一块不对,大神帮忙看看

#ifndef LIST_H
#define LIST_H

#define LIST_SIZE 10

enum LIST_TYPE
{
LIST_ERROR = -1,
LIST_OK
};

typedef int data_t;

typedef struct List List;

List *CreateList();

void DestoryList(List *pList);

int InsertItemList(List *pList, data_t tData, int iOffset);

List *ReverseList(List *pList);

int DelItem(List *pList, data_t *pData, int iOffset);

int UpdateItem(List *pList, data_t tNewData, data_t tOldData);

int SearchItem(List *pList, data_t tData);

void ShowList(List *pList);

#endif //_LIST_H_

#include
#include
#include
#include "list.h"

struct List
{
data_t data;
struct List *pNext;
};

List *CreateList()
{
List *pList = NULL;
pList = (List *) malloc (sizeof(List));
if (NULL == pList)
{
return pList;
}
memset(pList, 0, sizeof(List));
pList->pNext = NULL;
return pList;
}

void DestoryList(List *pList)
{
if (NULL == pList)
{
return;
}
//free data node
List *pNode = pList->pNext;
List *pTmp = NULL;
while(NULL != pNode)
{
pTmp = pNode->pNext;
free(pNode);
pNode = pTmp;
}
//free Header
free(pList);
pList = NULL;
}

int InsertItemList(List *pList, data_t tData, int iOffset)
{
if (NULL == pList)
{
return LIST_ERROR;
}
//create link node
List *pNode = (List *) malloc (sizeof(List));
if (NULL == pNode)
{
return LIST_ERROR;
}
memset(pNode, 0, sizeof(List));
pNode->data = tData;
pNode->pNext = NULL;
//insert node
pNode->pNext = pList->pNext;
pList->pNext = pNode;
return LIST_OK;
}

List *ReverseList(List *pList)
{
if (NULL == pList || NULL == pList->pNext)
{
return pList;
}
List *current = pList;
List *next = current->pNext;
List *prev = NULL;
while (next)
{
prev = next->pNext;
next->pNext = current;
current = next;
next = prev;
}
pList->pNext = NULL;
pList = current;
return pList;
}

int DelItem(List *pList, data_t *pData, int iOffset)
{
if (NULL == pList || NULL == pData)
{
return LIST_ERROR;
}
//free first data node
List *pDel = pList->pNext;
if (NULL != pDel)
{
*pData = pDel->data;
pList->pNext = pDel->pNext;
free(pDel);
pDel = NULL;
}
return LIST_OK;
}

int UpdateItem(List *pList, data_t tNewData, data_t tOldData)
{
if (NULL == pList)
{
return LIST_ERROR;
}
List *pNode = pList->pNext;
while(NULL != pNode)
{
if (pNode->data == tOldData)
{
pNode->data = tNewData;
return LIST_OK;
}
pNode = pNode->pNext;
}
return LIST_ERROR;
}

int SearchItem(List *pList, data_t tData)
{
if (NULL == pList)
{
return LIST_ERROR;
}
List *pNode = pList->pNext;
while(NULL != pNode)
{
if (pNode->data == tData)
{
return LIST_OK;
}
pNode = pNode->pNext;
}
return LIST_ERROR;
}

void ShowList(List *pList)
{
if (NULL == pList)
{
return;
}
List *pTmp = pList->pNext;
while(NULL != pTmp)
{
printf("%d ", pTmp->data);
pTmp = pTmp->pNext;
}
printf("\r\n");
}

#include
#include "list.h"

int main()
{
//create
List *pList = CreateList();
if (NULL == pList)
{
return -1;
}
printf("create ok\r\n");
//insert
int i = 5;
int iRet = -1;
while(i--)
{
iRet = InsertItemList(pList, i, 0);
if (LIST_ERROR == iRet)
{
printf("insert error\r\n");
break;
}
}
//show list
ShowList(pList);

pList = ReverseList(pList);
if (NULL == pList)
{
    return -1;
}
printf("Reverse ok\r\n");

ShowList(pList);

//delete item
data_t tData = -1;
iRet = DelItem(pList, &tData, 2);
printf("iret %d tdata %d\r\n", iRet, tData);
ShowList(pList);

//search
iRet = SearchItem(pList, 3);
printf("index %d\r\n", iRet);
//update
iRet = UpdateItem(pList, 8, 1);
printf("update iret %d\r\n", iRet);
//pList->iCount = 100;
ShowList(pList);
//destory list
DestoryList(pList);
pList = NULL;
return 0;

}

编译结果
create ok
0 1 2 3 4

Reverse ok
3 2 1 0 0

iret 0 tdata 3
2 1 0 0

index -1
update iret 0
2 8 0 0

倒置为什么没有最后一个

  • 写回答

3条回答 默认 最新

  • YXTS122 2016-11-05 11:40
    关注

    楼主可以自己调试一下,,,,,,,,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 9月28日

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序