单链表的插入与删除是不。我编好了一个,在2005下运行成功
这个是插入
#include<stdio.h>
#include<stdlib.h>
struct List
{
int Number;
struct List *Next;
};
typedef struct List Node;
typedef Node* Link;
int Data[10]={9,8,7,6,5,4,3,2,1};
Link Create_List(Link Head)
{
Link Pointer;
Link New;
Head=(Link)malloc(sizeof(Node));
if(Head==NULL)
{
printf("Memory allocate Failure!!!");
}
else
{
Head->Number = Data[0];
Head->Next = NULL;
Pointer = Head;
for(int i=1;i<10;i++)
{
New = (Link)malloc(sizeof(Node));
New->Number = Data[i];
New->Next = NULL;
Pointer->Next = New;
Pointer = New;
}
}
return Head;
}
void Free_List(Link Head)
{
Link Pointer;
while(Head!=NULL)
{
Pointer = Head;
Head = Head->Next;
free(Pointer);
}
}
void Print_List(Link Head)
{
Link Pointer;
Pointer = Head;
while(Pointer!=NULL)
{
printf("Number is [%d]\n",Pointer->Number);
Pointer = Pointer->Next;
}
}
Link Insert_List(Link Head,Link New,int Insert_Number)
{
Link Pointer;
Pointer = Head;
while(1)
{
if(Pointer->Number == Insert_Number)
{
New->Next = Pointer->Next;
Pointer->Next = New;
break;
}
Pointer = Pointer->Next;
if(Pointer==NULL)
{
New->Next = Head;
Head = New;
break;
}
}
return Head;
}
Link Search_List(Link Head,int Search_Number)
{
Link Pointer;
Pointer = Head;
while(1)
{
if(Pointer->Number == Search_Number )
{
printf("Search Number is [%d]\n",Pointer->Number);
break;
}
Pointer = Pointer->Next;
if(Pointer->Next==NULL)
{
printf("Not Found!!!");
break;
}
}
return Head;
}
int main()
{
Link Head=NULL;
Link New;
int Insert_Number;
Head = Create_List(Head);
if(Head!=NULL)
{
Print_List(Head);
// Free_List(Head);
}
printf("Please input Insert Number : ");
scanf("%d",&Insert_Number);
New = (Link)malloc(sizeof(Node));
scanf("%d",&New->Number);
// Search_List(Head,Search);
Head=Insert_List(Head,New,Insert_Number);
Print_List(Head);
return 0;
}
下来是删除
#include<stdlib.h>
#include<stdio.h>
struct List
{
int Number;
struct List *Next;
};
typedef struct List Node;
typedef Node* Link;
int Data[10] = {9,8,7,6,5,4,3,2,1,0};
void Free_List(Link Head)
{
Link Pointer;
while(Head!=NULL)
{
Pointer = Head;
Head = Head->Next;
free(Pointer);
}
}
Link Print_List(Link Head)
{
Link Pointer;
Pointer = Head;
if((Head->Number >-32762)&&(Head ->Number <32768) )
{
while(Pointer!=NULL)
{
printf("Number is [%d]\n",Pointer->Number);
Pointer= Pointer->Next;
}
}
else
return NULL;
}
Link Delete_List(Link Head,int Delete_Number)
{
Link Pointer;
Link Back;
Pointer = Head;
while(1)
{
if(Head->Number == Delete_Number )
{
Head = Pointer->Next;
free(Pointer);
break;
}
Back = Pointer;
Pointer = Pointer->Next;
if(Pointer->Number == Delete_Number)
{
Back->Next = Pointer->Next;
break;
}
if(Pointer == NULL)
{
printf("没有找到该节点\n");
break;
}
}
return Head;
}
Link Create_List(Link Head)
{
Link Pointer;
Link New;
Head = (Link)malloc(sizeof(Node));
if(Head == NULL)
{
printf("Memory allcoate Failure!!!\n");
}
else
{
Head->Number = Data[0];
Head->Next = NULL;
Pointer = Head;
for(int i=1 ;i<10; i++)
{
New = (Link)malloc(sizeof(Node));
New->Number= Data[i];
New->Next = NULL;
Pointer->Next = New;
Pointer= New;
}
}
return Head;
}
int main()
{
int Del_Number;
Link Head;
Head=Create_List(Head);
if(Head!=NULL)
{
Print_List(Head);
// Free_List(Head);
}
while(1)
{
printf("请输入您所要删除的节点:\n");
scanf("%d",&Del_Number);
if(Del_Number>=32767)
break;
Head = Delete_List(Head,Del_Number);
if(Head!=NULL)
Head=Print_List(Head);
else
break;
}
printf("链表已经空\n");
return 0;
}