单链表的插入删除查找

#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;


}







猜你喜欢

转载自blog.csdn.net/qq_40736410/article/details/80203332