栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C语言】【链表】尾插法创建一个链表,并在指定位置插入一个结点

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C语言】【链表】尾插法创建一个链表,并在指定位置插入一个结点

题目描述 

创建(尾插法)带头结点的链表,并在指定位置插入一个数据,如果成功,则输出插入后链表中的所有数据,如果插入失败,则输出“INSERT ERROR”

输入

第一行输入一组整数,以-1作为结束标志

第二行输入两个整数x和pos

输出

插入成功,输出整个链表数据,如果失败则显示“INSERT ERROR”

样例输入

1 2 3 4 5 -1 111 3

样例输出

1 2 111 3 4 5

带填充标签的C/C++原程序
#include"stdio.h"
#include"stdlib.h"
struct node {
    int data;
    struct node *next; 
};

typedef struct node  linknode;
linknode * createfromtail();
void printlink(linknode *h);
void destroylink(linknode *h);
int insert( linknode *h,int x ,int pos);

int main()
{
    linknode *head=NULL;
    int x;
    int pos,r;
    head=createfromtail();
    scanf("%d%d",&x,&pos);
    r=insert(head,x,pos);
    if (r<1)  //插入失败 
       printf("INSERT ERRORn");
    else  //插入成功,输出链表
       printlink(head);
    destroylink(head);
    return 0;
}

linknode * createfromtail()
{
    linknode *head,*tail,*p;
    int s;
    head=(linknode *)malloc(sizeof(linknode ));
    head->next=NULL;
    @@1

    @@1
    return head; 
}


void destroylink(linknode *h)
{
    linknode *q;
    while(h!=NULL)
    {
        q=h;
        h=h->next;
        free(q);
    }
}

void printlink(linknode *h)
{
    linknode *p=h->next;
    while(p)
    {
    	printf("%d ",p->data);
    	p=p->next;
    }
}

@@2

@@2

代码答案

注意第一个和最后一个的位置讨论,且本题是带头结点的链表

#include"stdio.h"
#include"stdlib.h"
struct node {
    int data;
    struct node *next; 
};

typedef struct node  linknode;
linknode * createfromtail();
void printlink(linknode *h);
void destroylink(linknode *h);
int insert( linknode *h,int x ,int pos);

int main()
{
    linknode *head=NULL;
    int x;
    int pos,r;
    head=createfromtail();
    scanf("%d%d",&x,&pos);
    r=insert(head,x,pos);
    if (r<1)  //插入失败 
       printf("INSERT ERRORn");
    else  //插入成功,输出链表
       printlink(head);
    destroylink(head);
    return 0;
}

linknode * createfromtail()
{
    linknode *head,*tail,*p;
    int s;
    head=(linknode *)malloc(sizeof(linknode ));
    head->next=NULL;
    
    tail=head; 
    while(scanf("%d",&s),s!=-1)
    {
    	p=(linknode *)malloc(sizeof(linknode));
    	p->data=s;
    	p->next=NULL;
    	tail=tail->next=p;
    }
    
    return head; 
}


void destroylink(linknode *h)
{
    linknode *q;
    while(h!=NULL)
    {
        q=h;
        h=h->next;
        free(q);
    }
}

void printlink(linknode *h)
{
    linknode *p=h->next;
    while(p)
    {
    	printf("%d ",p->data);
    	p=p->next;
    }
}

int insert( linknode *h,int x ,int pos)
{
	linknode *p=h->next,*q=NULL;
	if(pos>0)
	{
		linknode *s=(linknode *)malloc(sizeof(linknode ));
		s->data=x;
		int n=0;
		while(p->next&&++nnext;
		}
		if(n==pos||n+1==pos)
		{
			if(q==NULL)	{  s->next=p;h->next=s;  }
			else if(p->next==NULL)  {  s->next=NULL;p->next=s;  }
			else  {  s->next=p;q->next=s;  }
			return 1;
		}
	}
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号