• 跑骚时刻


    #include "stdafx.h"
    #include <stdlib.h>
    
    //声明函数
    //创建一个链表
    struct Node * create_list(void);
    //遍历链表中的内容
    void traverse_list(struct Node *);
    
    //链表的结构体
    struct Node
    {
    	//数据域
    	int data;
    	char data2;
    	//指针域
    	struct Node * pNext;
    };
    
    //主函数
    int main(int argc, _TCHAR* argv[])
    {
    	/*
    	2014年5月20日 20:30:48		链表
    	*/
    
    	//声明一个链表指针
    	struct Node * pHead = NULL;
    	//获取链表
    	pHead = create_list();
    	//循环输出链表的内容
    	traverse_list(pHead);
    
    
    
    	
    	printf("
    ");
    	system("pause");
    	return 0;
    }
    
    //创建一个链表
    struct Node * create_list(void){
    	int len;//用来存放节点的个数
    	int val;//用于存放节点的值
    	//头节点
    	struct Node * pHead = (struct Node*)malloc(sizeof(struct Node));
    	if (NULL == pHead)
    	{
    		printf("分配失败!程序终止");
    		exit(-1);//终止程序
    	}
    	struct Node * pTail = pHead;//首节点
    	pTail->pNext = NULL;
    
    	printf("请输入需要生成的链表节点个数:len=");
    	scanf_s("%d", &len);
    	for (int i = 0; i < len; i++)
    	{
    		printf("请输入第%d个节点的值:", i + 1);
    		scanf_s("%d", &val);
    
    		struct Node * pNew = (struct Node*)malloc(sizeof(struct Node));
    		if (NULL == pNew)
    		{
    			printf("分配失败!程序终止");
    			exit(-1);
    		}
    		pNew->data = val;//为当前节点赋值
    		pTail->pNext = pNew;//为上一个节点的指针域赋值
    		pNew->pNext = NULL;//清空当前节点的指针域,如果当前为NULL。当前节点为链表的最后一个值
    		pTail = pNew;//更新当前节点为:下一个节点的上一个节点;
    	}
    	return pHead;//返回的是头结点
    }
    
    //遍历链表中的内容
    void traverse_list(struct Node * pHead){
    	struct Node * p = pHead->pNext;
    	printf("链表中的值为:");
    	while (NULL != p)
    	{
    		printf("%d  ", p->data);
    		p = p->pNext;
    	}
    }
    

      

  • 相关阅读:
    ajax 发送请求无法重定向问题
    网页中转跳转的几种方式
    后台返回的HTML整个页面代码打开方法
    Json对象与Json字符串的转化、JSON字符串与Java对象的转换
    SpringMVC ModelAndView跳转失效
    springMVC中前台ajax传json数据后台controller接受对象为null
    $.ajax 中的contentType
    springMVC--请求的跳转和传值
    Windows NT WinLogon Notify
    虚拟机检测技术剖析
  • 原文地址:https://www.cnblogs.com/oncoy/p/3740972.html
Copyright © 2020-2023  润新知