当前位置: 首页 > news >正文

some code of c

//
//  main.c
//  LineList
//
//  Created by Rubert on 16/9/11.
//  Copyright © 2016年 Study. All rights reserved.
//

#include <stdio.h>
//定义链表数据结构
struct node
{
    int num;
    struct node *next;
    struct node *prior;

};
//函数声明
struct node *create();
void print();
void linkedList();
void headInsertLinkedList();
void tailInsertLinkedList();
void findLinkedList();
void insertLinkedList();
void deleteLinkedList();
void circleInsertLinkedList();
void doubleInsertLinkedList();
int main(int argc, const char * argv[]) {
    // insert code here...
    //headInsertLinkedList();
    //tailInsertLinkedList();
    //findLinkedList();
    //insertLinkedList();
    //deleteLinkedList();
    //circleInsertLinkedList();
    doubleInsertLinkedList();
    return 0;
}


/*
 * 头插入法创建链表
 */
void headInsertLinkedList() {
    struct node *head = NULL;
    struct node *p1;
    for(int i = 1; i < 10; i ++) {
        p1 = (struct node*)malloc(sizeof(struct node));
        p1 -> num = i;
        p1 -> next = head;
        head = p1;
    }
    print(head);
}

/*
 * 尾插入法创建链表
 */
void tailInsertLinkedList() {
    struct node *head = NULL;
    struct node *p1,*p2;
    p1 = p2 = (struct node*)malloc(sizeof(struct node));
    for(int i = 1; i < 10; i ++) {
        if(head == NULL) {
            head = p1;
        } else {
            p2 -> next = p1;
        }
        p2 = p1;
        p1 = (struct node*)malloc(sizeof(struct node));
        p1->num = i;
    }
    print(head);
}


/**
 * 创建循环链表
 */
void circleInsertLinkedList() {
    struct node *head = NULL;
    struct node *p1,*p2;
    p1 = p2 = (struct node*)malloc(sizeof(struct node));
    for(int i = 1; i < 10; i ++) {
        if(head == NULL) {
            head = p1;
        } else {
            
            if(i == 9) {
                p1->next = head;
            }
            p2 -> next = p1;
        }
        p2 = p1;
        p1 = (struct node*)malloc(sizeof(struct node));
        p1->num = i;
        
    }
    print(head);
}

/**
 * 创建双向链表
 */
void doubleInsertLinkedList() {
    struct node *head = NULL;
    struct node *p1,*p2;
    p1 = p2 = (struct node*)malloc(sizeof(struct node));
    for(int i = 1; i < 10; i ++) {
        if(head == NULL) {
            head = p1;
        } else {
            /*if(i == 9) {
                p1->next = head;
            }*/
            p2 -> next = p1;
            p1 -> prior = p2;
        }
        p2 = p1;
        p1 = (struct node*)malloc(sizeof(struct node));
        p1->num = i;
        
    }
    print(head);
}

/*
 * 链表查找
 */
void findLinkedList() {
    
    struct node *head = NULL;
    struct node *p1;
    for(int i = 1; i < 10; i ++) {
        p1 = (struct node*)malloc(sizeof(struct node));
        p1 -> num = i;
        p1 -> next = head;
        head = p1;
    }
    
    struct node *p;
    p = head -> next;
    int j = 1;
    int i = 5;
    while(p !=NULL && j < i)
    {
        p = p->next;
        ++j;
    }
    printf("%6d %d %d\n",p->num, p, p->next);/*输出链表节点的值*/
}

/*
 * 链表中插入
 */

void insertLinkedList() {
    
    struct node *head = NULL;
    struct node *p1;
    for(int i = 1; i < 10; i ++) {
        p1 = (struct node*)malloc(sizeof(struct node));
        p1 -> num = i;
        p1 -> next = head;
        head = p1;
    }
    
    struct node *p,*m;
    p = head;
    int j = 0;
    int i = 5;
    while(p !=NULL && j < i-1)
    {
        p = p->next;
        ++j;
    }
    
    if(p == NULL) {
        printf("error");
    } else {
        m = (struct node*)malloc(sizeof(struct node));
        m->num = j;
        m->next = p -> next;
        p->next = m;
    }
    
    print(head);
    
}


/*
 * 链表中删除
 */

