#include<stdio.h>#include<stdlib.h>//此鏈表中的數據均為int型typedef struct Link_list{ int date; struct Link_list *next;}Link; int main(){ Link *Link_creat(); vo ...
#include<stdio.h>
#include<stdlib.h>
//此鏈表中的數據均為int型
typedef struct Link_list{
int date;
struct Link_list *next;
}Link;
int main()
{
Link *Link_creat();
void Link_print(Link *head);
void ClearList(Link *head);
bool ListEmpty(Link *head);
int ListLength(Link *head);
int GetElem(Link *head,int n);
bool ListDelete(Link *head,int locate);
bool ListInsert(Link *head,int number,int locate);
Link *Line;
Line=Link_creat();
//if(ListDelete(Line,2))Link_print(Line);
//printf("%d ",GetElem(Line,6));
return 0;
}
Link *Link_creat()
{
Link *head=NULL;
Link *tail=head;
int number;
do{
scanf("%d",&number);
if(number==-1){
break;
}
Link *p=(Link *)malloc(sizeof(Link));
p->date=number;
p->next=NULL;
if(!tail){
head=p;
tail=head;
}
else {
while(tail->next)
{
tail=tail->next;
}
tail->next=p;
}
}while(number!=-1);
return head;
}
void Link_print(Link *head)
{
Link *p=head;
while(p)
{
printf("%d ",p->date);
p=p->next;
}
}
void ClearList(Link *head)
{
Link *p=head;
Link *pt=head->next;
while(pt)
{
free(p);
p=pt;
pt=pt->next;
}
free(p);
}
bool ListEmpty(Link *head)
{
Link *p=head;
if(!p)return false;
else return true;
}
int ListLength(Link *head)
{
Link *p=head;
int ans=0;
while(p)
{
ans++;
p=p->next;
}
return ans;
}
int GetElem(Link *head,int n)
{
Link *p=head;
if(n>ListLength(p))return -1;
for(int i=1;i<n;i++)
{
p=p->next;
}
return p->date;
}
bool ListInsert(Link *head,int number,int locate)
{
if(locate>ListLength(head))return false;
Link *p=head;
Link *temp=(Link *)malloc(sizeof(Link));
temp->date=number;
for(int i=1;i<locate-1;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
return true;
}
bool ListDelete(Link *head,int locate)
{
if(locate>ListLength(head))return false;
Link *p=head;
for(int i=1;i<locate-1;i++)
{
p=p->next;
}
p->next=(p->next)->next;
return true;
}