zoukankan      html  css  js  c++  java
  • 单链表!!

    #include"stdio.h"
    #include"stdlib.h"
    struct linknode
    {
    	int date;
    	struct linknode *next;
    };
    struct linknode *creat()//建立链表
    {
    	struct linknode *head,*tail,*temp;
    	int dates;
    	head=temp=tail=NULL;
    	while(scanf("%d",&dates)!=EOF)
    	{
    		temp=(struct linknode*)malloc(sizeof(struct linknode));
    		if(temp==NULL)
    			printf("allocate error!\n");
    		else
    		{
    			temp->date=dates;
    			temp->next=NULL;
    			if(head==NULL)
    				head=tail=temp;
    			else
    			{
    				tail->next=temp;
    				tail=temp;
    			}
    		}
    	}
    	return head;
    }
    void print(struct linknode *head)//打印链表
    {
    	struct linknode *p;
    	p=head;
    	while(p)
    	{
    		printf("%d ",p->date);
    		p=p->next;
    	}
    }
    struct linknode*find(struct linknode*head,int dates)//找特定值
    {
    	struct linknode *p;
    	p=head;
    	while(p->date!=dates&&p->next!=NULL)
    	{
    		p=p->next;
    	}
    	if(p->date==dates)
    		return p;
    	else
    		return NULL;
    }
    struct linknode*findahead(struct linknode *head,int dates)//找特定值的前一个
    {
    	struct linknode *p,*q;
    	p=head;
    	q=NULL;
    	while(p->date!=dates&&p->next!=NULL)
    	{
    		q=p;
    		p=p->next;
    	}
    	if(p->date==dates)
    		return q;
    	else
    		return NULL;
    }
    struct linknode *entertohead(struct linknode *head,int dates)//在头指针前加数据
    {
    	struct linknode *enter;
    	enter=(struct linknode *)malloc(sizeof(struct linknode));
    	if(enter==NULL)
    		printf("allocate error!\n");
    	enter->date=dates;
    	enter->next=NULL;
    	if(head==NULL)
    		head=enter;
    	else
    	{
    		enter->next=head;
    		head=enter;
    	}
    	return head;
    }
    struct linknode *entertotail(struct linknode *head,int dates)//在链表尾加数据
    {
    	struct linknode *enter,*p;
    	p=head;
    	enter=(struct linknode*)malloc(sizeof(struct linknode));
    	if(enter==NULL)
    		printf("allocate error!\n");
    	enter->date=dates;
    	enter->next=NULL;
    	if(head==NULL)
    		head=enter;
    	else
    	{
    		while(p->next)
    		{
    			p=p->next;
    		}
    		p->next=enter;
    	}
    	return head;
    }
    struct linknode *entertovalue(struct linknode*head,int value,int dates)//特定位置加节点
    {
    	struct linknode *enter,*fp,*p;
    	p=findahead(head,value);
    	fp=find(head,value);
    	if(fp==NULL)
    		printf("no number is value\n");
    	enter=(struct linknode*)malloc(sizeof(struct linknode));
    	enter->date=dates;
    	enter->next=NULL;
    	if(enter==NULL)
    		printf("allocate error\n");
    	else
    	{
    		if(fp==head)
    		{
    			enter->next=head;
    			head=enter;
    		}
    		else
    		{
    			p->next=enter;
    			enter->next=fp;
    		}
    	}
    	return head;
    }
    struct linknode *delet(struct linknode *head,int dates)//删除节点
    {
    	struct linknode *delet,*p;
    	p=findahead(head,dates);
    	delet=find(head,dates);
    	if(!delet)
    		return NULL;
    	else
    	{
    		p->next=delet->next;
    		free(delet);
    		return head;
    	}
    }
    void main()//测试链表
    {
    	struct linknode *head,*fd;
    	head=creat();
    	print(head);
    	fd=findahead(head,4);
    	if(fd==NULL)
    		printf("ahead of value is NULL.\n");
    	else
    		printf("\n%d\n",fd->date);
    	print(head=entertohead(head,88));
    	printf("\n");
    	print(entertotail(head,77));
    	printf("\n");
    	head=delet(head,6);
    	if(head)
    		print(head);
    	else
    		printf("have no this number\n");
    	printf("\n");
    }

  • 相关阅读:
    圣诞节快乐 | 圣诞特效来了!!
    前端特效demo | 值得收藏的6个 HTML5 Canvas 实用案例
    前端特效demo | 一起围观 10 种创意时钟
    即学即用,轻松搞定这些选择器!(下)
    架构师究竟要不要写代码?
    偷懒秘诀之变量篇
    弹幕,是怎样练成的?
    [C++]模板类和模板函数
    [C++]typedef用法
    [面试]CVTE 2019提前批 Windows应用开发一面
  • 原文地址:https://www.cnblogs.com/yyf573462811/p/6365373.html
Copyright © 2011-2022 走看看