/*
*link_list.c --测试单链表:创建、显示、销毁、增加、删除、修改、查找
*CentOS 7 / gcc -std=c99
*/
#include <stdio.h>
#include <stdlib.h>
struct Link{
int elem;
struct Link *next;
};
struct Link *Init_Link(int *length);
void Display_Link(struct Link *list,int *length);
void Destory_Link(struct Link *list,int *length);
struct Link *Insert_Elem(struct Link *list,int pos,int elem,int *length);
struct Link *Delete_Elem(struct Link *list,int pos,int *length);
int Alter_Elem(struct Link *list,int pos,int elem);
void Seek_Elem(struct Link *list,int elem);
int main(){
int key=0,elem=0,pos=0,size=0;
int *length=&size;
struct Link *list=NULL;
while(key!=88){
printf("请输入功能选项:\n1--创建链表\n2--显示链表所有元素\n3--查找链表元素\n4--插入链表元素\n5--删除链表元素\n6--修改链表元素\n7--销毁链表\n");
scanf("%d",&key);
if(key==1){
printf("请输入要设置的链表长度:\n");
scanf("%d",length);
list=Init_Link(length);
}
if(key==2){
Display_Link(list,length);
}
if(key==3){
printf("请输入要查找的元素!\n");
scanf("%d",&elem);
Seek_Elem(list,elem);
}
if(key==4){
printf("请输入要插入的元素和位置:\n");
scanf("%d %d",&elem,&pos);
list=Insert_Elem(list,pos,elem,length);
}
if(key==5){
printf("请输入要删除的元素位置:\n");
scanf("%d",&pos);
list=Delete_Elem(list,pos,length);
}
if(key==6){
printf("请输入要修改的元素及其所在位置:\n");
scanf("%d %d",&elem,&pos);
Alter_Elem(list,pos,elem);
}
if(key==7){
Destory_Link(list,length);
}
}
return 0;
}
struct Link *Init_Link(int *length){
struct Link *head=(struct Link*)malloc(sizeof(struct Link));
struct Link *cursor=head;
cursor->elem=0;
cursor->next=NULL;
struct Link *temp=NULL;
if(*length>1){
for(int i=1;i<*length;++i){
temp=(struct Link*)malloc(sizeof(struct Link));
cursor->next=temp;
cursor=temp;
cursor->elem=i;
}
cursor->next=NULL;
}
printf("链表创建成功!\n");
return head;
}
void Display_Link(struct Link *list,int *length){
if(*length==0){
printf("当前链表为空!\n");
}
else{
int i=1;
while(list){
printf("第%.3d/%.3d个链表元素为:%3d\n",i++,*length,list->elem);
list=list->next;
}
}
}
void Destory_Link(struct Link *list,int *length){
if(*length==0){
printf("当前不存在链表!\n");
}
else{
long *Dead=(long*)malloc((*length)*sizeof(long));
struct Link *cursor=list;
int cunt=0;
while(cursor){
Dead[cunt++]=(long)cursor;
cursor=cursor->next;
}
while(cunt){
free((long *)Dead[--cunt]);
}
free(Dead);
*length=0;
printf("链表销毁成功!\n");
}
}
struct Link *Insert_Elem(struct Link *list,int pos,int elem,int *length){
struct Link *cursor=list;
if(*length==0){
printf("当前无链表,无法插入链表元素!\n");
return list;
}
else{
struct Link *newNode=(struct Link*)malloc(sizeof(struct Link));
newNode->elem=elem;
if(pos==1){
newNode->next=list;
list=newNode;
}
if(pos>1){
for(int i=0;i<pos-2;++i){
cursor=cursor->next;
if(!cursor){
printf("插入位置无效!\n");
return list;
}
}
newNode->next=cursor->next;
cursor->next=newNode;
}
(*length)++;
}
return list;
}
struct Link *Delete_Elem(struct Link *list,int pos,int *length){
struct Link *cursor=list;
if(pos>*length){
printf("删除位置无效!\n");
return list;
}
else{
struct Link *temp=NULL;
if(pos==1){
temp=list;
list=list->next;
free(temp);
}
else{
for(int i=0;i<pos-2;++i){
cursor=cursor->next;
}
temp=cursor->next->next;
free(cursor->next);
cursor->next=temp;
}
(*length)--;
}
printf("元素删除成功!\n");
return list;
}
int Alter_Elem(struct Link *list,int pos,int elem){
for(int i=0;i<pos-1;++i){
list=list->next;
if(!list){
printf("所在位置无效!\n");
return 0;
}
}
list->elem=elem;
printf("元素修改成功!\n");
return 0;
}
void Seek_Elem(struct Link *list,int elem){
int i=0,j=1;
while(list){
if(list->elem==elem){
printf("查找的元素%d第%d的位置为:%d\n",elem,j++,i+1);
}
++i;
list=list->next;
}
printf("未找到指定元素!\n");
}