【问题标题】:Map Array in C - Linked List w/ Key: ValueC中的映射数组-带键的链表:值
【发布时间】:2014-01-22 15:48:36
【问题描述】:

好吧,我的问题是,当我编写节点时,它只会覆盖头节点,因此当您调用 map_get 时,您只会得到一个带有数据的节点。我想将数据放在头上或将头节点推到前面并将其挂在前面,无论哪种方式最简单,都最好感谢您的帮助。

#include <assert.h>
#include<stdlib.h>
#include "map.h"
#include <string.h>
int main(){
map_t* newList = malloc(sizeof(map_t));
map_init(newList);
const char* passString ="a";
const char* secondString="2";
map_put(newList,"3","3");
map_put(newList,"7","34");
map_put(newList,"a","45");
map_put(newList,passString,secondString);
map_get(newList,secondString);
}

void map_init(map_t* self) {
map_entry_t* newNode= malloc(sizeof(map_entry_t)); // allocate
self->size = 0;
self->entry = newNode; // link next
}

int map_put(map_t* self, const char* key, const char* val) {
  assert(self != NULL);
map_entry_t* current;
//current = self->entry->key;
while(current->next != NULL){
    current = current->next;

}
self->entry->key = key;
self->entry->value = val;
map_entry_t* newNode = malloc(sizeof(map_entry_t));


printf("\ntry printing the list \n");
printf(self->entry->key);
printf(" :was the key and the value is \n");
printf(self->entry->value);
printf("\n");


}

const char* map_get(map_t* self, const char* key) {
  assert(self != NULL);
const char* target = key;
int i=0;
for(i; i<=self->size; i++) 
{
printf("\n\ninside the list\n");
printf(self->entry->value);
printf("\n");
}
}

int map_size(map_t* self) {
  assert(self != NULL);

}

int map_remove(map_t* self, const char* key) {
  assert(self != NULL);

}

int map_serialize(map_t* self, FILE* stream) {
  assert(self != NULL);

}

int map_deserialize(map_t* self, FILE* stream) {
  assert(self != NULL);

}

void map_destroy(map_t* self) {
  assert(self != NULL);

}


map.h

    #ifndef __A1_MAP_H__
#define __A1_MAP_H__

#include <stdio.h>

#define SYS_ERROR -1
#define OK 0
#define KEY_EXISTS 1
#define NO_KEY_EXISTS 2

// Strange type definition due to the recursive nature
//   of the struct. This technique is called 'forward
//   declaration' and is necessary for compilation reasons.
typedef struct _map_entry map_entry_t;
struct _map_entry {
  char* key;
  char* value;
  map_entry_t* next;
} ;

typedef struct _map {
  map_entry_t* entry;
  int size;
} map_t;

// Part one functions.
void map_init(map_t*);
int map_put(map_t*, const char*, const char*);
const char* map_get(map_t*, const char*);
int map_remove(map_t*, const char*);
int map_size(map_t*);
void map_destroy(map_t*);

// Part two functions. 
int map_serialize(map_t*, FILE*);
int map_deserialize(map_t*, FILE*);

#endif

【问题讨论】:

    标签: c map linked-list associative-array


    【解决方案1】:

    map_put 中,您应该初始化current 以指向列表的头部self。现在,它开始指向内存中的……某处……然后你尝试从那里遍历一个列表。然后您完全忽略它并直接写入列表的头元素 (self) 而不是新节点 (newNode)。然后,您还需要设置从当前节点到新节点的链接。

    【讨论】:

    • 我知道需要发生什么我不知道它在代码中是什么样的,我知道我需要附加到应该更容易的头节点或者我需要推送头节点并推送入栈一个新节点。我宁愿在追加模式下这样做,有人可以告诉我通过追加节点来添加节点的正确方法
    • 使用= 初始化变量,如current = self。您需要将所有复制更改为self-&gt;things 并改用newNode,因为毕竟这就是它存在的原因(成为新节点)。然后你需要设置current 中的next 字段指向你的新节点,这样东西才能真正进入你的列表。
    • 您能否提供一个示例,我在阅读了我的代码并看到您刚才谈到的一些观点后做了一些更改。我现在必须考虑一个列表是如何工作的,唯一的问题实际上是让它挂在新节点上。生病重新上传代码,请提供节点添加示例
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2018-02-24
    • 2019-11-10
    • 2021-08-16
    相关资源
    最近更新 更多