Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104536
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: C/C++

2013-10-20 16:37:28

#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;
}

阅读(537) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~