码迷,mamicode.com
首页 > 编程语言 > 详细

c语言实现单链表

时间:2016-05-08 01:19:23      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:c语言实现单链表

// 初始化单链表(对于无头结点单链表,该函数没有意义)
void InitList(PSListNode* pHead);
 
// 销毁单链表
void DestroyList(PSListNode* pHead);
 
// 尾插
void PushBack(PSListNode* pHead, DataType data);
 
// 尾出
void PopBack(PSListNode* pHead);
 
// 头插
void PushFront(PSListNode* pHead, DataType data);
 
// 头出
void PopFront(PSListNode* pHead);
 
// 在链表中查找元素data
PSListNode Find(PSListNode pHead, DataType data);
 
// 删除pos位置的结点(注意不能用那种替换形式)
void  Erase(PSListNode* pHead, PSListNode pos);
 
// 在链表的pos位置插入元素data
void  Insert(PSListNode* pHead, PSListNode pos, DataType data);
 
void PrintList(PSListNode pHead);
 
/////////////////////////////////////////////////
 
// 从头至尾打印单链表
void PrintListTailToHead(PSListNode pHead);
 
// 链表的非头结点前插入元素data
void InsertNotHead(PSListNode pos, DataType data);
 
// 删除链表的非尾结点
void DelNotTailNode(PSListNode pos);
 
// 查找链表的中间结点,要求只遍历一次链表
void FindMidNode(PSListNode pHead);


 

 

SList.h

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *pNext;
}SListNode, *PSListNode;
void InitList(PSListNode *pHead)
{
PSListNode pNode = *pHead;
pHead=pNode->data = 0;
pHead=pNode->pNext = NULL;
 
return 0;
}
void DestroyList(PSListNode* pHead)
{
PSListNode pNode=NULL;
PSListNode pPreNode = NULL;
assert(NULL != pHead);
{
pPreNode = pNode;
pNode = pNode->pNext;
free(pPreNode);
}
*pHead = NULL;
}
void PushBack(PSListNode *pHead, DataType data)
{
PSListNode pPrePos = *pHead;
assert(pHead);
if (NULL == *pHead)
{
*pHead = ByeNode(data);
}
else
{
PSListNode pNode = ByeNode(data);
while (pPrePos)
{
if (pPrePos->pNext == NULL)
{
pPrePos->pNext = pNode;
pNode->pNext = NULL;
}
pPrePos = pPrePos->pNext;
}
}
}
void PopBack(PSListNode* pSList)
{
PSListNode pCurNode = *pSList;
PSListNode pPreCurNode = pCurNode;
assert(pSList);
while (pCurNode->pNext)
{
pPreCurNode = pCurNode;
pCurNode->pNext;
}
free(pCurNode);
pPreCurNode->pNext = NULL;
 
}
 
// 头插
void PushFront(PSListNode* pHead, DataType data)
{
assert(pHead);
if (NULL == *pHead)
{
*pHead = ByeNode(data);
}
else
{
PSListNode pNode = ByeNode(data);
 
pNode->pNext = *pHead;
*pHead = pNode;
}
}
 
// 头出
void PopFront(PSListNode* pHead)
{
assert(NULL != pHead);
if (NULL != pHead)
{
return;
}
else
{
PSListNode pNode = *pHead;
*pHead = pNode->pNext;
free(pNode);
}
}
void PrintList(PSListNode pHead)
{
PSListNode pCurNode = pHead;
while (pCurNode)
{
printf("%d->", pCurNode->data);
pCurNode = pCurNode->pNext;
}
printf("NULL\n");
}
 
// 在链表中查找元素data
PSListNode Find(PSListNode pHead, DataType data)
{
PSListNode pNode = pHead;
while (pNode)
{
if (pNode->data == data)
{
return pNode;
}
 
pNode = pNode->pNext;
}
return NULL;
}
 
 
// 删除pos位置的结点(注意不能用那种替换形式)
void  Erase(PSListNode* pHead, PSListNode pos)
{
PSListNode pPrePos = *pHead;
assert(NULL != pHead);
assert(NULL != pos);
//如果是头结点
if (pos == *pHead)
{
*pHead = pos->pNext;
free(pos);
return;
}
while (pPrePos)
{
if (pPrePos->pNext == pos)
{
pPrePos->pNext = pos->pNext;
free(pos);
break;
}
pPrePos = pPrePos->pNext;
}
}
 
// 在链表的pos位置插入元素data
void  Insert(PSListNode* pHead, PSListNode pos, DataType data)
{
PSListNode pNewNode = NULL;//申请空间
assert(NULL != pHead);
assert(NULL != pos);
if (NULL == *pHead)
{
return;
}
pNewNode = ByeNode(data);
if (NULL != pNewNode)
{
pNewNode->pNext = pos->pNext;
pos->pNext = pNewNode;
}
}
 
void PrintList(PSListNode pHead);
 
// 从头至尾打印单链表
void PrintListTailToHead(PSListNode pHead)
{
if (NULL != pHead)
{
PrintListTaiToHaed(pHead->pNext);
printf("%d", pHead->data);
}
}
 
 
// 删除链表的非尾结点
void DelNotTailNode(PSListNode pos)
{
PSListNode pDelNode = NULL;
assert(pos != NULL);
assert(pos->pNext != NULL);
pDelNode = pos->pNext;
pos->data = pDelNode->data;
pos->pNext = pDelNode->pNext;
free(pDelNode);
}
 
// 查找链表的中间结点,要求只遍历一次链表
void FindMidNode(PSListNode pHead)
{
PSListNode pSlow = pHead;
PSListNode pFast = pHead;
 
while (pFast != NULL&&pFast->pNext != NULL)
{
pFast = pFast->pNext->pNext;
pSlow = pSlow->pNext;
}
 
return pSlow;
}
 
 
ListTest.c
#include"SList.h"
#include<stdio.h>
#include<stdlib.h>
 
 
void FunTest1()
{
PSListNode pNode = NULL;
PSListNode pList;
InitList(&pList);
PushFront(&pList, 1);
PushFront(&pList, 2);
PushFront(&pList, 3);
PushFront(&pList, 4);
PrintList(pList);
}
 
int main()
{
FunTest1();
system("pause");
return 0;
}


c语言实现单链表

标签:c语言实现单链表

原文地址:http://frankenstein.blog.51cto.com/10918184/1771101

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!