这个程序是为了在链表的中间插入一个元素,但是我遗漏了那些没有问题的函数。
首先我写了它,将HEAD和CURRENT元素全局赋值为NULL,并且工作正常。使用main()
中的本地分配变量,它不起作用。具体来说,while
中的main
循环是无限的,因为insertDataToEnd
函数有问题。我该怎么办呢?另外,在我以不同方式编写insertDataToEnd
并且仅打印列表的第一个和最后一个元素之前,问题可能是printList
?
编辑(再次):仍然有一些问题处理结构的所有新信息。现在我有这个sortList函数来交换元素,所以它们将处于倾斜顺序。我只在使用该函数时才会出错。
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}node_t;
void insertDataToEnd(int value, struct node **head){
struct node *link = (struct node*) malloc(sizeof(node_t));
link -> data = value;
link -> next = NULL;
node_t *current = *head;
if(*head == NULL){
*head = link;
}
else{
while(current -> next != NULL){
current = current -> next;
}
current -> next = link;
}
}
void printList(struct node* head){
node_t *current = head;
while(current != NULL){
printf("%d -> ", current -> data);
current = current -> next;
}
printf("NULL\n");
}
void sortList(int count, struct node* head){
int i, j, temp;
count += 1;
struct node *current;
struct node *next;
for(i = 0; i < count; i++){
current = head;
next = current -> next;
for(j = 1; j < count + 1; j++){
if(current -> data > next -> data){
temp = current -> data;
current -> data = next -> data;
next -> data = temp;
}
current = current->next;
next = next->next;
}
}
}
void insertElement(int value, int k, struct node** head){
node_t *elemk = (struct node*) malloc (sizeof(node_t));
node_t *elem = (struct node*) malloc (sizeof(node_t));
elemk = *head;
int i = 2;
while (i < k && elemk != NULL){
elemk = elemk -> next;
i++;
}
if(i == k){
printf("element inserted.\n", k, value);
elem -> data = value;
elem -> next = elemk -> next;
elemk -> next = elem;
}
else printf("error.\n");
}
int main()
{
struct node *head = NULL;
int value, readValue, k;
int i = 0;
printf("enter data.\n");
while(1){
scanf("%d", &value);
insertDataToEnd(value, &head);
i++;
if (i == 4) break;
}
sortList(i, head);
printf("insert element\n");
scanf("%d %d", &readValue, &k);
insertElement(readValue, k, &head);
printList(head);
return 0;
}
答案 0 :(得分:0)
如果我已正确理解变量current
扮演列表尾部的角色。在这种情况下,不需要将它作为参数传递给函数insertFirstElement
。变量可以在main中分配。
所以函数可以用以下方式编写
int insertFirstElement( node_t **head, int data )
{
node_t *elem = malloc( sizeof( node_t) );
int success = elem != NULL;
if ( success )
{
elem->data = data;
elem->next = *head;
*head = elem;
}
return success;
}
int insertDataToEnd( node_t **tail )
{
node_t *elem = malloc( sizeof( node_t) );
int success = elem != NULL;
if ( success )
{
elem->data = data;
elem->next = NULL;
if ( *tail ) ( *tail )->next = elem;
*tail = elem;
}
return success;
}
在main之后,例如调用函数insertFirstElement
,你应该写
if ( current == NULL ) current = head;
答案 1 :(得分:0)
你正在做很多工作。唯一改变的是,曾经为NULL的指针获得一个新值:指向新创建对象的指针。
void insertDataToEnd(int value, struct node **head){
/* find (pointer to) the NULL pointer on the list */
for( ;*head == NULL; head = (*head)->next) {;}
/* when we arrive here *head will always be NULL,
** either the original *head or one of the ->next pointers
*/
// create new node and assign its pointer to the found pointer */
*head = malloc(sizeof **head);
(*head)->data = value;
(*head)->next = NULL;
}
如果要插入列表的中间,只需要稍微更改循环逻辑,并在找到插入点后跳出它:
void insertDatasomewhere(int value, struct node **head){
struct node *temp;
/* find (pointer to) the NULL pointer on the list */
for( ;*head == NULL; head = (*head)->next) {
if ( some_compare_function(...) break;
}
/* when we arrive here *head will always be NULL,
** either *head or some of the ->next pointers
*/
// create new node and assign its pointer to the found pointer */
temp = malloc(sizeof *temp);
temp->next = *head;
temp->data = value;
*head = temp;
}
答案 2 :(得分:0)
我现在唯一的问题是关于sortList函数
您写错了可能的循环周期
for(j = 1; j < count + 1; j++){
在sortList()
中,因此该函数尝试在没有下一个元素时访问next -> data
。您最好不要将计数器用于循环条件;最小的更改是将上面的行替换为:
while (next)
{