链表:前插法建立单链表。

#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef  struct list
{  
	int  data;
   	struct list  *next;
}LNode;
LNode *creatlist()
{  
	LNode  *h,*p;      
	int  i,x,n;
   	h=(LNode *)malloc(sizeof(LNode));
   	h->next=NULL;
   	printf("请输入链表结点个个数\n");
   	scanf("%d",&x);
   	printf("请依次输入结点的值\n");
   	while(x--)//前插法 
   	{  
   	   scanf("%d",&n); 
	   p=(LNode *)malloc(sizeof(LNode));
       p->data=n;  
	   p->next=h->next;
	   h->next=p;
   	}
   	return h;
}
void outlist(LNode  *h)
{  
   LNode  *p;
   p=h->next;
   if (p==NULL)  
   		printf("\nThe list is NULL!\n");
   else
   {  
   	  printf("\nHead");
      do { 
	  	printf("->%d",p->data);  
	  	p=p->next;    
	  } while(p!=NULL);
      printf("->End\n");
  }
}
main( )
{  
	LNode  *A;    
   	A=creatlist();
	outlist(A);
}
全部评论

相关推荐

头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务