【数据结构与算法】单链表程序练习

#include <stdio.h>
#define ERROR 0
#define OK 1 
#define ElemType int
typedef int status;
typedef struct Node
{
	ElemType data;
	struct Node* next;	
}Node;
typedef struct Node* Linklist;

/*遍历链表元素并打印*/ 
status VisitList(Linklist L)
{
   Linklist p;
   int j=0;
   p=(Linklist)malloc(sizeof(Node));
   p=L->next;
   while(p)
   {
   	 printf("%d ",p->data);
   	 p=p->next;
   	 j++;
   }	
   printf("链表长度为:%d\n",j);
   return OK;	
}
/*获取链表长度*/ 
int ListLength(Linklist L)
{
      Linklist p;
      //p=(Linklist)malloc(sizeof(Node));
	  int i=0;
	  p=L->next;
	  while(p)
	  {
  		   	 p=p->next;
   	         i++;
  	  }
  	  return i;  	
}
/*使用尾插法创建长度为n的链表*/ 
status CreateListTail(Linklist *L,int n)
{
	Linklist r,p;
	int j;
	*L=(Linklist)malloc(sizeof(Node));
	r=(Linklist)malloc(sizeof(Node));
	r=*L;
	srand(time(0));
	for(j=0;j<n;j++)
	{     
	    p=(Linklist)malloc(sizeof(Node));
	    p->data=rand()%100+1;
		r->next=p;
		r=p;
	}
	r->next=NULL;
    VisitList(*L);	
	return OK;
}
/*使用快慢指针法求得链表中间值*/ 
status ListlinkMid(Linklist L,ElemType *e)
{
	Linklist mid,search;
	mid=search=L;
	while((search->next)!=NULL)	
	{
		if((search->next->next)!=NULL)
		{
            search=search->next->next;
    		mid=mid->next;
    		
		}
		else
		{
			 search=search->next;
		}   
				
	}
	mid=mid->next;
	 *e=mid->data;
	return OK;
}
/*---获取链表第i个元素----*/
/*其中1<=i<=ListLength(L)*/  
status GetElem(Linklist L,int i,ElemType *e)
{
  Linklist p;
  int j=1;
  p=L->next;
  if((i<1)|| (i>ListLength(L)))
  {
   return ERROR;	
  }
  while(j<i)
  {
    p=p->next;
	j++;	
  }	
  *e=p->data;
  return OK;
}
/*---在链表第i个元素之前插入值e----*/
/*-----其中1<=i<=ListLength(L)----*/  
status ListInsert(Linklist *L,int i,ElemType e)
{
     Linklist p,s;
     p=*L;
	 int j;
	 if((i<1)||(i>ListLength(*L)))
	 {  
 		return ERROR;
 	 }	
     for(j=1;j<i;j++)
	    {
  	     p=p->next;           	
   	    }	  		
 	 s=(Linklist)malloc(sizeof(Node));
 	 s->data=e;
 	 s->next=p->next;
     p->next=s;
 	 return OK;
}
/*----------删除链表第i个元素------*/
/*-----其中1<=i<=ListLength(L)----*/ 
status ListDelete(Linklist *L,int i)
{
     Linklist p,q;
     p=*L;
	 int j;
	 if((i<1)||(i>ListLength(*L)))
	 {  
 		return ERROR;
 	 }	
     for(j=1;j<i;j++)
	    {
  	     p=p->next;           	
   	    }	  		
 	 q=p->next;
     p->next=q->next;
     free(q);
 	 return OK;
} 
/*------------链表整表删除---------*/
/*-----其中1<=i<=ListLength(L)----*/ 
status ClearList(Linklist *L)
{
     Linklist p,q;
     p=(*L)->next;
    while(p)
    { 
     	q=p->next;
     	free(p);
     	p=q; 	
     }
     (*L)->next = NULL ;
 	 return OK;
} 
int main()
 {	
     int x,n;
     ElemType e; 
	 Linklist L;
	 int CMR;
	 printf("请输入:\n");
     printf("1.查询链表:\n");
     printf("2.创建链表:\n");
     printf("3.查找中点:\n");
     printf("4.查找元素:\n");
     printf("5.插入元素:\n");
     printf("6.删除元素:\n");
     printf("7.删除链表:\n");
	 while(scanf("%d",&x)!=EOF){
	 switch(x)
    {
	 case 1: CMR=VisitList(L);
	         if(CMR)
         		 printf("OK\n");
         	 else
     	         printf("ERROR\n");
	         break;
	 case 2:CMR=CreateListTail(&L,9);
	         if(CMR)
	            {  
       		      printf("OK\n");
            	}
         	 else
     	         printf("ERROR\n");
             break;	
	 case 3: CMR=ListlinkMid(L,&e);
	         if(CMR)
	         {
         		printf("mid is:%d\n",e); 
          		printf("OK\n");
           	 }        		   		  	 
         	 else
     	         printf("ERROR\n");
	         break;	         
	  case 4: printf("请输入查找元素:");
              scanf("%d",&n);
	          CMR=GetElem(L,n,&e);
	          if(CMR)
	          {
         		printf("the %d number is:%d\n",n,e); 
          		printf("OK\n");
          	  }        		   		  	 
         	 else
     	         printf("ERROR\n");
	         break;  	
	  case 5:printf("请输入位置和元素:");	  
	         scanf("%d,%d",&n,&e);
			 CMR=ListInsert(&L,n,e); 
             if(CMR)
	         {  
	            VisitList(L);
          		printf("OK\n");
           	 }        		   		  	 
         	 else
 	            printf("ERROR\n");
	         break;  
	  case 6: printf("请输入删除元素:");
              scanf("%d",&n);
	          CMR=ListDelete(&L,n);
	          if(CMR)
	          {
         	    VisitList(L);
          		printf("OK\n");
          	  }        		   		  	 
         	 else
     	         printf("ERROR\n");
	         break;  
 	  case 7: CMR=ClearList(&L);
	         if(CMR)
                 printf("OK\n");	
           	 else
     	         printf("ERROR\n");
	         break;	  	  
     default:printf("输入有误\n");   
             break;
	}
	
	 }
	return 0;
 } 

            
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页