c - 我怎样才能将其转换为双向链表?

标签 c singly-linked-list doubly-linked-list

这是我最近为单链表编写的代码。如何将其转换为双向链表?这可能吗?最近刚开始做链表,但是老师讲的还不够清晰。

int delnum,id;


#define CLR system("cls")
#define INFO_SIZE 50

void view();

struct info
{
int cusnum;
char cusname[INFO_SIZE];
char cusdes[INFO_SIZE];
struct info *next;
struct info *prev;  
}*addcase,*temp,*tempdisplay, *start,*last;



void menu()
{
    void addToStart();
    void addToEnd();
    void printList();
    void removeNodeAt();

    int choice;
    bool valid = true;

    CLR;

    printf("\n\t Basic Linked list menu");
    printf("\n\t 1.Add a new node to the end");
    printf("\n\t 2.Add a new node to the beginning");
    printf("\n\t 3.Print out the entire list");
    printf("\n\t 4.Remove a node from the list");
    printf("\n\t 5.Back to main menu");
    printf("\n\t 6.Quit the program\n\n");

    do
    {
        printf ("\n\t Enter your choice: ");
        scanf("%d",&choice);fflush (stdin);

        switch (choice)
        {
            case 1:
                addToEnd();
                break;
            case 2:
                addToStart();
                break;
            case 3:
                printList ();
                break;
            case 4:
                removeNodeAt ();
                break;
            case 5:
                main();
                break;
            case 6:
                exit(0);
            default :
                printf("\n\t Invalid Input \n\n");
                valid = false;
                break;
        }
    }while(!valid);

}

void addToStart()
{
    addcase=(struct info*) malloc(sizeof(struct info));
    char buffer[INFO_SIZE];
    bool isInt(char[]);
    bool isValidName(char[]);

    CLR;
    printf("\n\tAdd from begining\n");
    printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
    gets(buffer);fflush(stdin);

    while(!isValidName(buffer))
    {
        printf("\n\t Invalid Input \n");
        printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
        gets(buffer);fflush(stdin);
    }

    sscanf(buffer,"%s", &addcase->cusname);

    printf("\n\t What is your customer number: ");
    gets(buffer);fflush(stdin);

    while(!isInt(buffer))
    {
        printf("\n\t Invalid Input \n");
        printf("\n\t What is your customer number: ");
        gets(buffer);fflush(stdin);
    }

    sscanf(buffer," %d",&addcase->cusnum);

    printf("\n\t What is your description: ");
    gets(buffer);fflush(stdin);

    while(!isValidName(buffer))
    {
        printf("\n\t Invalid Input\n");
        printf("\n\t What is your description: ");
        gets(buffer);fflush(stdin);
    }

    sscanf(buffer,"%s", &addcase->cusdes);

    addcase->next= NULL;

    if(start== NULL)
    {
        start=addcase;
    }
    else
    {
        addcase->next =start;
        start= addcase;
    }
    printf("\n\n\tRecord was successfully saved !!!\n");
    printf("\n\t");
    system("pause");
    menu();
}


void addToEnd()
{
    addcase =(struct info*) malloc(sizeof(struct info));
    char buffer[INFO_SIZE];
    bool isInt(char[]);
    bool isValidName(char[]);


        CLR;
        printf("\n\tAdd from end\n");
        printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
        gets(buffer);fflush(stdin);

        while(!isValidName(buffer))
        {
            printf("\n\t Invalid Input\n");
            printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
            gets(buffer);fflush(stdin);
        }

        sscanf(buffer,"%s",&addcase->cusname);

        printf("\n\t What is your customer number: ");
        gets(buffer);fflush(stdin);

        while(!isInt(buffer))
        {
            printf("\n\t Invalid Input \n");
            printf("\n\t What is your customer number: ");
            gets(buffer);fflush(stdin);
        }

        sscanf(buffer," %d",&addcase->cusnum);

        printf("\n\t What is your description: ");
        gets(buffer);fflush(stdin);

        while(!isValidName(buffer))
        {
            printf("\n\t Invalid input \n");
            printf("\n\t What is your description: ");
            gets(buffer);fflush(stdin);
        }

        sscanf(buffer,"%s",&addcase->cusdes);

        addcase->next=NULL;

        if(start== NULL){
            start=addcase;
        }else {
            temp=start;
            while(temp->next!= NULL){
                temp= temp->next;
            }
            temp->next=addcase;
        }
    printf("\n\n\tRecord was successfully saved !!!\n");
    printf("\n\t");
    system("pause");
    menu();
}

void printList()
{
    CLR;
    printf("\n\tRecord List");
    if(start==NULL)
        printf("\n\n\t No record available");
    else
    {
    tempdisplay=start;
    while (tempdisplay != NULL)
    {
        printf("\n\n\t %d \t%s \t%s\n",tempdisplay->cusnum,tempdisplay->cusname,tempdisplay->cusdes);
        tempdisplay=tempdisplay->next;

    }

}printf("\n\t");
    system("pause");
    menu();
}

