zoukankan      html  css  js  c++  java
  • 写一个函数insert,用来向一个动态链表插入结点

    写一个函数insert,用来向一个动态链表插入结点

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct LNode
    {
    	int num;
    	struct LNode *next;
    } LNode;
    
    void insert(int n, LNode *node)
    {
    	//创建新节点
    	LNode *newNode = (LNode *)malloc(sizeof(LNode));
    	newNode->num = n;
    
    	LNode* next = node->next;
    
    	// node ---> newNode  ---> next
    	newNode->next = next;
    	node->next = newNode;
    }
    
    LNode* creat(int n)
    {
    	LNode *head, *p;
    	head = (LNode *)malloc(sizeof(LNode));
    	p = head; //头节点为0 加上头节点共11个节点
    	head->num = 0;
    	head->next = NULL;
    	for (int i = 1; i <= n; i++)
    	{
    		LNode *newNode = (LNode *)malloc(sizeof(LNode));
    		newNode->num = i;
    		newNode->next = NULL;
    		p->next = newNode;
    		p = p->next;
    	}
    	return head;
    }
    
    void printNode(LNode* head)
    {
    	LNode* p = head->next;
    	while (p != NULL)
    	{
    		printf("num -> %d
    ", p->num);
    		p = p->next;
    	}
    }
    
    int main()
    {
    	LNode *head;
    	int n;
    	head = creat(10);
    	printNode(head);
    	printf("请输入需要插入的节点:
    ");
    	scanf("%d", &n);
    	insert(n, head);
    	printf("链表的新内容:
    ");
    	printNode(head);
    	return 0;
    }
    

    运行截图:

    写一个函数insert,用来向一个动态链表插入结点

  • 相关阅读:
    实现跨域的几种方法
    2015-07-15
    unity3d中给GameObject绑定脚本的代码
    unity3d的碰撞检测及trigger
    区块链 (未完)
    mono部分源码解析
    量化策略分析的研究内容
    mono搭建脚本整理
    unity3d简介
    Hook技术之API拦截(API Hook)
  • 原文地址:https://www.cnblogs.com/cyuyanchengxu/p/13470495.html
Copyright © 2011-2022 走看看