这是我最近为单链表编写的代码。如何将其转换为双向链表?这甚至可能吗?我最近才开始做链表,但是我们的讲师教的还不够清楚。
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();
}