#include
#include
#define ERROR 1
#define OK 0
typedef int ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
//++++++++++++++++++++++链表的初始化++++++++++++++++++++
Status InitList(LinkList *L)
{
*L=(LinkList)malloc(sizeof(struct LNode));
if(!*L)
exit(1);
(*L)->next=NULL;
return OK;
}
//++++++++++++++++++++++链表的长度计算+++++++++++++++++++
int ListLength(LinkList L)
{
int i=0;
LinkList p=L->next;
while(p)
{
i++;
p=p->next;
}
return i;
}
//+++++++++++++++++++++获取链表中元素+++++++++++++++++++
Status GetElem(LinkList L,int i,ElemType *e)
{
int j=1;
LinkList p=L->next;
while(p&&j
{
p=p->next;
j++;
}
if(!p||j>i)
return ERROR;
*e=p->data;
return OK;
}
//++++++++++++++++++++++链表的插入元素+++++++++++++++++++++++++
Status ListInsert(LinkList L,int i,ElemType e)
{
int j=0;
LinkList p=L,s;
while(p&&j
{
p=p->next;
j++;
}
if(!p||j>i-1)
return ERROR;
s=(LinkList)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
//+++++++++++++++++++++++++删除链表中的元素+++++++++++++++++++++++++
Status ListDelete(LinkList L,int i,ElemType *e)
{
int j=0;
LinkList p=L,q;
while(p->next&&j
{
p=p->next;
j++;
}
if(!p->next||j>i-1)
return ERROR;
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;
}
//+++++++++++++++++++++++++打印链表的值+++++++++++++++++++++++++
Status ListTraverse(LinkList L)
{
LinkList p=L->next;
while(p)
{
printf(" %d",p->data);
p=p->next;
}
printf("\n");
return OK;
}
//+++++++++++++++++++++++main++++++++++++++++++++++++++++++++
int main (void)
{
LinkList L;
ElemType num;
int pos;
char input;
InitList(&L);
do{
printf("input the positon and the number you want to L:");
scanf("%d %d",&pos,&num);
ListInsert(L,pos,num);
fflush(stdin); // to flush the content in std cahe e.g.‘\n’
printf("Do you want to input another time(y/n)?\n");
scanf("%c",&input);
}while (tolower(input)=='y');
printf("after Insert op the number(s) in the list is :");
ListTraverse(L);
return 0;
}
阅读(557) | 评论(0) | 转发(0) |