void removeNodeAt()
{
    char buffer[INFO_SIZE];
    bool isInt(char[]);

    CLR;
    if(start==NULL)
        printf("\n\n\t No record available to remove"),
        printf("\n\n\t"),
        system("pause"),
    menu();

    else
    {
    tempdisplay=start;
    printf("\n\tDeletation\n");
        printf("\n\t Number\t Name\t Desscription\n");
    while (tempdisplay != NULL)
    {

        printf("\n\t %d\t%s\t\t%s",tempdisplay->cusnum,tempdisplay->cusname,tempdisplay->cusdes);
        tempdisplay=tempdisplay->next;
    }
    }
    printf("\n\n\t Input specific Customer Number to delete:");
    gets(buffer);fflush(stdin);

    while(!isInt(buffer))
    {
        printf("\n\t Invalid Input \n");
        printf("\n\n\t Input specific Customer Number to delete:");
        gets(buffer);fflush(stdin);
    }

    sscanf(buffer," %d",&delnum);




    if(delnum==start->cusnum)
        start=start->next;

    else if(&delnum!=&temp->cusnum)
    {
        printf("\n\t No record specific to this number\n");
        Sleep(1000);
        removeNodeAt();
    }
    else{

    temp=start;

    last->next=last->next->next;
    }
    printf("\n\n\t Record was successfully deleted !!!\n");
    printf("\n\t");
    system("pause");
    menu();
}

最佳答案

我很久以前就编写了这个程序,用于在双向链表中输入学生信息。请引用下面的程序并在同一行上编辑您的程序。您的程序只是创建一个单链表,并提供从列表中添加、删除元素和打印元素的选项。

 #include<stdio.h>
 #include<malloc.h>
 #include<string.h>

 struct student
 {
    char name[10];
    int m1;
    int m2;
    struct student *llink;
    struct student *rlink;
 };

 struct student * addelement(struct student *,char name[],int,int,int,int);
 void display( struct student *,char name[]);

 int main()
 {

    int num,i,m1,m2;
    struct student *head;
    head=NULL;
    char name[10];
    printf("\n Enter the number of students data to be entered  :");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
            printf("Enter the name of the student :");
            scanf("%s",name);
            printf("\n Enter the marks in maths  :");
            scanf("%d",&m1);
            printf("\n Enter the marks in science : ");
            scanf("%d",&m2);
            head=addelement(head,name,m1,m2,i,num);
    }

    printf("\n Enter the student name to be searched : ");
    scanf("%s",name);
    display(head,name);

}

 struct student * addelement(struct student *head,char name[10],int m1,int m2,int i,int num){
            struct student *newnode;

            newnode=(struct student *)malloc(sizeof(struct student));

            strcpy(newnode->name,name);
            newnode->m1=m1;
            newnode->m2=m2;

            if(i==num)
            {
                    newnode->llink=NULL;
                    newnode->rlink=head;
                    head->llink=newnode;
                    return newnode;
            }



            else if(head==NULL)

            {

                    newnode->rlink=NULL;
                    newnode->llink=NULL;
                    return newnode;

            }
            else

            {
                    head->llink=newnode;
                    newnode->rlink=head;
                    return newnode;
            }
}

      void display(struct student *p3,char name[10])

     {
              struct student *temp;
              temp=p3;
              int flag=0;

            if(p3==NULL)
            {

                    printf("\n The list is empty \n");

            }

            while(p3!=NULL)

            {
                   printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);

                    if(p3->m1>=90 && p3->m2>=90)
                    {
                            printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);
                            p3=p3->rlink;
                    }
                    else
                    {

                    p3=p3->rlink;

                    }


            }

            p3=temp;

            while(p3!=NULL)
            {
                    if(strcmp(p3->name,name)==0)
                    {
                            flag=1;
                            break;
                    }

                    else
                    {
                            p3=p3->rlink;
                    }
            }

            if(flag==1)
            {

                    printf("\n The student information exists in the database \n ");
            }
            else
            {
                    printf("\n The student information does not exist in the database \n");
            }
   }

关于c - 我怎样才能将其转换为双向链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921166/

相关文章:

c++ - 循环链表

c++ - C/C++ 中的数值转换

将 HTML 页面转换为十六进制值数组以在 Arduino 服务器中使用

c - 链接列表反向递归功能不起作用

java - 如何迭代双链表并创建一个具有特定值的新列表?

c++ - 如何删除双向链表数据并返回?

c - 反转链表的每 k 个节点

c - 如何在编译 clang 时包含 GLUT

java - 为什么我的方法不应该打印出列表的最后一个元素?

java - 在方法中重新分配引用是如何工作的?