首页 > 代码库 > 双链表(建立、删除、添加节点、打印)

双链表(建立、删除、添加节点、打印)

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <curses.h>using namespace std;typedef struct student{    int data;    struct student *next;    struct student *pre;}dnode;dnode *creat(){    dnode *head,*p,*s;    int x,cycle=1;    head=(dnode*)malloc(sizeof(dnode));    p=head;    while(cycle)    {        printf("\nplease input the data:");        scanf("%d",&x);        if(x)        {            s=(dnode*)malloc(sizeof(dnode));            s->data =http://www.mamicode.com/ x;            printf("\n %d",s->data);            p->next = s;            s->pre = p;            p=s;        }        else cycle=0;    }    head = head->next;    head->pre = NULL;    p->next = NULL;    return head;}dnode* del(dnode *head,int num){    dnode *p1,*p2;    p1=head;    while(num!=p1->data && p1->next!=NULL)        p1 = p1->next;    if(num == p1->data)    {        if(p1 == head)        {            head = head->next;            head->pre=NULL;            free(p1);        }        else if(p1->next == NULL)        {            p1->pre->next = NULL;            free(p1);        }        else        {            p1->pre->next = p1->next;            p1->next->pre = p1->pre;            free(p1);        }    }    else        printf("\n%d could not been found",num);    return head;}dnode* insert(dnode *head,int num){    dnode *p0,*p1;    p1=head;    p0=(dnode*)malloc(sizeof(dnode));    p0->data =http://www.mamicode.com/ num;    while(p0->data>p1->data && p1->next!=NULL)     p1 = p1->next;    if(p0->data <= p1->data)    {        if(head == p1)        {            p0->next = p1;            p1->pre = p0;            head=p0;        }        else        {            p1->pre->next = p0;            p0->next = p1;            p0->pre = p1->pre;            p1->pre = p0;        }    }    else //p0->data is bigest    {        p1->next = p0;        p0->pre = p1;        p0->next = NULL;    }    return head;}int length(dnode *head) //measuring the length of list{    int n=0;    dnode *p;    p=head;    while(p)    {        p=p->next;        n++;    }    return n;}int print(dnode *head) //print the list{    dnode *p;    int n=0,i=1;    n=length(head);    printf("\nNow.These %d records are: ",n);    p=head;    if(!head) return -1;    while(p)    {        printf("\n %d   ",p->data);        p=p->next;    }}int main (int argc, char **argv){    dnode *head;    int num,n;    while(1)    {        printf("\n-----------------");        printf("\n******Menu****** |");        printf("\n0:exit           |");        printf("\n1:cerat dlist    |");        printf("\n2:delete a dnode |");        printf("\n3:insert a dnode |");        printf("\n4:print dlist    |");        printf("\n-----------------");        printf("\n please chose your number listed above(0 mease stop): ");        scanf("%d",&num);        switch(num)        {            case 0: return 0;            case 1: head=creat(); break;            case 2: printf("\nchose the number you want delete: ");                    scanf("%d",&n);                    head=del(head,n);                    break;            case 3: printf("\nchose the number you want insert: ");                    scanf("%d",&n);                    head=insert(head,n);                    break;            case 4: print(head);break;            default: printf("\nyour number is invalid,please input again");break;        }    }} /*  ----- End of main() ----- */

 

双链表(建立、删除、添加节点、打印)