我正在尝试创建一个以字符串作为数据的链表,并且我必须对它们进行一些操作,但是这些操作仅适用于整数。
这些是我过去几周完成的功能:
struct node {
int data;
char* s;
struct node* next;
};
typedef struct node node;
int remove_by_data(node** L, int data) {
if (*L == NULL) {
return 0;
}
node* p = *L;
if (p->data == data) {
*L = p->next;
free(p);
return 1;
}
else {
while (p->next != NULL && p->next->data != data) {
p = p->next;
}
if (p->next == NULL) {
return 0;
}
else {
node* q = p->next;
p->next = p->next->next;
free(q);
return 1;
}
}
}
node* search(node* L, int data) {
node* p = L;
while (p != NULL) {
if (p->data == data)
return p;
p = p->next;
}
return NULL;
}
int insert_end(node** L, int data) {
node* aux = malloc(sizeof(node));
if (!aux)
return 0;
aux->data = data;
aux->next = NULL;
node* p = *L;
if (p == NULL) {
*L = aux;
}
else {
while (p->next) {
p = p->next;
}
p->next = aux;
}
return 1;
}
int insert_begin(node** L, int data) {
node* aux = malloc(sizeof(node));
if (!aux)
return 0;
aux->data = data;
aux->next = *L;
*L = aux;
return 1;
}
void print_data(node* L) {
node* p = L;
while (p != NULL) { // while (p) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int cont(node* L, int N){
int cont=1;
node* atual= L;
while (atual != search(L, N)){
cont++;
atual = atual->next;
}
return cont;
}
void swap_node(node** L, int m, int n){
int i, t;
node* p;
node* q;
p = q = *L;
for(i=1; i<m; i++)
p = p->next;
for(i=1; i<n; i++)
q = q->next;
t = p->data;
p->data = q->data;
q->data = t;
}
我只是不知道如何将那些函数“转换”为字符串,以及如何将字符串存储到列表中