#include<stdio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node *pNext;
};
//增:尾添加
void AddToEnd(struct Node *pHead, int *pNode_Count, int Data);
//删:释放
void Free(struct Node *pHead, int *pNode_Count);
//查:遍历(查看整个链表的数据)
void Look(struct Node *pHead);
//增:在指定位置插入节点
void Insert(struct Node *pHead, int *pNode_Count ,int i, int Data);
//删:删除指定位置的节点
int Delete(struct Node *pHead, int *pNode_Count ,int i);
//改:修改指定位置的节点的数据
int Change(struct Node *pHead, int *pNode_Count, int i, int Data);
//查:查看指定位置的节点的数据
void Get(struct Node *pHead, int *pNode_Count, int i);
//交互:
void Interaction_1(struct Node *pHead, int *pNode_Count);
void Interaction_2(struct Node *pHead, int *pNode_Count, int *O);
int main(void)
{
//创建一个空头
struct Node Head = {0, NULL};
//创建一个计数器
int Node_Count = 0;
//struct Node *pHead = (struct Node *)malloc(sizeof(struct Node));
//pHead->Data = 0;
//pHead->pNext = NULL;
//free(pHead);
Interaction_1(&Head,&Node_Count);
return 0;
}
void AddToEnd(struct Node *pHead, int *pNode_Count, int Data)
{
//检测参数的合法性(&Head!=NULL、Node_Count>0、&Node_Count!=NULL)
if(pHead == NULL || pNode_Count == NULL || *pNode_Count < 0)
{
printf("Error of AddToEnd !\n");
return 0;
}
//申请节点的空间
struct Node *pTemp = (struct Node *)malloc(sizeof(struct Node));
//检测指针变量的合法性(指针变量 != NULL)
if(pTemp == NULL)
{
printf("申请节点失败!\n");
return 0;
}
//给节点装入数据
pTemp->Data = Data;
pTemp->pNext = NULL;
//把节点连在链表上(先找到链表的尾节点(pNext == NULL),后连上去)
struct Node *End = pHead;
while(End->pNext != NULL)
{
End = End->pNext;
}
End->pNext = pTemp;
//计数
(*pNode_Count)++;//优先级:++ > *
}
void Free(struct Node *pHead, int *pNode_Count)
{
if(pHead == NULL || pNode_Count == NULL || *pNode_Count <= 0)
{
printf("\nError of FreeList ! \n");
return 0;
}
struct Node *pTemp = pHead->pNext;
while(pTemp != NULL)
{
struct Node *ptemp = pTemp;
free(ptemp);
pTemp = pTemp->pNext;
}
pHead->pNext = NULL;
*pNode_Count = 0;
}
void Look(struct Node *pHead)
{
if(pHead == NULL)
{
printf("\nError of Look !\n");
return 0;
}
struct Node *pTemp = pHead->pNext;
while(pTemp != NULL)
{
printf("%d ", pTemp->Data);
pTemp = pTemp->pNext;
}
}
void Insert(struct Node * pHead, int *pNode_Count, int i, int Data)
{
//检测参数的合法性
if(pHead == NULL || i <= 0 || i > *pNode_Count)
{
printf("\nError of Insert ! \n");
return 0;
}
//申请空间
struct Node *pTemp = (struct Node *)malloc(sizeof(struct Node));
//申请到空间了吗?
if(pTemp == NULL)
{
printf("\n");
return 0;
}
//寻找指定节点的前一个节点
struct Node *pT = pHead;
for(int k = 1 ; k < i ; k++)
{
pT = pT->pNext;
}
pTemp->pNext = pT->pNext;
pT->pNext = pTemp;
pTemp->Data = Data;
(*pNode_Count)++;
}
int Delete(struct Node *pHead, int *pNode_Count, int i)
{
//检测参数的合法性
if(pHead == NULL || *pNode_Count <=0 || i <= 0 || i > *pNode_Count)
{
printf("\nError of Delete !\n");
return 0;
}
//寻找指定节点的前一个节点
struct Node *pTemp = pHead;
for(int k = 1 ; k < i ; k++)
{
pTemp = pTemp->pNext;
}
struct Node *pT = pTemp->pNext;
//获取要删除的节点里的数据
int a = pT->Data;
//短路处理
pTemp->pNext = (pTemp->pNext)->pNext;
//释放空间
free(pT);
//计数器-1
(*pNode_Count)--;
return a;
}
int Change(struct Node *pHead, int *pNode_Count, int i, int Data)
{
//检测参数的合法性
if(pHead == NULL || pNode_Count == NULL || *pNode_Count <= 0 || i <= 0 || i > *pNode_Count)
{
printf("\nError of Change !\n");
}
//寻找指定位置的节点
struct Node *pTemp = pHead;
for(int k = 1 ; k <= i ; k++)
{
pTemp = pTemp->pNext;
}
//获取原来的数据
int a = pTemp->Data;
//修改数据
pTemp->Data = Data;
return a;
}
void Get(struct Node *pHead, int *pNode_Count, int i)
{
//判断参数的合法性
if(pHead == NULL || pNode_Count == NULL || *pNode_Count <= 0 || i <= 0 || i > *pNode_Count)
{
printf("\nError of Get ! \n");
return 0;
}
//寻找指定位置的节点
struct Node *pTemp = pHead;
for(int k = 1 ; k <= i ;k++)
{
pTemp = pTemp->pNext;
}
printf("\n第%d个位置的数据为%d!",i,pTemp->Data);
}
void Interaction_1(struct Node *pHead, int *pNode_Count)
{
printf("你想存储数据吗?\n");
printf("[1]想 [2]不想\n");
int a;
scanf("%d",&a);
if(a != 1)
{
printf("\n尊重你的选择!\n");
}
else
{
printf("\n你想存储几个数据?\n");
int b;
scanf("%d",&b);
printf("\n请输入你想存储的数据!\n");
for(int i = 1 ; i <= b ; i++)
{
int c;
scanf("%d",&c);
AddToEnd(pHead,pNode_Count,c);
}
printf("\n你输入的数据如下!\n");
Look(pHead);
printf("\n\n你可以对这些数据进行如下操作:\n");
printf("[1]增:在结尾添加数据!\n");
printf("[2]增:在指定位置添加数据!\n");
printf("[3]删:删除指定位置的数据!\n");
printf("[4]改:修改指定位置的数据!\n");
printf("[5]查:查看指定位置的数据!\n");
int o = 1;
while(o == 1)
{
Interaction_2(pHead, pNode_Count, &o);
}
}
}
void Interaction_2(struct Node *pHead, int *pNode_Count, int *O)
{
printf("\n\n你想对已有数据进行操作吗?\n");
printf("[1]想 [2]不想\n");
int d;
scanf("%d",&d);
if(d != 1)
{
printf("\n尊重你的选择!\n感谢你的使用!\n");
*O = 0;
}
else
{
printf("\n请选择你想进行的操作!\n");
int e;
scanf("%d",&e);
switch(e)
{
case 1 :
printf("\n请输入你想添加的数据!\n");
int f;
scanf("%d",&f);
AddToEnd(pHead,pNode_Count,f);
printf("操作后的数据如下:\n");
Look(pHead);
break;
case 2 :
printf("\n你想在第几个位置添加数据?\n");
int g;
scanf("%d",&g);
if( g <= 0 || g > *pNode_Count)
{
printf("\nError of Insert !\n");
printf("\n操作后的数据如下:\n");
Look(pHead);
}
else
{
printf("\n请输入你想添加的数据!\n");
int h;
scanf("%d",&h);
Insert(pHead,pNode_Count,g,h);
printf("操作后的数据如下:\n");
Look(pHead);
}
break;
case 3 :
printf("\n你想删除第几个位置的数据?\n");
int i;
scanf("%d",&i);
if( i <= 0 || i > *pNode_Count)
{
printf("\nError of Delete !\n");
printf("\n操作后的数据如下:\n");
Look(pHead);
}
else
{
int j = Delete(pHead,pNode_Count,i);
printf("\n你删除的数据为%d!\n\n",j);
printf("操作后的数据如下:\n");
Look(pHead);
}
break;
case 4 :
printf("\n你想修改第几个位置的数据?\n");
int k;
scanf("%d",&k);
if( k <= 0 || k > *pNode_Count)
{
printf("\nError of Change ! \n");
printf("\n操作后的数据如下:\n");
Look(pHead);
}
else
{
printf("\n你想修改成什么数据?\n");
int l;
scanf("%d",&l);
int m = Change(pHead,pNode_Count,k,l);
printf("\n你修改的数据为%d!\n",m);
printf("\n操作后的数据如下:\n");
Look(pHead);
}
break;
case 5 :
printf("\n你想查看哪个位置的数据?\n");
int n;
scanf("%d",&n);
if( n <= 0 || n > *pNode_Count)
{
printf("\nError of Get ! \n");
printf("\n操作后的数据如下:\n");
Look(pHead);
}
else
{
Get(pHead,pNode_Count,n);
}
break;
default :
return 0;
break;
}
}
}
有空头单向链表
最新推荐文章于 2025-04-18 09:42:15 发布