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