链表c--单项链表--自己练手

chain.c
#include<stdio.h>
#include<malloc.h>
typedef struct node Node;

struct node
{
	Node *pnext;
	int num;
};
Node* listcreat()
{
	Node* head;
	head=(Node*)malloc(sizeof(Node));
	head->num=0;
	head->pnext = NULL;
	return head;
}
void listadd(Node* ph,int num)
{
	Node* ptem=ph;
	Node* padd;
	if(ph==NULL)
	{
		printf("the list is null");
		return ;
	}
	while(ptem->pnext != NULL)
		ptem=ptem->pnext;
		padd=(Node*)malloc(sizeof(Node));
		padd->num=num;;
		ptem->pnext=padd;
		padd->pnext=NULL;
}
int listlen(Node* ph)
{
	Node* ptem=ph;
	int i=1;
	if (ptem == NULL)
			return 0;
	while(ptem->pnext != NULL)
	{
		ptem=ptem->pnext;
		i++;	
	}
	return i;
}
void dellist(Node* ph,int i)
{
	Node* ptem=ph;
	Node* pcur;
	int len=0;
	int j=0;
	len=listlen(ph);
	if((len<=1)&&(i<1)&&(i>len))
		return ;
	while(j<i-1)
	{		pcur=ptem;
			ptem=ptem->pnext;
			j++;
	}
	pcur->pnext=ptem->pnext;
	free(ptem);
	return;
}
void listscan(Node*ph)
{
	int i=1;
	Node* ptem=ph;
	if(ph==NULL)
		return ;
	while(ptem->pnext !=NULL)
	{
			printf("the %d number:is %d \n",i,ptem->num);
			ptem=ptem->pnext;
			i++;
	}
	printf("the last %d number:is %d \n",i,ptem->num);
	return;	
}
int main()
{
	Node*head, ptem,pnode;
	head=(Node*)listcreat();
	if (head==NULL)
	{
		printf("creat error");
		return 0;	
	}
	listadd(head,2);
//	listadd(head,3);
	listscan(head);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值