编写一个程序algo2-2.cpp,实现单链表的各种基本运算~,并在此基础上设计一个主程序完成如下功能: 1.初

2024-11-04 14:26:23
推荐回答(1个)
回答1:

完整的帮你完成了 加点分吧

#include

typedef struct _NODE_
{
char a;

_NODE_ * next;

}Node,*pNode;

pNode Head = NULL;

pNode Tail = NULL;

void CreateLinkNode(char a);

void main()
{
char temp = 'a';

for(int i=0;i<5;i++)
{
CreateLinkNode(temp+i);
}

int Count=0;

pNode pNodeTemp = Head;

while(pNodeTemp!=NULL)
{
Count++;

printf("%c ",pNodeTemp->a);

pNodeTemp = pNodeTemp->next;
}
printf("链表长度为%d\n",Count);

int m = 2;

pNodeTemp = Head;

while(m)
{
pNodeTemp = pNodeTemp->next;

m--;
}

printf("第三个元素为 %c \n",pNodeTemp->a);

pNodeTemp = Head;

int Pos=0;

while(pNodeTemp)
{
Pos++;

if(pNodeTemp->a == 'a')
{
printf("a的位置为%d\n",Pos);

break;
}

pNodeTemp = pNodeTemp->next;
}

pNodeTemp = Head;

for(int k=0;k<2;k++)
{
pNodeTemp = pNodeTemp->next;
}

pNode pNodeNew = new Node;

if(pNodeNew==NULL)
{
return ;
}

pNodeNew->a = 'f';

pNodeNew->next = NULL;

pNodeNew->next = pNodeTemp->next;

pNodeTemp->next = pNodeNew;

Count++;

pNodeTemp = Head;

while(pNodeTemp!=NULL)
{

printf("%c ",pNodeTemp->a);

pNodeTemp = pNodeTemp->next;
}

pNodeTemp = Head->next;

pNodeTemp->next = pNodeTemp->next->next;

pNodeTemp = Head;

printf("\n");

while(pNodeTemp!=NULL)
{

printf("%c ",pNodeTemp->a);

pNodeTemp = pNodeTemp->next;
}

while(Head!=Tail)
{
pNodeTemp = Head;

Head = Head->next;

delete pNodeTemp;
}

delete Head;

}

void CreateLinkNode(char a)
{
pNode pNodeTemp = new Node;

if(pNodeTemp==NULL)
{
return ;
}

pNodeTemp->a = a;

pNodeTemp->next = NULL;

if(Head==NULL)
{

Head=Tail = pNodeTemp;

}
else
{

Tail->next = pNodeTemp;

Tail = pNodeTemp;

}

}