这是一个好的concatList
方法吗?这对我来说似乎没问题,但当我测试它时,Microsoft Visual Studio说Unable to start the program. Access denied.
我想看看它是否是方法本身,如果不是,我会减少为什么我的主程序只在该方法未测试时运行的可能性。
List concatList(List A, List B) {
if (A == NULL || B == NULL) {
printf("Error: List ADT: Attempted to call concatList(List A, List B) when a List argument is NULLn");
exit(1);
} else {
List concatedList = newList; //create the new List which we will return
int lengthh = 0;
Node As = A->front;
while (As != NULL) { //go through and append everything in List A to the new List
append(concatedList, As->data);
As = As->next;
lengthh++;
}
Node Bs = B->front;
while (Bs != NULL) { //go through and append everything in List B to the new List
append(concatedList, Bs->data);
Bs = Bs->next;
lengthh++;
}
concatedList->front = A->front;
concatedList->back = B->back;
concatedList->length = lengthh;
return concatedList; //return the new, concatenated, List
}
}
我的清单的重要部分。c:
typedef struct nodeObj {
int data;
struct nodeObj *prev;
struct nodeObj *next;
} nodeObj;
//private Node type
typedef nodeObj *Node;
//private ListObj tyoe
typedef struct ListObj {
Node front;
Node back;
Node cursor;
int length;
int index;
} ListObj;
List newList(void) {
List Z;
Z = malloc(sizeof(ListObj));
Z->front = Z->back = NULL;
Z->length = 0;
Z->index = -1;
Z->cursor = NULL;
return (Z);
}
int front(List L) {
// If list is NULL, throw error
if (L == NULL) {
printf("List Error: calling front() on NULL List referencen");
exit(1);
}
// If no elements in L, throw error
if (length(L) <= 0) {
printf("List Error: front() called on empty Listn");
}
// Else return head element of L
return L->front->data;
}
void append(List L, int data) {
Node N = newNode(data);
// If list is NULL, throw error
//if (L == NULL) {
// printf("List Error: calling append() on a NULL List referencen");
// exit(1);
//}
// If tail of list L is NULL, set front and back of list L to N, then set cursor to back of L
if (L->back == NULL) {
L->front = L->back = N;
L->cursor = L->back;
} else {
// If List L is not empty, then
// Set next value of list L to N
L->back->next = N;
// Set the prior value of N to the tail of L
N->prev = L->back;
// Set tail of L to Node N
L->back = N;
// Remove value from N
N->next = NULL;
}
// Increment the length of L
L->length++;
}
这里似乎有一个错误:
List concatedList = newList;
我希望它是:
List concatedList = newList();
您遇到的问题可能与代码无关,而是与如何调用程序无关。
同时,您的concatList
函数存在一些问题:
-
应该通过调用
NewList
构造函数来分配新列表:List concatedList = newList();
-
不需要手动计算长度,
append
已经执行了必要的记账。 -
更新
front
和back
指针是不正确的。您实际上损坏了concatedList
对象。根本不需要更新concatedList
中的字段。
这是一个更正的版本:
List concatList(List A, List B) {
if (A == NULL || B == NULL) {
printf("Error: List ADT: Attempted to call concatList(List A, List B) when a List argument is NULLn");
exit(1);
} else {
// create the new List which we will return
List concatedList = newList();
// go through and append everything in List A to the new List
Node As = A->front;
while (As != NULL) {
append(concatedList, As->data);
As = As->next;
}
// go through and append everything in List B to the new List
Node Bs = B->front;
while (Bs != NULL) {
append(concatedList, Bs->data);
Bs = Bs->next;
}
// return the new, concatenated, List
return concatedList;
}
}
还要注意以下备注:
List
未在发布的源代码中定义。我假设定义为typedef listObj *List;
- 将指针隐藏在
typedef
s后面是令人困惑和容易出错的 - 您不测试潜在的
malloc()
故障