</pre><pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct Node
{
int data;
Node* pnext;
}Node,*List;//???List
static Node* BuyNode(int data)//???static
{
Node *p=(Node*)malloc(sizeof(Node));
assert(p!=NULL);
p->data=data;
p->pnext=NULL;
return p;
}
void ListInit(Node **phead)
{
*phead=BuyNode(0);
}
void ListInsertHead(Node* phead,int data)
{
if(phead==NULL)
{
ListInit(&phead);
}
Node *ptmp=BuyNode(data);
ptmp->pnext=phead->pnext;
phead->pnext=ptmp;
printf("头插成功--插入%d\n",data);
}
void ListInsertTail(Node *phead,int data)
{
if(phead==NULL)
{
ListInit(&phead);
}
Node *ptmp=BuyNode(data);
Node *pcur=phead;
while(pcur->pnext!=NULL)
{
pcur=pcur->pnext;
}
pcur->pnext=ptmp;
printf("尾插成功--插入%d\n",data);
}
Node* ListFindData(Node *phead,int data)
{
Node* pcur=phead;
while(pcur->pnext!=NULL)
{
pcur=pcur->pnext;
if(pcur->data==data)
{
printf("成功找到%d\n",data);
return pcur;
}
}
return NULL;
}
bool ListDeleteData(Node *phead,int data)
{
Node *pcur=phead;
Node *ptmp=NULL;
int cout=0;
while(pcur!=NULL)
{
ptmp=ListFindData(pcur,data);
if(ptmp==NULL&&cout==0)
{
printf("没有查找到%d!!!\n",data);
return false;
}
else if(ptmp==NULL&&cout!=0)
{
return true;
}
else if(ptmp!=NULL)
{
while(pcur->pnext!=ptmp)
{
pcur=pcur->pnext;
}
pcur->pnext=ptmp->pnext;
cout++;
printf("成功删除%d\n",ptmp->data);
free(ptmp);
ptmp=NULL;
}
}
}
int ListSize(Node* phead)
{
Node *pcur=phead;
int cout=0;
while(pcur->pnext!=NULL)
{
pcur=pcur->pnext;
cout++;
}
return cout;
}
Node* ListFindPos(Node* phead,int pos)
{
if(pos<0||pos>ListSize(phead))
{
printf("输入参数错误!!!\n");
return NULL;
}
Node *pcur=phead;
while(pos--)
{
pcur=pcur->pnext;
}
return pcur;
}
Node *ListDeletePos(Node *phead,int pos)
{
if(pos<0||pos>=ListSize(phead))
{
printf("输入参数不合法!!!\n");
return NULL;
}
Node *pcur=phead;
Node *ptmp=ListFindPos(pcur,pos);
while(pcur->pnext!=ptmp)
{
pcur=pcur->pnext;
}
pcur->pnext=ptmp->pnext;
free(ptmp);
}
void ListReverse(Node* phead)
{
Node *pcur=phead->pnext;
Node *ptmp=NULL;
if(pcur==NULL) return ;
while(pcur->pnext!=NULL)
{
ptmp=pcur->pnext;
if(ptmp!=NULL)
{
pcur->pnext=ptmp->pnext;
ptmp->pnext=phead->pnext;
phead->pnext=ptmp;
}
}
}
Node* ListMerge(Node* phead1,Node* phead2,Node *pmerge)
{
Node* pcur1=phead1->pnext;
Node* pcur2=phead2->pnext;
Node *pcur=pmerge;
if(pcur1==NULL){return pcur2;}
if(pcur2==NULL){return pcur1;}
while(pcur1!=NULL&&pcur2!=NULL)
{
if(pcur1->data<=pcur2->data)
{
pcur->pnext=BuyNode(pcur1->data);
pcur1=pcur1->pnext;
pcur=pcur->pnext;
}
else
{
pcur->pnext=BuyNode(pcur2->data);
pcur2=pcur2->pnext;
pcur=pcur->pnext;
}
}
if(pcur1!=NULL)
{
pcur->pnext=pcur1;
}
else
{
pcur->pnext=pcur2;
}
return pmerge;
}
Node* ListReverseToK(Node *phead,int k)
{
if(k<=0||k>ListSize(phead))
{
printf("输入参数不合法!!!\n");
return NULL;
}
Node* ptmp=phead;
Node* pcur=phead;
while(k--)
{
ptmp=ptmp->pnext;
}
while(ptmp!=NULL)
{
pcur=pcur->pnext;
ptmp=ptmp->pnext;
}
printf("%d\n",pcur->data);
return pcur;
}
Node* ListSort(Node* phead)
{
return NULL;
}
bool IsListCircle(Node *phead)
{
return true;
}
bool IsListCommon(Node *phead1,Node *phead2)
{
return false;
}
void ListClear(Node *phead)
{
Node* pcur=phead->pnext;
Node* ptmp=phead;
while(pcur!=NULL)
{
ptmp=pcur;
pcur=pcur->pnext;
free(ptmp);
ptmp=NULL;
}
phead->pnext=NULL;
}
void ListShow(Node* phead)
{
if(phead->pnext==NULL)
{
printf("空链表!!!\n");
return;
}
Node *pcur=phead;
printf("链表为:");
while(pcur->pnext!=NULL)
{
pcur=pcur->pnext;
printf("%d ",pcur->data);
}
printf("\n");
printf("链表总结点个数为:%d\n",ListSize(phead));
}
void main()
{
Node* phead1=NULL;
Node *phead2=NULL;
Node *pmerge=NULL;
ListInit(&phead1);
ListInit(&phead2);
ListInit(&pmerge);
ListInsertHead(phead1,10);
ListInsertHead(phead1,8);
ListInsertHead(phead1,6);
ListInsertHead(phead1,4);
ListInsertHead(phead1,2);
ListShow(phead1);
ListInsertTail(phead2,4);
ListInsertTail(phead2,10);
ListInsertTail(phead2,12);
ListInsertTail(phead2,14);
ListInsertTail(phead2,14);
ListInsertTail(phead2,14);
ListInsertTail(phead2,4);
ListShow(phead2);
// ListDeleteData(phead,11);
// ListShow(phead);
// ListDeleteData(phead,99);
// ListShow(phead);
// ListDeletePos(phead,100);
// ListShow(phead);
// ListDeleteData(phead,5);
// ListShow(phead);
// ListReverse(phead);
// ListShow(phead);
// ListClear(phead);
// ListShow(phead);
ListMerge(phead1,phead2,pmerge);
ListShow(pmerge);
ListReverseToK(phead1,1);
// ListShow(phead1);
}
常见链表操作
最新推荐文章于 2025-05-05 00:34:28 发布
1万+

被折叠的 条评论
为什么被折叠?