void deleteLinkedList() {
    
    struct node *head = NULL;
    struct node *p1;
    for(int i = 1; i < 10; i ++) {
        p1 = (struct node*)malloc(sizeof(struct node));
        p1 -> num = i;
        p1 -> next = head;
        head = p1;
    }
    
    struct node *p,*m;
    p = head;
    int j = 0;
    int i = 5;
    while(p !=NULL && j < i-2)
    {
        p = p->next;
        ++j;
    }
    
    if(p == NULL) {
        printf("error");
    } else {
        //m = (struct node*)malloc(sizeof(struct node));
        //m->num = j;
        //m->next = p -> next;
        p->next = p->next->next;
        
    }
    
    print(head);
    
}

/*
 * sample 1
 */
void linkedList()
{
    struct node *head;
    head = NULL;//创建一个空表
    head = create(head); /* 创建单链表 */
    print(head);/* 打印单链表 */
    
}


struct node *create(struct node *head) //返回的是与节点相同类型的指针
{
    struct node *p1,*p2;
    int i = 1;
    //利用malloc() 函数向系统申请分配一个节点
    p1 = p2 = (struct node*)malloc(sizeof(struct node));//新节点
    printf("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %d\n",p1);
    scanf("%d",&p1->num);/*输入节点的值*/
    p1->next=NULL;/*将新节点的指针置为空*/
    while(p1->num > 0)/*输入节点的数值大于0*/
    {
        //④将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新节点接到表尾;
        if(head == NULL)
            head = p1;/*空表,接入表头*/
        else
            p2->next = p1;/*非空表,接到表尾*/
        p2 = p1;
        
        p1 = (struct node*)malloc(sizeof(struct node));/*下一个新节点*/
        i = i+1;
        printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %d\n",i,p2);
        scanf("%d",&p1->num);/*输入节点的值*/
        //⑤判断一下是否有后续节点要接入链表,若有转到3 ),否则结束;
    }
    //==============原来程序更正部分:(多谢@daling_datou提醒)================================
    //free(p1);  //申请到的没录入,所以释放掉
    p1 = NULL;   //使指向空
    p2->next = NULL; //到表尾了,指向空
    printf("链表输入结束(END)\n");
    //==============================================
    return head;
}




void print(struct node *head)/*出以head为头的链表各节点的值*/
{
    struct node *temp;
    temp = head;/*取得链表的头指针*/
    
    printf("\n\n\n链表存入的值为:\n");
    while(temp != NULL)/*只要是非空表*/
    {
        printf("%6d %d %d %d\n",temp->num, temp, temp->next, temp->prior);/*输出链表节点的值*/
        temp = temp->next;/*跟踪链表增长*/
    }
    printf("链表打印结束!!");
}

 

相关文章:

  • __stdcall、__cdcel和__fastcall
  • 温故知新之javascript面向对象
  • Validation Application Block动手实验室
  • 关于 WebRequest.RegisterPrefix
  • 慎用margin系列1---CSS的margin塌陷(collapse) 问题与对策
  • Workarounds一词的翻译
  • jquery ui中的dialog,官网上经典的例子
  • screen 后台实时执行命令
  • IP数据包首部校验和的计算
  • 烂泥:rsync与inotify集成实现数据实时同步更新
  • 使用junitamp;spring修改系统的环境变量,解决docker程序测试问题
  • Eclipse下使用Subversion =subclipse
  • 上传附件
  • check_nginx pnp4nagios 模版
  • 统计挖掘那些事那些情(2)-回归分析spss
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • Brief introduction of how to 'Call, Apply and Bind'
  • eclipse(luna)创建web工程
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • k8s 面向应用开发者的基础命令
  • MaxCompute访问TableStore(OTS) 数据
  • SOFAMosn配置模型
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 电商搜索引擎的架构设计和性能优化
  • 读懂package.json -- 依赖管理
  • 你不可错过的前端面试题(一)
  • 排序(1):冒泡排序
  • 前端代码风格自动化系列(二)之Commitlint
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 算法系列——算法入门之递归分而治之思想的实现
  • 探索 JS 中的模块化
  • Java总结 - String - 这篇请使劲喷我
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • raise 与 raise ... from 的区别
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • 组复制官方翻译九、Group Replication Technical Details
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • (6)添加vue-cookie
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (转)h264中avc和flv数据的解析
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • *p++,*(p++),*++p,(*p)++区别?
  • .mysql secret在哪_MYSQL基本操作(上)
  • .NET 8.0 发布到 IIS
  • .NET CF命令行调试器MDbg入门(一)
  • .NET Core 版本不支持的问题
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net framework4与其client profile版本的区别