c中链表删除函数修改问题

c中链表删除函数修改问题,c,linked-list,C,Linked List,我正在为考试而学习,我在调试我的链表练习试题时遇到了困难。我对他想要我们做的删除函数的不同修改有问题。具体来说,我的deleteEven是一个无休止的外观,我的deleteNthNode并没有像它应该的那样工作。因为我正在努力解决这两个问题,我甚至不知道从哪里开始删除列表中的其他元素。我非常感谢您的帮助,因为我想在转到堆栈和队列之前完全理解这个主题!:) 下面是我的代码: /* Q1: Try implementing the functions shown in class on y

我正在为考试而学习,我在调试我的链表练习试题时遇到了困难。我对他想要我们做的删除函数的不同修改有问题。具体来说,我的deleteEven是一个无休止的外观,我的deleteNthNode并没有像它应该的那样工作。因为我正在努力解决这两个问题,我甚至不知道从哪里开始删除列表中的其他元素。我非常感谢您的帮助,因为我想在转到堆栈和队列之前完全理解这个主题!:)

下面是我的代码:

    /*
Q1: 
Try implementing the functions shown in class on your own: 
    check:  node creation 
    check:  insertion at the end of a linked list,
    check:  insertion at the head of a linked list, 
    check:  a list printing function.
Q2: 
    check: Write a recursive printList() function.
Q3:
    check: Write a recursive tailInsert() function.
Q4:
    check: Write a function that inserts nodes at the beginning of the linked list.
Q5:
    check: Write a recursive function that prints a linked list in reverse order. 
           The function signature is: void printReverse(node *head);
Q6:
    check: Write an iterative destroyList() function that frees all the nodes in a linked list.
Q7:
    check: Now implement destroyList() recursively.
Q8:
    - Write a function that deletes the nth element from a linked list. 
      If the linked list doesn't even have n nodes, don't delete any of them. 
      The function signature is: node *deleteNth(node *head, int n). 
     - Try implementing the function iteratively and recursively. 
     - (In terms of how to interpret n, you can start counting your nodes from zero or one; your choice.)
Q9:
    - Write a function that deletes every other element in a linked list. 
    - (Try writing it both ways: one where it starts deleting at the head of the list, 
    - and one where it starts deleting at the element just after the head of the list.)
    - Can you write this both iteratively and recursively?
Q10:
    - Write a function that deletes all even integers from a linked list.
Q11:
    - Write a function that takes a sorted linked list and an element to be inserted into that linked list,
      and inserts the element in sorted order. 
      The function signature is: node *insertSorted(node *head, int n);
Q12:
    - One of the problems with the first insertNode() function from today is that it 
      requires us to call it using head = insertNode(head, i). 
      That's a bit dangerous, because we could forget the "head =" part very easily. 
      Re-write the function so that it takes a pointer to head, 
      thereby allowing it to directly modify the contents of head without any need for a return value. 
      The function signature is: void insertNode(node **head, int data). 
      The function will be called using insertNode(&head, i).
*/

//come back to
#include <stdio.h>
#include <stdlib.h>


// Basic linked list node struct; contains 'data' and 'next' pointer.
// What happens if we type "node *next" instead of "struct node *next"?
typedef struct node
{
    // data field
    int data;

    // the next node in the list
    struct node *next;
} node;

// Allocate a new node. Initialize its fields. Return the pointer.
// We call this from our insertion functions.
node *createNode(int data)
{
    node *ptr = NULL;
    ptr = malloc(sizeof(node));
    if(ptr == NULL)
    {
        printf("space could not be allocated\n");
        return NULL;
    }
    ptr->data = data;
    ptr->next = NULL;
    return ptr;
}

// Insert into the end of the linked list. Return the head of the linked list.
// (What is the order (Big-Oh) of this function?)

