单链表之插入结点

单链表之插入结点

题:编写程序实现单链表的插入。【美国某著名计算机嵌入式公司2005年面试题】

答案:完整代买如下:

 

// P167_example1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>

typedef struct student
{
	int data;
	struct student *next;
}node;

//建立单链表
node* create()
{
	node *head,*p,*s;
	int x, cycle = 1;
	head = (node *)malloc(sizeof(node));
	p = head;
	while(cycle)
	{
		std::cout<<"please input the data: ";
		std::cin>>x;
		std::cout<<std::endl;
		if(x != 0)
		{
			s = (node *)malloc(sizeof(node));
			s->data = x;
			p->next = s;
			p = s;
		}
		else
			cycle = 0;
	}
	head = head->next;
	p->next = NULL;
	return head;
}
//单链表测长
int length(node *head)
{
	int n = 0;
	node *p;
	p = head;
	while(p != NULL)
	{
		p = p->next;
		n++;
	}
	return n;
}
//单链表打印
void print(node *head)
{
	node *p;
	int n;
	n = length(head);
	std::cout<<"Now, These "<<n<<" records are: "<<std::endl;
	p = head;
	if(p != NULL)
	{
		while(p != NULL)
		{
			std::cout<<p->data<<" -> ";
			p = p->next;
		}
		std::cout<<std::endl;
	}
}
//单链表删除结点
node* del(node *head, int num)
{
	node *p1,*p2;
	p1 = head;
	while(num != p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	if(num == p1->data)
	{
		if(p1 == head)
		{
			head = p1->next;
			free(p1);
		}
		else
		{
			p2->next = p1->next;
			free(p1);
		}
	}
	else
	{
		std::cout<<num<<" could not been found"<<std::endl;
	}
	return head;
}
//插入结点
node* insert(node *head, int num)
{
	node *p0,*p1,*p2;
	p1 = head;
	p0 = (node *)malloc(sizeof(node));	//待插入的结点
	p0->data= num;
	while(p0->data > p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	if(p0->data < p1->data)
	{
		if(p1 == head)
		{
			p0->next = p1;
			head = p0;
		}
		else
		{
			p0->next = p1;
			p2->next = p0;
		}
	}
	else
	{
		p1->next = p0;
		p0->next = NULL;
	}
	return head;
}


int _tmain(int argc, _TCHAR* argv[])
{
	node *head;
	head = create();
	print(head);
	//删除结点
	int num;
	std::cin>>num;
	head = del(head, num);
	print(head);	//打印删除后的单链表
	//插入结点
	std::cin>>num;
	head = insert(head, num);
	print(head);
	return 0;
}

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110061.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 定点数和浮点数_定点数和浮点数哪个精度高

    定点数和浮点数_定点数和浮点数哪个精度高计算机中常用的数据表示格式有两种,一是定点格式,二是浮点格式。所谓定点数和浮点数,是指在计算机中一个数的小数点的位置是固定的还是浮动的:如果一个数中小数点的位置是固定的,则为定点数;如果一个数中小数点的位置是浮动的,则为浮点数。一般来说,定点格式可表示的数值的范围有限,但要求的处理硬件比较简单。而浮点格式可表示的数值的范围很大,但要求的处理硬件比较复杂。采用定点数表示法的计算机称为定点计算

    2022年10月22日
  • Kong网关初探_API网关

    Kong网关初探_API网关安装Kong安装文档Kong开源版不提供dashboard支持,只有Kong企业版才有该功能。但有第三方控制台Konga同样可以友好地管理KongAdminAPI对象,快速安装如下:dockerrun-d-p1337:1337\–namekonga\–network=kong-net\-eDB_ADAPTER=postgres\-eDB_HOST=kong-database\-eDB_PORT=5432\-eDB_USER=kong\

    2022年9月10日
  • BIRCH详解_Bilabial

    BIRCH详解_BilabialBIRCH(BalancedIterativeReducingandClusteringusingHierarchies)详解第三十次写博客,本人数学基础不是太好,如果有幸能得到读者指正,感激不尽,希望能借此机会向大家学习。这一篇作为可伸缩聚类(ScalableClustering)算法的第三篇,主要是对BIRCH(BalancedIterativeReducingand…

    2022年10月31日
  • 安装vmware虚拟机步骤_如何安装vmware虚拟机

    安装vmware虚拟机步骤_如何安装vmware虚拟机一、基础介绍VMWare虚拟机软件是一个“虚拟PC”软件,它使你可以在一台机器上同时运行二个或更多Windows、DOS、LINUX系统。与“多启动”系统相比,VMWare采用了完全不同的概念。多启

    2022年8月2日
  • 我用了两年时间去读《Thinking in Java》

    我用了两年时间去读《Thinking in Java》路漫漫其修远兮,吾将上下而求索。——题记我用了两年时间去读《Think…

    2022年7月7日
  • 亚马逊EBS:aws ebs 和 ssd的区别「建议收藏」

    亚马逊EBS:aws ebs 和 ssd的区别

    2022年2月16日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号