公 告

欢迎各位网友添加友情链接,在您添加本博客:http://linux0818.blogspot.com/ 做为链接后, E-mail:linux0818@gmail.com给我,我将将您的网址添加到本博客。

2008年10月14日星期二

双链表的创建,显示,删除,插入,释放

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(1)         //结构体按一位对齐,ubuntu下默认按4位对齐,转换能节省内存
struct element {
    struct element *prev;
    char name[4];
    int age;
    struct element *next;
};
struct element *header = 0;
struct element *tail = 0;
struct element *temp = 0;

void insert(void);
void delchain(void);
void displayTwo(void);
void viewone(void);

void input(void)
{
    char tempName[16];
    char yesNo;
    int tempAge;
    do {
        temp = (struct element *) malloc(sizeof(struct element));
        printf("请输入姓名和年龄:");
        scanf("%s %d", tempName, &tempAge);
        strcpy(temp->name, tempName);
        temp->age = tempAge;
        temp->next = 0;

        if (!header) {
            temp->prev = 0;
            header = temp;
        } else {
             temp->prev = tail;
            tail->next = temp;
        }
        tail = temp;

        printf("是否继续输入?(Q或q退出)");
        yesNo = getchar();
        yesNo = getchar();
    } while (!(yesNo == 'q' || yesNo == 'Q'));
}

void display(void)
{
    int i = 0;
    temp = header;
    if (!temp) {
        printf("没有员工\n");
        return;
    }
    printf("所有员工信息如下:\n");
    while (temp) {
        printf
            ("第%d个员工 地址是:%u 姓名:%s(%u) 年龄:%d(%u)  下一个员工的地址:%d\n",
             i + 1, temp, temp->name, temp->name, temp->age, &(temp->age),
             temp->next);
        temp = temp->next;
        i++;
    }
    printf("共有%d个员工\n", i);
}

void freechain(void)
{
    while (header) {
        temp = header;
        header = header->next;

        free(temp);
    }
    header = 0;
    tail = 0;
    temp = 0;
}

int main(int argc, char **argv)
{
    char yesNo = 0;
    do {
        printf("\n");
        printf("------------XXX系统------------\n");
        printf("|    1. 输入                  |\n");
        printf("|    2. 显示                  |\n");
        printf("|    3. 释放                  |\n");
        printf("|    4. 插队                  |\n");
        printf("|    5. 删除                  |\n");
        printf("|    6. 查看                  |\n");
        printf("|    q. 退出                  |\n");
        printf("-------------------------------\n");
        printf("请选择:");
        yesNo = getchar();
        switch (yesNo) {
        case '1':
            input();
            break;
        case '2':
            display();
            displayTwo();
            break;
            case '3':
            freechain();
            break;
        case '4':
            insert();
            break;
        case '5':
            delchain();
            break;
        case '6':
            viewone();
        }
    } while (!(yesNo == 'q' || yesNo == 'Q'));
    return 0;
}

void insert(void)
{
    char tempName[16];
    int tempAge;
    int pos, i;
    struct element *tempPos;
    printf("请问要插队到第几个元素的后面?");
    scanf("%d", &pos);
    temp = (struct element *) malloc(sizeof(struct element));
    printf("请输入姓名和年龄:");
    scanf("%s %d", tempName, &tempAge);
    strcpy(temp->name, tempName);
    temp->age = tempAge;
    temp->next = 0;
    tempPos = header;
    for (i = 1; i < pos; i++)
        tempPos = tempPos->next;
    if (pos) {
        temp->next = tempPos->next;
        tempPos->next = temp;
    } else {
        temp->next = header;
        header = temp;
    }
}

void delchain(void)
{
    int pos, i;
    struct element *tempPos;
    struct element *tempFro;
    printf("请问要删除第几个?");
    scanf("%d", &pos);
    tempPos = header;
    tempFro = header;
    if (!(pos == 1)) {
        for (i = 1; i < (pos - 1); i++) {
            tempFro = tempFro->next;
        }
        tempPos = tempFro->next;
        tempFro->next = tempFro->next->next;
    } else {
        header = header->next;
    }
    free(tempPos);
}

void displayTwo(void)
{
    int i = 0;
    temp = tail;
    if (!temp) {
        printf("没有员工\n");
        return;
    }
    printf("所有员工信息如下:\n");
    while (temp) {
        printf
            ("第%d个员工 地址是:%u 姓名:%s(%u) 年龄:%d(%u)  上一个员工的地址:%d\n",
             i + 1, temp, temp->name, temp->name, temp->age, &(temp->age),
             temp->prev);
        temp = temp->prev;
        i++;
    }
    printf("共有%d个员工\n", i);
}

void viewone(void)
{
        int x, i;
        struct element * p;
        printf("请输入要查看的数据的编号:");
        scanf("%d", &x);
        p = header;
        for(i = 1; i < x; i++) p = p->next;
        printf("元素地址:%u 姓名:%s 年龄:%d\n", p, p->name, p->age);
}

--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/

没有评论:

发表评论