自己实现C语言双向向链表

来源:互联网 发布:dota2解除小黑屋软件 编辑:程序博客网 时间:2024/06/11 23:24
#include<iostream>#include<stdio.h>#include<string.h>#include<malloc.h>#include<list>#define CHAR char#define VARTYPE CHAR //默认存放char类型using namespace std;struct myNode;typedef struct myNode Node;typedef Node* List;typedef Node* PtrToNode;struct myNode{    VARTYPE data;    PtrToNode next;    PtrToNode prev;};//在p位置后插入元素void _insert(List T,int p,VARTYPE x);//头插入元素void _push_front(List T,VARTYPE x);//尾部插入元素void _push_back(List T,VARTYPE x);//移除内容为x的元素void _removec(List T,VARTYPE x);//移除内容为编号为p的元素void _removep(List T,int p);//删除链表void _dellist(List T);//打印char类型链表内容#ifdef CHARvoid _print(List T);#endif//清空链表void _clear(List T);//创建链表List createlist();//返回编号为p的元素VARTYPE _findc(List T,int p);//链表元素个数int _size(List T);int main(){    List T = createlist();    _push_back(T,'a');    _push_back(T,'b');    _push_back(T,'c');    _push_back(T,'d');    _push_back(T,'e');    _push_back(T,'e');    _push_back(T,'d');    _push_front(T,'x');    //_insert(T,2,'p');    //_removec(T,'e');    //_removep(T,3);    //_print(T);    //_clear(T);    _push_back(T,'p');    _push_back(T,'q');    //printf("%d",_size(T));    //printf("%c",_findc(T,3));    _print(T);    _dellist(T);    return 0;}//在p位置后插入元素void _insert(List T,int p,VARTYPE x){    PtrToNode tmp;    int i = 0;    while(NULL!=T)    {        T = T->next;        if(++i==p)            break;    }    tmp = (PtrToNode)malloc(sizeof(Node));    if(NULL==tmp)        perror("malloc");    else    {        tmp->data = x;        tmp->next = T;        tmp->prev = T->prev;        T->prev->next = tmp;        T->prev = tmp;    }};//尾部插入元素void _push_back(List T,VARTYPE x){    while(NULL!=T->next)        T = T->next;    PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));    if(NULL==tmp)        perror("malloc");    else    {        tmp->data = x;        tmp->next = NULL;        tmp->prev = T;        T->next = tmp;    }};//头部插入元素void _push_front(List T,VARTYPE x){    PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));    if(NULL==tmp)        perror("malloc");    else    {        tmp->data = x;        tmp->prev = T;        tmp->next = T->next;        T->next = tmp;        tmp->next->prev = tmp;    }};//移除内容为x的元素void _removec(List T,VARTYPE x){    T = T->next;    while(NULL!=T)    {        if(T->data==x)        {            T = T->prev;            T->next = T->next->next;            free(T->next->prev);            T->next->prev = T;        }        T = T->next;    }};//移除内容为编号为p的元素void _removep(List T,int p){    int i = 0;    while(NULL!=T)    {        T = T->next;        if(++i==p)            break;    }    T->prev->next = T->next;    T->next->prev = T->prev;    free(T);};//删除链表void _dellist(List T){    PtrToNode tmp = T;    while(NULL!=T)    {        tmp = T->next;        free(T);        T = tmp;    }};//打印char类型链表内容#ifdef CHARvoid _print(List T){    T = T->next;    while(NULL!=T)    {        printf("%c ",T->data);        T=T->next;    }};#endif//清空链表void _clear(List T){    _dellist(*&T->next);    T->next = NULL;    T->prev = NULL;}//创建链表List createlist(){    PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));    tmp->next = NULL;    tmp->prev = NULL;    return tmp;}//返回编号为p的元素VARTYPE _findc(List T,int p){    int i = 0;    while(NULL!=T)    {        T = T->next;        if(++i==p)            break;    }    return T->data;}//链表元素个数int _size(List T){    int i = -1;    while(NULL!=T)    {        T = T->next;        ++i;    }    return i;}


2014/10/15更新

#include<iostream>#include<cstdio>#include<string>#include<malloc.h>using namespace std;struct mynode;typedef struct mynode Node;struct mynode{    int data;    Node *next;    Node *prev;};char str[] = {"\-------------------------------------------------\n\-------C:创建链表-------------------------------\n\-------D:删除节点-------------------------------\n\-------I:插入新节点-----------------------------\n\-------P:输出节点-------------------------------\n\-------Q:撤销链表-------------------------------\n\-------E:退出-----------------------------------\n\-------------------------------------------------\n\"             };Node* createlist(){    Node* head,*temp,*p;    int n;    head = (Node *)malloc(sizeof(Node));    if(NULL==head)    {        perror("out of space");        exit(1);    }    head->data = -1;    head->next = NULL;    head->prev = NULL;    printf("请输入节点个数:");    scanf("%d",&n);    p = head;    for(int i=1; i<=n; ++i)    {        temp = (Node *)malloc(sizeof(Node));        if(NULL==temp)        {            perror("out of space");            exit(1);        }        temp->next = NULL;        temp->prev = p;        printf("data%2d:",i);        scanf("%d",&temp->data);        p->next = temp;        p = temp;    }    return head;}void dellist(Node *head){    Node *p;    while(head)    {        p = head->next;        free(head);        head = p;    }}void printlist(Node *head){    int i = 1;    Node *p;    head = head->next;    while(head)    {        printf("data%2d: %d\n",i++,head->data);        p = head;        head = head->next;    }    printf("反向\n");    while(p)//测试反向是正确连接    {        printf("data%2d: %d\n",--i,p->data);        p = p->prev;    }}void _insert(Node *head){    int pos;    Node* temp;    printf("输入插入节点的位置:");    scanf("%d",&pos);    if(pos<0)        return ;    while(head->next&&pos--)    {        head = head->next;    }    temp = (Node *)malloc(sizeof(Node));    if(NULL==temp)    {        perror("out of space");        exit(1);    }    printf("输入data:");    scanf("%d",&temp->data);    temp->prev = head;    temp->next = head->next;    head->next->prev = temp;    head->next = temp;}void delnode(Node *head){    int pos;    printf("输入删除节点位置:");    scanf("%d",&pos);    if(pos<1)        return ;    while(pos--)    {        head = head->next;    }    head->prev->next = head->next;    head->next->prev = head->prev;    free(head);}int main(){    char p;    printf(str);    while(scanf("%c",&p)!=EOF)    {        if(p>='a'&&p<='z')p = p-('a'-'A');        Node *_list;        switch(p)        {        case 'C':            dellist(_list);            _list = createlist();            break;        case 'D':            delnode(_list);            break;        case 'I':            _insert(_list);            break;        case 'P':            printlist(_list);            break;        case 'Q':            dellist(_list);            break;        case 'E':            dellist(_list);            return 0;            break;        default:            printf(str);            break;        }    }}


0 0