/*
node *insertNode(node *head, int data)
{    
    node *temp;
    if (head == NULL)
        return createNode(data);

    for(temp = head; temp->next != NULL; temp = temp->next)
        ;
    temp->next = createNode(data);

    return head;
}
*/

node *insertNodeFront(node *head, int data)
{
    node *temp;
    if(head == NULL)
        return createNode(data);

    temp = createNode(data);
    temp->next = head;

    return temp;
}   

// Simple function to print the contents of a linked list.
void printList(node *head)
{
    if (head == NULL)
    {
        printf("Empty List\n");
        return;
    }

    for(; head != NULL; head = head->next)
        printf("%d ", head->data);

    printf("\n");
}

void printListRecursiveHelper(node *head)
{
    if (head == NULL)
        return;

    printf("%d%c", head->data, (head->next == NULL) ? '\n' : ' ');
    printListRecursiveHelper(head->next);
}

void printListRecursive(node *head)
{
    if (head == NULL)
    {
        printf("empty list\n");
        return;
    }
    printListRecursiveHelper(head);
}
// Q3: - Write a recursive tailInsert() function.

node *tailInsert(node *head, int data)
{
    if(head->next == NULL)
    {
        node *temp;
        temp = createNode(data);
        temp->next = NULL;
        head->next = temp;
        return temp;
    }
    return tailInsert(head->next, data);
}   
//Q5: Write a recursive function that prints a linked list in reverse order. 
void printReverse(node *head)
{
    if (head == NULL)
        return;

    printReverse(head->next);

    printf("%d ", head->data);
}

// Q6: - Write an iterative destroyList() function that frees all the nodes in a linked list.
// Got code from internet, memorize it
/* Function to delete the entire linked list */
void destroyList (struct node** head)
{
   struct node* current = *head;
   struct node* next;

   while (current != NULL) 
   {
       next = current->next;
       free(current);
       current = next;
   }


   *head = NULL;
}

// Q7:  - Now implement destroyList() recursively.
// Look up online, need to examine why it deson't work
node *destroyListRecursive(node * head)
{
    if (head != NULL) 
    {
        destroyListRecursive(head->next);
        free(head);
    }

    return NULL;
}

/* Q8:
    - Write a function that deletes the nth element from a linked list. 
    - If the linked list doesn't even have n nodes, don't delete any of them. 
    - Try implementing the function iteratively and recursively. 
    - (In terms of how to interpret n, you can start counting your nodes from zero or one; your choice.)
*/

node *deleteNth(node *head, int n)
{
    /*
    int i;
    node*current;
    node *prev;

    current = head;

    while(i = 0; i < n; i++)
    {
        current = current->next;   
    }

    prev = current;
    current = head->next->next;
    */
    return head;
}

/*
Q9:
    - Write a function that deletes every other element in a linked list. 
    - (Try writing it both ways: one where it starts deleting at the head of the list, 
    - and one where it starts deleting at the element just after the head of the list.)
    - Can you write this both iteratively and recursively?
*/

/* deletes alternate nodes of a list starting with head */
//got code from http://www.geeksforgeeks.org/delete-alternate-nodes-of-a-linked-list/
void deleteAltHead(struct node *head)
{
    if (head == NULL)
        return;

    struct node *node = head->next;

    if (node == NULL)
        return;

    /* Change the next link of head */
    head->next = node->next;

    /* free memory allocated for node */
    free(node);

    /* Recursively call for the new next of head */
    deleteAltHead(head->next);
}

void deleteOtherTail()
{

}

//Q10:  - Write a function that deletes all even integers from a linked list.
void deleteEvenInts(node **head)
{

}

