求助 C言於linkedlist的程
麻大大忙
我在是做不出
可以50
按1增加
按2可除所的
按3可看之前所有的
按ESC束
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h "
int main(int argc, char *argv[])
{
int i;
struct Info info;
char selection;
printf( "Welcome to the storage room.\n\n ");
printf( "Please select an option that you wish. ");
printf( "\n*************************************\n ");
printf( "Press 1 to add a record.\n ");
printf( "Press 2 to delete a record.\n ");
printf( "Press 3 to view all records.\n ");
printf( "Press 'ESC ' to exit. ");
printf( "\n*************************************\n ");
printf( "\n ");
selection = getch();
switch( selection )
{
case '1 ':
if (isEmptyList())
{
printf( "The list is empty.\n ");
}
for (i=0; i <50; i++)
{
printf( "record number: ");
scanf( " %d ", &info.record);
printf( "Name of compenent type: ");
fflush(stdin);
gets(info.type);
printf( "The value of component: ");
fflush(stdin);
gets(info.value);
printf( "How many in the stock: ");
scanf( " %d ", &info.number);
insertNode(&info);
}
case 27:
printf( "Bye.\n\n ");
break;
default:
printf( "Your selection is not valid.\n ");
printf( "Please try again\n ");
printf( "\n ");
}
rewindList();
retrieveNode(&info);
printf( "record number: %d\t compenent type: %s\t component value: %s\t stock: %3d\n\n "
, info.record, info.type, info.value, info.number);
do
{
retrieveNode(&info);
printf( "record number\t%d\n ", info.record);
printf( "compenent type:\t%s\n ", info.type);
printf( "component value:\t%s\n ", info.value);
printf( "stock:\t%3d\n ", info.number);
} while (traversalList());
rewindList();
traversalList();
deleteNode();
rewindList();
do
{
retrieveNode(&info);
printf( "record number\t%d\n ", info.record);
printf( "compenent type:\t%s\n ", info.type);
printf( "component value:\t%s\n ", info.value);
printf( "stock:\t%3d\n ", info.number);
} while (traversalList());
while(traversalList());
deleteNode();
rewindList();
do
{
retrieveNode(&info);
printf( "record number\t%d\n ", info.record);
printf( "compenent type:\t%s\n ", info.type);
printf( "component value:\t%s\n ", info.value);
printf( "stock:\t%3d\n ", info.number);
} while (traversalList());
system( "PAUSE ");
return 0;
}
以下是linkedlist的程(是正的)
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h "
void insert();
void create();
void assign(struct Info *info);
struct Node *head=NULL, *current=NULL, *temp=NULL;
int isEmptyList()
{
return (head==NULL);
}
void rewindList()
{
current = head;
}
int traversalList()
{
if (current==NULL) return 0;
if (current-> link==NULL) return 0;
current = current-> link;
return 1;
}
void insert()
{
if (head == NULL)
{
head = temp;
current = head;
}
else
{
temp-> link = current-> link;
current-> link = temp;
current = current-> link;
}
}
void deleteNode()
{
if (head==NULL) return;
if (current==head)
{
head = current-> link;
free(current);
current = head;
}
else
{
temp = head;
while(1)
{
if (temp-> link==current) break;
temp = temp-> link;
}
temp-> link = current-> link;
free(current);
current = temp-> link;
if (current==NULL) current = head;
}
}
void create()
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp-> link = NULL;
}
void assign(struct Info *info)
{
if (temp==NULL) return;
memcpy(&temp-> info,info,sizeof(struct Info));
}
void retrieveNode(struct Info *info)
{
if (head==NULL || current==NULL) return;
memcpy(info,¤t-> info,sizeof(struct Info));
}
void insertNode(struct Info *info)
{
create();
assign(info);
insert();
}
[解决办法]
deleteNode(); 的调用非常不合理 ...
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h "
int main(int argc, char *argv[])
{
int i;
struct Info info;
char selection;
printf( "Welcome to the storage room.\n\n ");
printf( "Please select an option that you wish. ");
printf( "\n*************************************\n ");
printf( "Press 1 to add a record.\n ");
printf( "Press 2 to delete a record.\n ");
printf( "Press 3 to view all records.\n ");
printf( "Press 'ESC ' to exit. ");
printf( "\n*************************************\n ");
printf( "\n ");
selection = getch();
switch( selection )
{
case '1 ':
if (isEmptyList())
{
printf( "The list is empty.\n ");
}
for (i=0; i <50; i++)
{
printf( "record number: ");
scanf( " %d ", &info.record);
printf( "Name of compenent type: ");
fflush(stdin);
gets(info.type);
printf( "The value of component: ");
fflush(stdin);
gets(info.value);
printf( "How many in the stock: ");
scanf( " %d ", &info.number);
insertNode(&info);
}
case 27:
printf( "Bye.\n\n ");
break;
case '2 ': //调用 deleteNode() 释放节点,current 赋值为节点指针
//while 查找要删除的记录, 找到后调用 deleteNode()
while(traversalList()-> value != ??); //?? 是你要删除的记录的 value,或者可以是其他成员
deleteNode();
break;
case '3 ':
rewindList();
do
{
retrieveNode(&info);
printf( "record number\t%d\n ", info.record);
printf( "compenent type:\t%s\n ", info.type);
printf( "component value:\t%s\n ", info.value);
printf( "stock:\t%3d\n ", info.number);
} while (traversalList());
break;
default:
printf( "Your selection is not valid.\n ");
printf( "Please try again\n ");
printf( "\n ");
break;
}
system( "PAUSE ");
return 0;
}