J
Jeff Cheng
Can anybody help to trace the runtime error for the following code
about sub-Linked list.
Thank you,
# include <stdio.h>
# include <stdlib.h>
typedef char *CategoryType;
typedef char *ItemType;
typedef char *CodeType;
typedef int QtyType;
typedef struct node_item {
ItemType ItemDesc;
CodeType CodeNo;
QtyType Quantity;
node_item *NextItemLink;
} items;
typedef struct node_cat {
node_item *ItemLink;
CategoryType CatData;
node_cat *CatLink;
} category;
typedef category *CatPtr;
//typedef items *ItemLink;
typedef items *ItemLink;
void Initialise (CatPtr *L) {
*L = NULL;
}
// this is to add category
void AddCatNode (CatPtr *L, CategoryType x) {
CatPtr p, q;
p=(CatPtr) malloc (sizeof(category));
p->CatData = x;
p->CatLink = NULL;
if (*L == NULL)
*L = p;
else {
q = *L;
while (q->CatLink!=NULL)
q=q->CatLink;
q->CatLink = p;
}
}
// this is to add item
void AddItemNode (ItemLink *L, ItemType x, CodeType y, QtyType z) {
ItemLink p, q;
p=(ItemLink) malloc (sizeof(items));
p->ItemDesc = x;
p->CodeNo = y;
p->Quantity = z;
p->NextItemLink = NULL;
if (*L == NULL)
*L = p;
else {
q = *L;
//while (q->NextItemLink!=NULL)
// q=q->NextItemLink;
//q->NextItemLink = p;
}
}
//cat search
CatPtr SearchCatList (CatPtr L, CategoryType x) {
CatPtr p;
p = L;
while (p!=NULL) {
if (x == p->CatData)
return p;
else
p=p->CatLink;
}
return p;
}
//item search
ItemLink SearchItemList (ItemLink L, ItemType x) {
ItemLink p;
p = L;
while (p!=NULL) {
if (x == p->CodeNo)
return p;
else
p=p->NextItemLink;
}
return p;
}
//item display
void DisplayItemList (ItemLink L) {
ItemLink p;
for (p=L; p!=NULL; p=p->NextItemLink) {
printf("%s\n", p->ItemDesc);
printf("%s\n", p->CodeNo);
printf("%d\n", p->Quantity);
}
}
void main() {
CatPtr CatList,p;
ItemLink ItemList, i;
int choice, z;
char yn;
char *x, *y;
Initialise (&CatList);
do {
printf ("Main Menu\n");
printf ("1. Add Record\n");
printf ("2. Search\n");
printf ("3. Print\n");
printf ("9. Exit\n");
printf("\nPlease enter your choice> ");
scanf ("%d", &choice);
switch (choice) {
case 1:
printf ("Please select what you want to add\n");
printf ("1. Add Category\n");
printf ("2. Add Item\n");
printf ("Please enter your choice> ");
scanf ("%d", &choice);
// this is to add new category
if (choice == 1) {
printf ("Please key in New Category> ");
scanf ("%s", &x);
AddCatNode (&CatList, x);
}
// this is to add new item
if (choice == 2) {
printf ("Please key in Category> ");
scanf ("%s", &x);
p= SearchCatList (CatList, x);
if (p==NULL)
printf ("Category not found\n");
else {
printf ("Please enter the New Item> ");
scanf ("%s", &x);
printf ("Please enter the New Code> ");
scanf ("%s", &y);
printf ("Please enter the Quantity> ");
scanf ("%d", &z);
AddItemNode (&ItemList, x, y ,z);
}
}
break;
case 2:
printf ("Please Enter Category> ");
scanf("%s", &x);
p = SearchCatList (CatList, x);
printf ("Please Enter Code No> ");
scanf("%s", &y);
i = SearchItemList (ItemList, y);
if (i==NULL)
printf ("No such item");
else {
printf("%s", i->ItemDesc);
printf("%s", i->CodeNo);
printf("%d", i->Quantity);
}
break;
case 3:
printf ("Please enter your Category> ");
scanf ("%s", &x);
DisplayItemList (ItemList);
break;
//int tem;
//tem = 1;
//while (tem < 100000) {
// tem++;
printf("%s", &x);
//}
}
}while (choice !=9);
}
about sub-Linked list.
Thank you,
# include <stdio.h>
# include <stdlib.h>
typedef char *CategoryType;
typedef char *ItemType;
typedef char *CodeType;
typedef int QtyType;
typedef struct node_item {
ItemType ItemDesc;
CodeType CodeNo;
QtyType Quantity;
node_item *NextItemLink;
} items;
typedef struct node_cat {
node_item *ItemLink;
CategoryType CatData;
node_cat *CatLink;
} category;
typedef category *CatPtr;
//typedef items *ItemLink;
typedef items *ItemLink;
void Initialise (CatPtr *L) {
*L = NULL;
}
// this is to add category
void AddCatNode (CatPtr *L, CategoryType x) {
CatPtr p, q;
p=(CatPtr) malloc (sizeof(category));
p->CatData = x;
p->CatLink = NULL;
if (*L == NULL)
*L = p;
else {
q = *L;
while (q->CatLink!=NULL)
q=q->CatLink;
q->CatLink = p;
}
}
// this is to add item
void AddItemNode (ItemLink *L, ItemType x, CodeType y, QtyType z) {
ItemLink p, q;
p=(ItemLink) malloc (sizeof(items));
p->ItemDesc = x;
p->CodeNo = y;
p->Quantity = z;
p->NextItemLink = NULL;
if (*L == NULL)
*L = p;
else {
q = *L;
//while (q->NextItemLink!=NULL)
// q=q->NextItemLink;
//q->NextItemLink = p;
}
}
//cat search
CatPtr SearchCatList (CatPtr L, CategoryType x) {
CatPtr p;
p = L;
while (p!=NULL) {
if (x == p->CatData)
return p;
else
p=p->CatLink;
}
return p;
}
//item search
ItemLink SearchItemList (ItemLink L, ItemType x) {
ItemLink p;
p = L;
while (p!=NULL) {
if (x == p->CodeNo)
return p;
else
p=p->NextItemLink;
}
return p;
}
//item display
void DisplayItemList (ItemLink L) {
ItemLink p;
for (p=L; p!=NULL; p=p->NextItemLink) {
printf("%s\n", p->ItemDesc);
printf("%s\n", p->CodeNo);
printf("%d\n", p->Quantity);
}
}
void main() {
CatPtr CatList,p;
ItemLink ItemList, i;
int choice, z;
char yn;
char *x, *y;
Initialise (&CatList);
do {
printf ("Main Menu\n");
printf ("1. Add Record\n");
printf ("2. Search\n");
printf ("3. Print\n");
printf ("9. Exit\n");
printf("\nPlease enter your choice> ");
scanf ("%d", &choice);
switch (choice) {
case 1:
printf ("Please select what you want to add\n");
printf ("1. Add Category\n");
printf ("2. Add Item\n");
printf ("Please enter your choice> ");
scanf ("%d", &choice);
// this is to add new category
if (choice == 1) {
printf ("Please key in New Category> ");
scanf ("%s", &x);
AddCatNode (&CatList, x);
}
// this is to add new item
if (choice == 2) {
printf ("Please key in Category> ");
scanf ("%s", &x);
p= SearchCatList (CatList, x);
if (p==NULL)
printf ("Category not found\n");
else {
printf ("Please enter the New Item> ");
scanf ("%s", &x);
printf ("Please enter the New Code> ");
scanf ("%s", &y);
printf ("Please enter the Quantity> ");
scanf ("%d", &z);
AddItemNode (&ItemList, x, y ,z);
}
}
break;
case 2:
printf ("Please Enter Category> ");
scanf("%s", &x);
p = SearchCatList (CatList, x);
printf ("Please Enter Code No> ");
scanf("%s", &y);
i = SearchItemList (ItemList, y);
if (i==NULL)
printf ("No such item");
else {
printf("%s", i->ItemDesc);
printf("%s", i->CodeNo);
printf("%d", i->Quantity);
}
break;
case 3:
printf ("Please enter your Category> ");
scanf ("%s", &x);
DisplayItemList (ItemList);
break;
//int tem;
//tem = 1;
//while (tem < 100000) {
// tem++;
printf("%s", &x);
//}
}
}while (choice !=9);
}