// Q11: - Write a function that takes a sorted linked list and an element to be inserted into that linked list,
//        and inserts the element in sorted order. 
node *insertSorted(node *head, int n)
{    
    struct node *current;
    struct node *newNode = createNode(n);

    /* Special case for the head end */
    if (head == NULL || head->data >= newNode->data)
    {
        newNode->next = head;
        head = newNode;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = head;
        while (current->next != NULL &&
               current->next->data < newNode->data)
        {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }

    return current;
    //or should it return head?
}



// Q12:  Re-write the insertNode() function so that it takes a pointer to head
void insertNode(node **head, int data)
{
    node *newNode = createNode(data);

    while (*head != NULL)
    {
        head = &(*head)->next;
    }

    newNode->next = *head;
    *head = newNode;
}

// Q10: Write a function that deletes all even integers from a linked list.
void deleteEven(node **head)
{
    node *current = *head;
    node *next = current;
    node *prev = NULL;

    for ( ; current != NULL; )
    {
        next = current->next;

        if( current->data %2)
        {
            if(prev != NULL)
            {
                prev->next = next;
                free(current);
                current = next;
            }
            else
            {
                free(current);
                current = next;
            }
        }
        else
        {
            prev = current;
            current = next;
        }
    }
}

//Q8: Write a function that deletes the nth element from a linked list. If the linked list doesn't even have n nodes, don't delete any of them. 
node *deleteNthNode(node *head, int n)
{
    int i = 0; 
    int nBigger = 0;
    node *current = head;
    node *prev = current;
    for ( i = 0; i < n; i++)
    {
        prev = current;
        current = current->next;
        if (current == NULL)
        {
            nBigger = 1;
        }
    }

    if(nBigger == 1)
    {
        return head;
    }

    prev = current->next;
    free(current);

    return head;
}

int main(void)
{
    int i, r;

    // The head of our linked list. If we don't initialize it to NULL, our
    // insertNode() function might segfault.
    node *head = NULL;

    srand(time(NULL));

    // Populate the linked list with random integers. We are inserting into the
    // head of the list each time.
    for (i = 0; i < 10; i++)
    {
        printf("Inserting %d...\n", r = rand() % 20 + 1);
        insertNode(&head, r);
    }

    head = insertNodeFront(head, 1);

    tailInsert(head, 5);

    // Print the linked list.
    printList(head);
    printf("\n");
    printReverse(head);
    printf("\n\n");

    // Print the linked list using our recursive function.
    printListRecursive(head);

    //destroyList(&head);
    deleteAltHead(head);
    printf("\n");
    printList(head);
{
    if (head == NULL)
        return;

    struct node *node = head->next;

    if (node == NULL)
        return;

    /* Change the next link of head */
    head->next = node->next;

    /* free memory allocated for node */
    free(node);

    /* Recursively call for the new next of head */
}
     deleteEven(&head);

  //  head = destroyListRecursive(head);
    printf("\n");
    printList(head);

    node *head2 = NULL;
    insertNode(&head2, 1);
    insertNode(&head2, 2);
    insertNode(&head2, 4);

    printf("\n");
    printList(head2);

    insertSorted(head2, 3);
    printf("\n");
    deleteNthNode(head, 1);
    printList(head2);


    //destroyListRecursive(head2);

    system("PAUSE");
    return 0;
}
/*
问题1:
尝试自己实现类中显示的函数:
检查:节点创建
选中:在链表末尾插入,
选中:在链表的开头插入,
检查:列表打印功能。
问题2:
检查:编写递归printList()函数。
问题3:
检查:编写递归tailInsert()函数。
第四题:
检查:编写一个函数,在链表的开头插入节点。
问题5:
检查:编写一个递归函数,按相反顺序打印链表。
功能签名为:void printReverse(节点*头部);
问题6:
检查:编写一个迭代destroyList()函数,释放链表中的所有节点。
问题7:
检查:现在递归实现destroyList()。
问题8:
-编写一个函数,从链表中删除第n个元素。
如果链表甚至没有n个节点,请不要删除其中任何节点。
函数签名是:node*deleteNth(node*head,intn)。
-尝试以迭代和递归的方式实现该函数。
-(关于如何解释n,您可以从0或1开始计算节点数;这是您的选择。)
问题9:
-编写一个函数,删除链表中的所有其他元素。
-(试着用两种方式写:一种是从列表开头开始删除,
-并且在列表头之后的元素处开始删除。)
-你能以迭代和递归的方式写吗?
问题10:
-编写一个函数,从链表中删除所有偶数整数。
问题11:
-编写一个函数,该函数接受已排序的链表和要插入该链表的元素,
并按排序顺序插入元素。
函数签名为:node*insertSorted(node*head,int n);
问题12:
-今天第一个insertNode()函数的问题之一是
要求我们使用head=insertNode(head,i)调用它。
这有点危险,因为我们很容易忘记“head=”部分。
重新编写函数,使其指针指向head,
从而允许它直接修改head的内容,而不需要任何返回值。
函数签名为:void insertNode(节点**head,int data)。
将使用insertNode(&head,i)调用该函数。
*/
//回到
#包括
#包括
//基本链表节点结构;包含“数据”和“下一个”指针。
//如果我们键入“node*next”而不是“struct node*next”,会发生什么?
类型定义结构节点
{
//数据字段
int数据;
//列表中的下一个节点
结构节点*下一步;
}节点;
//分配一个新节点。初始化它的字段。返回指针。
//我们从插入函数中调用它。
节点*createNode(int数据)
{
node*ptr=NULL;
ptr=malloc(sizeof(node));
如果(ptr==NULL)
{
printf(“无法分配空间\n”);
返回NULL;
}
ptr->data=数据;
ptr->next=NULL;
返回ptr;
}
//插入到链接列表的末尾。返回链接列表的标题。
//(此函数的顺序(大Oh)是什么?)
/*
节点*插入节点(节点*头,int数据)
{    
节点*温度;
if(head==NULL)
返回createNode(数据);
对于(临时=头部;临时->下一步!=NULL;临时=临时->下一步)
;
temp->next=createNode(数据);
回流头;
}
*/
节点*insertNodeFront(节点*head,int数据)
{
节点*温度;
if(head==NULL)
返回createNode(数据);
temp=createNode(数据);
温度->下一步=头部;
返回温度;
}   
//打印链接列表内容的简单函数。
无效打印列表(节点*头)
{
if(head==NULL)
{
printf(“空列表”);
返回;
}
for(;head!=NULL;head=head->next)
printf(“%d”,头部->数据);
printf(“\n”);
}
无效printListRecursiveHelper(节点*头)
{
if(head==NULL)
返回;
printf(“%d%c”,head->data,(head->next==NULL)?'\n':'';
printListRecursiveHelper(头部->下一步);
}
void printListRecursive(节点*头)
{
if(head==NULL)
{
printf(“空列表”);
返回;
}
printListRecursiveHelper(头);
}
//Q3:-编写一个递归tailInsert()函数。
节点*tailInsert(节点*head,int数据)
{
if(head->next==NULL)
{
节点*温度;
temp=createNode(数据);
temp->next=NULL;
头部->下一步=温度;
返回温度;
}
返回tailInsert(head->next,data);
}   
//问题5:编写一个递归函数,按相反顺序打印链表。
无效打印反转(节点*头部)
{
if(head==NULL)
返回;
打印反转(头部->下一步);
printf(“%d”,头部->数据);
}
//Q6:-编写一个迭代destroyList()函数,释放链表中的所有节点。
//从网上得到代码,记住它
/*函数删除整个链接列表*/
无效销毁列表(结构节点**头)
{
结构节点*当前=*头部;
结构节点*下一步;
while(当前!=NULL)
{
下一步=当前->下一步;
自由(电流);
当前=下一个;
}
*head=NULL;
}
//Q7:-现在递归地实现destroyList()。
//在线查找,需要检查它为什么不起作用
节点*头(节点*头)
{
if(head!=NULL)
{
删除列表递归(head->next);
自由(头);
}
返回NULL;
}
/*问题8:
-写一个函数t