//可直接运行
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
/*------------------------------------------------------*/
typedef int Elemtype;
typedef struct Node{
Elemtype data;
struct Node*next;
}LNode;
/*------------------------------------------------------*/
//带头结点的线性链表
void createlist(LNode*L)
{int c;
printf("please input the elem and end with #\n");
while((scanf("%d",&c)))
{
LNode*temp=(LNode*)malloc(sizeof(LNode));
temp->data=c;
temp->next=L->next;
L->next=temp;
(L->data)++;
}
}
void InitiList( LNode* L)
{L=(LNode*)malloc(sizeof(LNode));
if(!L)
exit(0);
L->data=0;
L->next=NULL;
}
void DestroyList(LNode * L)
{
free(L);
}
void ClearList(LNode*L)
{
L->data=0;
L->next=NULL;
}
void ListEmpty(LNode*L)
{
if(L->data==0)
printf("the list is empty\n");
else
printf("the list is not empty\n");
}
int ListLength(LNode*L)
{
return(L->data);
}
//return the number you wantted
Elemtype GetElem(LNode*L,int i)
{Elemtype e;
while(i>(L->data)||i<1)
{printf("the number you want is illeagl\n");
scanf("%d",&i);
}
LNode*p=L;
for(int j=0;j<i;j++)
p=p->next;
e=p->data;
return e;
}
void LocatElem(LNode*L,Elemtype e)
{int j;
LNode*p;
p=L->next;
for( j=0;j<L->data;j++,p=p->next )
{
if(p->data==e)
{printf("the number you want is in %dth\n",j);
break;
}
}
if(j>=(L->data))
printf("the number you want wae not in list\n");
}
void PriorElem(LNode*L,Elemtype e)
{int j;
LNode*p;
LNode*temp;
p=L->next;
for( j=0;j<L->data;j++,temp=p,p=p->next )
{
if(p->data==e)
{
break;
}
}
if(j>=(L->data))
printf("the number you want wae not in list\n");
printf("the priorelem number of %d is %d\n",e,temp->data);
}
void Nextelem(LNode*L,Elemtype e)
{int j;
LNode*p;
LNode*temp;
p=L->next;
temp=p;
for( j=0;j<L->data;j++,p=p->next )
{temp=temp->next;
if(p->data==e)
{
break;
}
}
if(j>=(L->data))
printf("the number you want wae not in list\n");
printf("the nextelem number of %d is %d\n",e,temp->data);
}
void ListInsert(LNode *L,int i, Elemtype e)
{
if(i<1||i>L->data)
{
printf("the order you insert is error\n");
}
LNode*p=L;
for(int j=0;j<i;j++)
p=p->next;
LNode*np=(LNode*)malloc(sizeof(LNode));
np->data=e;
np->next=p->next;
p->next=np;
(L->data)++;
}
void ListDelete(LNode*L,int i)
{
Elemtype e;
LNode*p=L;
if(L->data==0)
printf("here is a empty list \n");
else{
while(i<1||i>L->data)
{
printf("the number you input is error\nplease input again\n");
scanf("%d",&i);
}
for(int j=0;j<i;j++)
p=p->next;
e=p->data;
p->next=p->next->next;
L->data--;
printf("the number you delete is %d\n",e);
}
}
void ListTrabverse(LNode*L)
{
LNode*p=L->next;
if(L->data==0)
printf("there was not elements in list\n");
else{
printf("\n-------------all elements of liear table----------------\n");
for(;p!=NULL;p=p->next)
printf(" %d ",p->data);
}
}
void menu(void){
printf("\n 武汉大学 \n");
printf(" Mune for Linear Table On Sequence Structure \n");
printf("------------------------------------------------------\n");
printf(" 1. IntiaList 7. LocatElem\n");
printf(" 2. DestroyList 8. priorElem\n");
printf(" 3. ClearList 9. next elem \n");
printf(" 4. ListEmpty 10. ListInsert\n");
printf(" 5. ListLength 11. ListDelete\n");
printf(" 6. GetElem 12. ListTrabverse\n");
printf(" 0. Exit\n");
printf("------------------------------------------------------\n");
}
int main()
{
LNode List,*L;
L=&List;
L->data=0;
L->next=NULL;
createlist(L);
getchar();
int op=0;
do{ menu();
printf(" Please input your option[0-12]:");
scanf("%d",&op);
switch(op){
case 0: break;
case 1:
{
printf("\n here is IntiaList()\n");
InitiList(L);
break;
}
case 2: printf("\n here is DestroyList()\n");
DestroyList( L);
break;
case 3: printf("\n here is ClearList()\n");
ClearList(L);
break;
case 4: printf("\n here is ListEmpty()\n");
ListEmpty(L);
break;
case 5: printf("\n here is ListLength() \n");
printf("\nthe length of list is %d\n",ListLength(L));
break;
case 6:
{getchar();
printf("\n here is GetElem()\n");
int i;
int temp;
printf("/n/nplease input the number you want to get\n");
scanf("%d",&i);
temp=GetElem(L,i);
printf("the number you want is %d \n",temp);
break;
}
case 7: printf("\n here is LocatElem()\n");
printf("please insert the number you want to search\n");
int e;
scanf("%d",&e);
LocatElem(L, e);
break;
case 8: printf("\n here is PriorElem()\n");
printf("please input the number you want to find its prior\n");
int ee;
scanf("%d",&ee);
PriorElem(L,ee);
break;
case 9: printf("\n next elem \n");
printf("please input the number you want to find its next\n");
int e3;
scanf("%d",&e3);
Nextelem(L,e3);
break;
case 10: printf("\n here is ListInsert()\n");
int ii,e2;
printf("please input the number you want to insert\n");
scanf("%d",&e2);
printf("please input the location you want to insert\n");
scanf("%d",&ii);
while(ii<0||ii>L->data)
{
printf("the number you input is error\nplease input again\n");
scanf("%d",&ii);
}
ListInsert( L,ii, e2);
break;
case 11: printf("\n here is ListDelete()\n");
int iii;
printf("please input the location you want to delete\n");
scanf("%d",&iii);
ListDelete(L,iii);
break;
case 12: printf("\n here is ListTrabverse()\n");
ListTrabverse(L);
break;
default: ;
}
}while(op);
return 0;
}
链表的c源代码及一些相关操作
最新推荐文章于 2024-04-23 15:14:23 发布