Andy 胡

导航

C语言 链表(VS2012版)

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Question{
    int _id;
    struct Question* pre;
    struct Question* next;
};
void chain_print(struct Question* qFirst){
    puts("----------print------------");
    struct Question* q = qFirst;
    if (qFirst->next == NULL){
        puts("没有元素可以打印");
        return;
    }else{
        q = qFirst->next;
    }
    // 遍历链表
    for(q; (q->next) != NULL;q=q->next ) {
        printf("%d\n", q->_id);
    }
    // 最后一项特殊对待
    printf("%d\n", q->_id);
}
void chain_add(struct Question* qFirst, struct Question* qAdd){
    // 遍历链表
    struct Question* q = qFirst;
    for(q; (q->next) != NULL;q=q->next ) {
    }
    // 最后一项
    q->next = qAdd;
    qAdd->pre = q;
}

void chain_remove(struct Question* qFirst, struct Question* qRemove){
    struct Question* qPre = NULL;
    struct Question* qNext = NULL;
    struct Question* q = qFirst;
    
    if (qFirst == qRemove){
        (qFirst->next)->pre = NULL;
        qFirst = (qFirst->next);
        free(q);
        return;
    }

    // 遍历链表


    for(q; (q->next) != NULL;q=q->next ) {
        if (q == qRemove){
            qPre = q->pre;
            qNext = q->next;
            if (qPre!=NULL){
                qPre->next= qNext;
            }
            if (qNext!=NULL){
                qNext->pre = qPre;
            }
            free(qRemove);
            return;
        }
    }
    // 最后一项
    if (q == qRemove){
        qPre = q->pre;
        if (qPre!=NULL){
            qPre->next= qNext;
        }
        free(qRemove);
    }
}

struct Question* chain_get(struct Question* qFirst, int index){
    int i = 0;
    // 遍历链表
    struct Question* q = qFirst;
    for(q; 
        (q->next) != NULL;
        q=q->next,i++ ) {
        if (index == i){
            return q;
        }
    }
    if (index == i){
        // 获取最后一个元素
        return q;
    }
    return NULL;
}

int chain_count(struct Question* qFirst){
    int i = 0;
    // 遍历链表
    struct Question* q = qFirst;
    for(q; 
        (q->next) != NULL;
        q=q->next,i++ ) {
    }

    return i;
}
struct Question* newQuestion(int id){
    struct Question* q = (struct Question*)malloc(sizeof(struct Question));
    memset(q, 0, sizeof(struct Question));
    q->_id = id;
    return q;
}

void newQuestion(int id, struct Question** q){
    *q = (struct Question*)malloc(sizeof(struct Question));
    memset(*q, 0, sizeof(struct Question));
    (*q)->_id = id;
}

int _tmain(int argc, _TCHAR* argv[])
{
    struct Question* qFirst = NULL;
    qFirst = newQuestion(0);

    struct Question* q1 = NULL;

    newQuestion(1,&q1);
    chain_add(qFirst, q1);

    struct Question* q2 = newQuestion(2);
    chain_add(qFirst, q2);

    struct Question* q3 = newQuestion(3);
    chain_add(qFirst, q3);

    struct Question* q4 = newQuestion(4);
    chain_add(qFirst, q4);

    struct Question* q5 = newQuestion(5);
    chain_add(qFirst, q5);

    chain_print(qFirst);


    printf("get %d\n", chain_get(qFirst, 1)->_id);
    printf("get %d\n", chain_get(qFirst, 3)->_id);
    printf("get %d\n", chain_get(qFirst, 5)->_id);
    //------------------------------
    chain_remove(qFirst, q3);
    chain_print(qFirst);
    chain_remove(qFirst, q5);
    chain_print(qFirst);
    chain_remove(qFirst, q1);
    chain_print(qFirst);

    printf("元素个数: %d\n", chain_count(qFirst));
    return 0;
}
----------print------------
1
2
3
4
5
get 1
get 3
get 5
----------print------------
1
2
4
5
----------print------------
1
2
4
----------print------------
2
4
元素个数: 2

 

posted on 2017-06-16 09:37  talkwah  阅读(276)  评论(0编辑  收藏  举报