some code of c

Posted 洛易

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("链表打印结束!!");
}

 

以上是关于some code of c的主要内容,如果未能解决你的问题,请参考以下文章

精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解

非常有用的 48 个 JavaScript 代码片段

C中的命名空间

eat the ____??___ of some vegetables

是否可以注释掉包含注释的代码块?

C语言常见问题(10):Sections of code should not be commented out