单链表的插入删除查找

#include"seqlist.h"
pLinkList CrtatNode()
{
pLinkList node = (pLinkList) malloc(sizeof(pLinkList));
node->next = NULL;
return node;
}
 void DestroyNode(pLinkList node)
{
free(node);
}
 void InitNode(pLinkList* phead)
{
HAND;
*phead = CrtatNode();
(*phead)->next = NULL;
printf("初始化完成\n");
}
 
 void pLinkListPushFront(pLinkList* phead, ElemType value)
 {
HAND;
if (phead == NULL)
{
return;
}


pLinkList temp;
pLinkList Node;
if ((*phead)->next == NULL)
{
Node = CrtatNode();
Node->value = value;
(*phead)->next=Node;
Node->next = NULL;
 }
else
{
temp = *phead;
pLinkList after_temp ;
Node = CrtatNode();
Node->value = value;
after_temp = temp->next;
temp->next = Node;
Node->next = after_temp;
printf("插入成功\n");
}
LinkListPrint(phead);
 }
 int LinkListLength(pLinkList* phead)
 {
HAND;
int len = 0;
pLinkList p;
p = (*phead)->next;
while (p != NULL)
{
len++;
p = p->next;
}
printf("len=%d\n", len);
return len;
 }
 void pLinkListPopFront(pLinkList* phead)
 {
HAND;
if ((*phead)->next == NULL)
{
printf("weikong");
return;
}
else {


pLinkList temp = *phead;
pLinkList del = temp->next;
temp->next = temp->next->next;
DestroyNode(del);
del = NULL;
printf("头删成功\n");
}
LinkListPrint(phead);
 }
 void pLinkListPushBack(pLinkList* phead, ElemType value)
 {
HAND;
pLinkList temp;
pLinkList Node;
if ((*phead)->next == NULL)
{
Node = CrtatNode();
Node->value = value;
(*phead)->next = Node;
Node->next = NULL;
}
else if ((*phead)->next!=NULL)
{
temp = *phead;
while (temp->next)
{
temp = temp->next;
}
 
Node = CrtatNode();
Node->value = value;
 temp->next=Node;
 Node->next=NULL;
printf("插入成功\n");
}
LinkListPrint(phead);
 }
 void pLinkListPopBack(pLinkList* phead)
 {
HAND;
pLinkList before_temp = NULL;
if ((*phead)->next == NULL)
{
printf("weikong");
return;
}
else 
{
pLinkList temp=*phead;
 while (temp->next)
{
before_temp = temp;
temp = temp->next;
}
DestroyNode(temp);
before_temp->next = NULL;
printf("尾删成功\n");
}
LinkListPrint(phead);
 }
 pLinkList LinkListFindPos(pLinkList* phead, ElemType value)
 {
HAND;
if ((*phead)->next == NULL)
{
printf("空链表\n");
return;
}
else
{
pLinkList temp = (*phead);
while (temp->next != NULL)
{
if (temp->next->value = value)
{
printf("%d的位置", temp->next->value);
printf("[%p]->[%d]\n", temp->next, temp->next->value);
return temp->next;
}
temp = temp->next;
}
printf("链表没有此元素\n");
}
printf("\n");
 }
 void InsertBeforePos(pLinkList* phead, int pos, ElemType value)
 {
HAND;
 if ((*phead)->next == NULL)
{
printf("链表为空\n");
pLinkListPushBack(phead, value);
return;
}
else
{
pLinkList temp = *phead; 
pLinkList Node;
Node = CrtatNode();
Node->value = value;
pLinkList after_tmp;
int ret = pos - 1;
while (ret--)
{
temp = temp->next;
}
after_tmp = temp->next;
temp->next = Node;
Node->next = after_tmp;
printf("在%d的位置之前插入[%d]成功", pos, value);
}
 
LinkListPrint(phead);
 }
 void InsertAfterPos(pLinkList* phead, int pos, ElemType value)
 {
HAND;
if ((*phead)->next == NULL)
{
printf("链表为空\n");
pLinkListPushBack(phead, value);
return;
}
else
{
pLinkList temp = *phead;
pLinkList Node;
Node = CrtatNode();
Node->value = value;
pLinkList after_tmp;
 
while (pos--)
{
temp = temp->next;
}
after_tmp = temp->next;
temp->next = Node;
Node->next = after_tmp;
printf("在%d的位置之后插入[%d]成功", pos, value);
}
 
LinkListPrint(phead);
 }
 Deletepos(pLinkList* phead, int pos)
 {
if ((*phead)->next == NULL)
{
return;
}
pLinkList pos_Node;
pLinkList before_pos;
pLinkList temp=*phead;
int ret = pos - 1;
while (ret--)
{
temp = temp->next;
}
before_pos = temp;
pos_Node = before_pos->next;
before_pos->next = pos_Node->next;
int del_value= pos_Node->value;
DestroyNode(pos_Node);
pos_Node = NULL;
printf("删除指定元素成功  \n");
LinkListPrint(phead);


 }
 void LinkListPrint(pLinkList* phead)
 {
pLinkList temp;
temp = (*phead)->next;
printf("=====顺序输出链表======\n");
if (temp == NULL)
{
printf("链表为空\n");
return;
}
while (temp)
{
printf("[%d|%p]\n", temp->value, temp->next);
temp = temp->next;
}
printf("\n");
 }
 #include<stdio.h>
#include<stdlib.h>
#define HAND   printf("=====%s=====\n",  __FUNCTION__);
typedef  int ElemType;
typedef struct LinkList{
struct LinkList* next;
ElemType value;
}LinkList, *pLinkList;
void pLinkListPushFront(pLinkList* phead, ElemType value);
void LinkListPrint(pLinkList* phead);
int LinkListLength(pLinkList* phead);
 void InitNode(pLinkList* phead);
 void pLinkListPopFront(pLinkList* phead);
 void pLinkListPushBack(pLinkList* phead, ElemType value);
 void pLinkListPopBack(pLinkList* phead);
 pLinkList LinkListFindPos(pLinkList* phead, ElemType value);
 void InsertBeforePos(pLinkList* phead,pLinkList* pos,ElemType value);

 void InsertAfterPos(pLinkList* phead, pLinkList* pos, ElemType value);



 Deletepos(pLinkList* phead, ElemType data);

#include"seqlist.h"
int main()
{
pLinkList linklist;
InitNode(&linklist);
LinkListLength(&linklist);
pLinkListPushFront(&linklist, 1);
pLinkListPushFront(&linklist, 3);
pLinkListPushFront(&linklist, 5);
pLinkListPushFront(&linklist, 7);
pLinkListPopFront(&linklist);
pLinkListPopFront(&linklist);
pLinkListPushBack(&linklist, 2);
pLinkListPushBack(&linklist, 4);
pLinkListPushBack(&linklist, 6);
pLinkListPushBack(&linklist, 8);
pLinkListPopBack(&linklist);
pLinkListPopBack(&linklist);
LinkListFindPos(&linklist, 4);
InsertBeforePos(&linklist, 3,2);
InsertAfterPos(&linklist, 3, 4);
Deletepos(&linklist, 3);
return 0;


}







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值