Double-stranded table (create, delete, add node, print)

Source: Internet
Author: User

#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 = 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 = 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() ----- */

 

Double-stranded table (create, delete, add node, print)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.