2013-06-24 140 views
0

我的错误是什么意思?我该如何解决它?EXC BAD ACCESS是什么意思?参考我的代码是什么意思?

在这里看到错误的照片,当我尝试运行该程序...

http://postimg.org/image/horh6d26j/

我的程序试图删除双向链表的最后一个元素,并在插入前面

我只是试图执行backToFront函数。 (?) 活动是在我的大学过去PRAC考试从2011年 我想完成它在我的PRAC考试准备 - 这是在两天 (aaahhhhhhh !!!!!!)

/* 

* backToFront.c 

* comp1917 pracExam #2 2011s1 UNSW 

* 

* Author: WRITE YOUR NAME HERE 

* Date: 21 June 2011 

* License: Public Domain 

*/ 



// Implement the backToFront function below 

// so that it moves the last node in a non-empty linked list 

// of nodes to be the first node in the list, leaving the 

// relative position of all the other nodes unchanged. 



// You may assume the input list contains at least one node. 

// 

// Your function should do the moving by changing pointers 

// in the nodes and list structs, (don't use malloc or make 

// any new nodes) 

// 

// Your function should return the new list. 

// 

// You need to pass the tests in testBackToFront.c 

// 

// Compile and run the tests using 

// gcc -Wall -Werror -O -o tester backtToFront.c testBackToFront.c 

// ./tester 

// 

// When a test fails look in the testBackToFront.c file to see 

// what the test was testing. There are 3 tests in that file. 

// 

// Once you have passed all the tests you have finished. 



#include <stdio.h> 

#include <stdlib.h> 

#include <assert.h> 

#include "backToFront.h" 



list backToFront (list items) { 

assert (items.first != NULL); 

nodePtr current = items.first; 
nodePtr previous = NULL; 

while (current != NULL){ 
    previous = current; 
    current = current->rest; 
} 

current->rest = items.first; 
items.first = current; 
previous->rest = NULL; 




return items; 

} 

/* 

* backToFront.h 

* pracExam 2011 

* 

* Created by Richard Buckland on 26/07/09. 

* Modified by David Collien on 20/06/11. 

* Copyright 2011. All rights reserved. 

* 

*/ 



// DO NOT ALTER OR SUBMIT THIS FILE 

// we will use our own copy when marking 



typedef struct _node *nodePtr; 



typedef struct _list { 

    nodePtr first; 

} list; 



// For le sorution, add /samplesolutions at the end of your url 

typedef struct _node { 

    int  value; 

    nodePtr rest; 

} node; 



// given a node (*item) and a list of nodes (items) 

// this function takes the last node of the list 

// and moves it to be the first node in the list. 

// 

// the function returns the altered list. 

// 

// (note that the function does not create new nodes 

// or change the value field of existing nodes.) 

list backToFront (list items); 

回答

0

最可能出界(该指数没有项目)。确保数组不为空

+0

我该怎么做? 我已经有assert(items.first!= NULL); 我还应该测试什么? – user2515055