实现单链表的建立、清空、插入、删除查找、修改等运算。用数据结构-C语言描述的知识。。。
展开全部
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <windows.h>
typedef struct _DATA_
{
char Name[20];
int Age;
}Data,*pData;
typedef struct _NODE_
{
Data DataTemp;
_NODE_* pNext;
}Node,*pNode;
Data DataTemp ={0};
pNode CreatNode(Data DataTemp);
void Link(pNode pNodeTemp);
void Travel(pNode pNodeTemp);
void Search();
void Insert();
void BubbleSort();
void BackList();
void TravelListBack();
void DelList(int pNum);
pNode gHead = NULL;
pNode gTail = NULL;
int k=0;
int num=0;
int pNum=0;
void main()
{
printf("Input num\n");
scanf("%d",&num);
int i=0;
pNode pNodeTemp=NULL;
for(i=0;i<num;i++)
{
printf("Input Name\n");
scanf("%s",DataTemp.Name);
printf("Input Age\n");
scanf("%d",&DataTemp.Age);
pNodeTemp=CreatNode(DataTemp);
Link(pNodeTemp);
}
Travel(pNodeTemp);
Search();
Travel(pNodeTemp);
printf("Input Place to Insert\n");
scanf("%d",&k);
Insert();
Travel(pNodeTemp);
printf("\n\n");
BubbleSort();
Travel(pNodeTemp);
BackList();
TravelListBack();
printf("Input pNum\n");
scanf("%d",&pNum);
DelList(pNum);
TravelListBack();
printf("\n");
}
pNode CreatNode(Data DataTemp)
{
pNode pNodeTemp=(pNode)malloc(sizeof(Node));
if(pNodeTemp==NULL)
{
return NULL;
}
else
{
pNodeTemp->DataTemp=DataTemp;
pNodeTemp->pNext=NULL;
return pNodeTemp;
}
}
void Link(pNode pNodeTemp)
{
if(gHead==NULL)
{
gHead=gTail=pNodeTemp;
}
else
{
pNodeTemp->pNext = gTail;
gTail = pNodeTemp;
}
}
void Travel(pNode pNodeTemp)
{
pNodeTemp = gTail;
while(pNodeTemp!=NULL)
{
printf("%s %d\n",pNodeTemp->DataTemp.Name,pNodeTemp->DataTemp.Age);
pNodeTemp=pNodeTemp->pNext;
}
}
void Insert()
{
printf("Input Name\n");
scanf("%s",DataTemp.Name);
printf("Input Age\n");
scanf("%d",&DataTemp.Age);
pNode pNodeNew = CreatNode(DataTemp);
pNode pNodepre = gTail;
char a='0';
if(k==1)
{
printf("F B\n");
scanf(" %c",&a);
if(a=='F'||a=='f')
{
pNodeNew->pNext = gTail;
gTail = pNodeNew;
return ;
}
}
while(--k)
{
pNodepre = pNodepre->pNext;
}
pNodeNew->pNext = pNodepre->pNext ;
pNodepre->pNext=pNodeNew;
if(pNodeNew->pNext==NULL)
{
gHead=gHead->pNext;
}
num++;
}
void FreeList()
{
pNode pNodeTemp = gTail;
while (pNodeTemp!=NULL)
{
gTail = gTail->pNext;
free(pNodeTemp);
pNodeTemp = gTail;
}
gTail = gHead = NULL;
}
void BubbleSort()
{
pNode pNodeCurrent = gTail;
pNode pNodepre = NULL;
for(int i=0;i<num;i++)
{
pNodepre = pNodeCurrent->pNext;
for(int j=i+1;j<num;j++)
{
if(pNodeCurrent->DataTemp.Age<pNodepre->DataTemp.Age)
{
DataTemp = pNodepre->DataTemp;
pNodepre->DataTemp=pNodeCurrent->DataTemp ;
pNodeCurrent->DataTemp = DataTemp;
}
pNodepre = pNodepre->pNext;
}
pNodeCurrent = pNodeCurrent->pNext;
}
}
void Search()
{
int k=0;
pNode pNodeTemp=gTail;
char Name[20] = {0};
printf("Input Name\n");
scanf("%s",Name);
while(pNodeTemp!=NULL)
{
if(strcmp(Name,pNodeTemp->DataTemp.Name)==0)
{
printf("%d\n",pNodeTemp->DataTemp.Age);
k=1;
}
pNodeTemp=pNodeTemp->pNext;
}
if(k==0)
{
printf("No Body\n");
}
}
void DelList(int pNum)
{
pNode pNodeCurrent = gHead;
pNode pNodePre = gHead;
int i = 0;
if(pNum==1)
{
gHead = gHead->pNext;
free(pNodeCurrent);
}
else if(pNum == num)
{
for(i=0;i<pNum-2;i++)
{
pNodeCurrent = pNodeCurrent->pNext;
}
free(gTail);
gTail->pNext = NULL;
gTail = pNodeCurrent;
}
else if(pNum>1&&pNum<num)
{
for(i=0;i<pNum-2;i++)
{
pNodePre = pNodePre->pNext;
}
pNodeCurrent = pNodePre->pNext->pNext;
free(pNodePre->pNext);
pNodePre->pNext = pNodeCurrent;
}
else
{
printf("No exist\n");
}
}
void BackList()
{
pNode pNodeCurrent =gHead ;
pNode pNodeNext = NULL;
while(pNodeCurrent != gTail)
{
pNodeNext = gTail;
while(pNodeNext->pNext!= pNodeCurrent)
{
pNodeNext= pNodeNext->pNext;
}
pNodeCurrent->pNext = pNodeNext;
pNodeCurrent = pNodeCurrent->pNext;
}
pNodeCurrent->pNext = NULL;
}
void TravelListBack()
{
pNode pNodeTemp = gHead;
while(pNodeTemp != NULL)
{
printf("%s %d \n",pNodeTemp->DataTemp.Name,pNodeTemp->DataTemp.Age);
pNodeTemp = pNodeTemp->pNext;
}
}
void DelByRule(Data DataTemp)
{
pNode pNodeTravel = gHead;
pNode pNodePre = NULL;
while(pNodeTravel!=NULL)
{
if(strcmp(pNodeTravel->DataTemp.Name,DataTemp.Name)==0)
{
if(pNodeTravel == gHead)
{
gHead = gHead->pNext;
free(pNodeTravel);
pNodeTravel = gHead;
}
else
{
pNodePre = gHead;
while(pNodePre->pNext != pNodeTravel)
{
pNodePre = pNodePre->pNext;
}
pNodePre->pNext = pNodeTravel->pNext;
free(pNodeTravel);
pNodeTravel = pNodePre->pNext;
if(pNodeTravel!=NULL)
{
gTail = pNodePre;
}
}
}
/* else
{
pNodeTravel = pNodeTravel->DataTemp;
}*/
}
}
这个代码基本包括了 有什么不懂hi我
#include <string.h>
#include <malloc.h>
#include <windows.h>
typedef struct _DATA_
{
char Name[20];
int Age;
}Data,*pData;
typedef struct _NODE_
{
Data DataTemp;
_NODE_* pNext;
}Node,*pNode;
Data DataTemp ={0};
pNode CreatNode(Data DataTemp);
void Link(pNode pNodeTemp);
void Travel(pNode pNodeTemp);
void Search();
void Insert();
void BubbleSort();
void BackList();
void TravelListBack();
void DelList(int pNum);
pNode gHead = NULL;
pNode gTail = NULL;
int k=0;
int num=0;
int pNum=0;
void main()
{
printf("Input num\n");
scanf("%d",&num);
int i=0;
pNode pNodeTemp=NULL;
for(i=0;i<num;i++)
{
printf("Input Name\n");
scanf("%s",DataTemp.Name);
printf("Input Age\n");
scanf("%d",&DataTemp.Age);
pNodeTemp=CreatNode(DataTemp);
Link(pNodeTemp);
}
Travel(pNodeTemp);
Search();
Travel(pNodeTemp);
printf("Input Place to Insert\n");
scanf("%d",&k);
Insert();
Travel(pNodeTemp);
printf("\n\n");
BubbleSort();
Travel(pNodeTemp);
BackList();
TravelListBack();
printf("Input pNum\n");
scanf("%d",&pNum);
DelList(pNum);
TravelListBack();
printf("\n");
}
pNode CreatNode(Data DataTemp)
{
pNode pNodeTemp=(pNode)malloc(sizeof(Node));
if(pNodeTemp==NULL)
{
return NULL;
}
else
{
pNodeTemp->DataTemp=DataTemp;
pNodeTemp->pNext=NULL;
return pNodeTemp;
}
}
void Link(pNode pNodeTemp)
{
if(gHead==NULL)
{
gHead=gTail=pNodeTemp;
}
else
{
pNodeTemp->pNext = gTail;
gTail = pNodeTemp;
}
}
void Travel(pNode pNodeTemp)
{
pNodeTemp = gTail;
while(pNodeTemp!=NULL)
{
printf("%s %d\n",pNodeTemp->DataTemp.Name,pNodeTemp->DataTemp.Age);
pNodeTemp=pNodeTemp->pNext;
}
}
void Insert()
{
printf("Input Name\n");
scanf("%s",DataTemp.Name);
printf("Input Age\n");
scanf("%d",&DataTemp.Age);
pNode pNodeNew = CreatNode(DataTemp);
pNode pNodepre = gTail;
char a='0';
if(k==1)
{
printf("F B\n");
scanf(" %c",&a);
if(a=='F'||a=='f')
{
pNodeNew->pNext = gTail;
gTail = pNodeNew;
return ;
}
}
while(--k)
{
pNodepre = pNodepre->pNext;
}
pNodeNew->pNext = pNodepre->pNext ;
pNodepre->pNext=pNodeNew;
if(pNodeNew->pNext==NULL)
{
gHead=gHead->pNext;
}
num++;
}
void FreeList()
{
pNode pNodeTemp = gTail;
while (pNodeTemp!=NULL)
{
gTail = gTail->pNext;
free(pNodeTemp);
pNodeTemp = gTail;
}
gTail = gHead = NULL;
}
void BubbleSort()
{
pNode pNodeCurrent = gTail;
pNode pNodepre = NULL;
for(int i=0;i<num;i++)
{
pNodepre = pNodeCurrent->pNext;
for(int j=i+1;j<num;j++)
{
if(pNodeCurrent->DataTemp.Age<pNodepre->DataTemp.Age)
{
DataTemp = pNodepre->DataTemp;
pNodepre->DataTemp=pNodeCurrent->DataTemp ;
pNodeCurrent->DataTemp = DataTemp;
}
pNodepre = pNodepre->pNext;
}
pNodeCurrent = pNodeCurrent->pNext;
}
}
void Search()
{
int k=0;
pNode pNodeTemp=gTail;
char Name[20] = {0};
printf("Input Name\n");
scanf("%s",Name);
while(pNodeTemp!=NULL)
{
if(strcmp(Name,pNodeTemp->DataTemp.Name)==0)
{
printf("%d\n",pNodeTemp->DataTemp.Age);
k=1;
}
pNodeTemp=pNodeTemp->pNext;
}
if(k==0)
{
printf("No Body\n");
}
}
void DelList(int pNum)
{
pNode pNodeCurrent = gHead;
pNode pNodePre = gHead;
int i = 0;
if(pNum==1)
{
gHead = gHead->pNext;
free(pNodeCurrent);
}
else if(pNum == num)
{
for(i=0;i<pNum-2;i++)
{
pNodeCurrent = pNodeCurrent->pNext;
}
free(gTail);
gTail->pNext = NULL;
gTail = pNodeCurrent;
}
else if(pNum>1&&pNum<num)
{
for(i=0;i<pNum-2;i++)
{
pNodePre = pNodePre->pNext;
}
pNodeCurrent = pNodePre->pNext->pNext;
free(pNodePre->pNext);
pNodePre->pNext = pNodeCurrent;
}
else
{
printf("No exist\n");
}
}
void BackList()
{
pNode pNodeCurrent =gHead ;
pNode pNodeNext = NULL;
while(pNodeCurrent != gTail)
{
pNodeNext = gTail;
while(pNodeNext->pNext!= pNodeCurrent)
{
pNodeNext= pNodeNext->pNext;
}
pNodeCurrent->pNext = pNodeNext;
pNodeCurrent = pNodeCurrent->pNext;
}
pNodeCurrent->pNext = NULL;
}
void TravelListBack()
{
pNode pNodeTemp = gHead;
while(pNodeTemp != NULL)
{
printf("%s %d \n",pNodeTemp->DataTemp.Name,pNodeTemp->DataTemp.Age);
pNodeTemp = pNodeTemp->pNext;
}
}
void DelByRule(Data DataTemp)
{
pNode pNodeTravel = gHead;
pNode pNodePre = NULL;
while(pNodeTravel!=NULL)
{
if(strcmp(pNodeTravel->DataTemp.Name,DataTemp.Name)==0)
{
if(pNodeTravel == gHead)
{
gHead = gHead->pNext;
free(pNodeTravel);
pNodeTravel = gHead;
}
else
{
pNodePre = gHead;
while(pNodePre->pNext != pNodeTravel)
{
pNodePre = pNodePre->pNext;
}
pNodePre->pNext = pNodeTravel->pNext;
free(pNodeTravel);
pNodeTravel = pNodePre->pNext;
if(pNodeTravel!=NULL)
{
gTail = pNodePre;
}
}
}
/* else
{
pNodeTravel = pNodeTravel->DataTemp;
}*/
}
}
这个代码基本包括了 有什么不懂hi我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询