#include <stdio.h>
#include <stdlib.h>
#include <memory.h> //在vc++ 里可以直接用#include <memory>
//在linux下#include <stdlib.h> #include <memory.h>两个一起用
typedef struct a
{
int i;
int j;
char c;
struct a* pNext;
}A;
A* getMemory(A** pA) //这里一定要传指针的地址,否则无法返回申请的内存
{
*pA = (A*)malloc(sizeof(A));
return *pA;
}
A* insertZ(A* pA,int i,int j,char c)
{
if (pA != NULL)
{
pA->i = i;
pA->j = j;
pA->c = c;
}
return pA;
}
A* selectZ(A* pA, int i) //下标i从0开始
{
for(int cout = 0;cout <=i,pA!= NULL;pA = pA->pNext,++cout)
{
if (cout == i-1)
{
printf("selectZ: %d %d %c\n\n",pA->i,pA->j,pA->c);
return pA; //找到下标为i的上一个节点地址
}
}
}
bool deleteZ(A* pA,int i) //下标i从0开始
{
pA = selectZ(pA,i);
pA->pNext = pA->pNext->pNext;
return true;
/*for(int cout = 0;cout <=i,pA!= NULL;pA = pA->pNext,++cout)
{
if (cout == i-1)
{
printf("%d %d %c\n\n",pA->i,pA->j,pA->c);
pA->pNext = pA->pNext->pNext;
return true;
}
}*/
}
void modyNode(A* node,int i)
{
node = selectZ(node,i); //查到的是下标为i-1的节点
node->pNext->i = 9;
node->pNext->j = 10;
node->pNext->c = 'Z';
/*node->i = 9;
node->j = 10;
node->c = 'Z';*/
}
void showNode(A* node)
{
while(node != NULL)
{
printf("showNode: %d %d %c\n\n",node->i,node->j,node->c);
node = node->pNext;
}
}
int main()
{
//简单链表插入
A* node1;
node1= getMemory(&node1); //创建
node1= insertZ(node1,1,2,'a'); //插入
A* node2;
node2= getMemory(&node1->pNext);
node2= insertZ(node2,3,4,'b');
A* node3;
node3= getMemory(&node2->pNext);
node3= insertZ(node3,5,6,'j');
node3->pNext = NULL;
A* node4;
node4= getMemory(&node4);
node4= insertZ(node4,7,8,'k');
node4->pNext = NULL;
node3->pNext = node4; //将node4拼接在node3之后
//deleteZ(node1,1); //删除下标为1的节点
showNode(tmp1); //遍历节点
modyNode(tmp1,1);
showNode(tmp1);
return 0;
}
只有next的单向链表简单创建、增删改查以及不需中介拼接两个链表
最新推荐文章于 2022-11-14 21:37:50 发布