2013-07-29 23 views
-1

這是我最近爲單向鏈表編寫的代碼。我如何將它轉換爲雙向鏈表?這甚至有可能嗎?我剛剛開始做鏈表,但我們的講師教的內容還不夠清楚。我如何能夠將其轉換爲雙向鏈表?

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(); 
} 
+0

線索在結構中。分類使用prev。 – Joe

+0

你能告訴我任何函數的部分代碼嗎? – user2629792

回答

0

我已經編寫了這個程序,用於在雙向鏈接列表中輸入學生信息。參考下面的程序並在同一行編輯您的程序。您的程序只創建一個單獨鏈接列表,並提供添加,刪除元素和列表中元素的選項。

#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"); 
      } 
    } 
+0

好的,我會嘗試這種方法 – user2629792