谁可以帮我提供C中的关键列表?

时间:2015-11-04 21:22:37

标签: c

这是我的应用程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


struct node
{
     int data;
    struct node *next;
};


    void push(struct node** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}


    void deleteKey(struct node **head_ref, int key)
{

struct node* temp = *head_ref, *prev;


while (temp != NULL && temp->data == key)
{
    *head_ref = temp->next;   // Changed head
    free(temp);               // free old head
    temp = *head_ref;         // Change Temp
}


while (temp != NULL)
{

    while (temp != NULL && temp->data != key)
    {
        prev = temp;
        temp = temp->next;
    }


    if (temp == NULL) return;


    prev->next = temp->next;

    free(temp);  // Free memory


    temp = prev->next;
    }
}


    void printList(struct node *node)
    { 
    while (node != NULL)
    {
        printf(" %d ", node->data);
        node = node->next;
    }
}


    int main()
{

struct node* head = NULL;

push(&head, 7);
push(&head, 2);
push(&head, 3);
push(&head, 2);
push(&head, 8);
push(&head, 1);
push(&head, 2);
push(&head, 2);

int key = 2; // key to delete

puts("Created Linked List: ");
printList(head);

deleteKey(&head, key);
puts("\nLinked List after Deletion of 1: ");

printList(head);
return 0;
}

要求:

#ifndef LNKDLST_H_
#define LNKDLST_H_
/*
 * Function : Initialises the list
 * Purpose : Ensures that pointers are set to point to the right thing
  If the lists exists already it is emptied before
  initialising a new.
  * Return : 1 indicates success, 0 error

 int initialise ();
 /*
  * Function : put
  * Purpose : Add data mapped with key to the end of the list
  * Return : 1 indicates success, 0 error

 int put (int key, void *data);
 /*
  * Function : get
  * Purpose : Get the first item in the list and return the data
  * Return : A pointer to the data, NULL on error

 void *get ();
 /*
  * Function : getKeyd
  * Purpose : Get the item with key and return the data
  * Return : A pointer to the data, NULL on error

 void *getKeyd (int key);
 /*
  * Function : isEmpty
  * Purpose : Checks if the list is empty
  * Return : 1 to indicate an empty list, 0 a non empty list

 int isEmpty();
 /*
  * Function : exists
  * Purpose : Checks if the item with key exists in the list
  * Return : 1 if key exists, 0 if not

 int exists (int key);
 #endif 

我试图重新设计程序以满足要求,但我发现很难遵循。我是否需要制作另一个节目,或者可以重新设计这个节目?

这是一项家庭作业,但是我被困住了,无法弄清楚它是如何正确完成的。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望使用上面编写的程序来达到要求。
可以使用您自己的代码重新设计它。

例如:对于get()

https://{databaseaccount}.documents.azure.com/dbs/{_rid-db}/colls/{_rid-col}/docs

通过这种方式,您可以使用程序中的代码并稍微修改它以实现所要求的内容。 如果列表存在,则您访问的第一个节点是列表的头部。系统会要求您返回指向列表头部的指针,并将该节点中的数据复制到变量&#39;键中。 如果你仍然遇到麻烦,我会建议你再次通过链表概念,以确保你没有遇到任何麻烦。您可以参考Youtube查找有关这些概念的视频。

相关问题