void insert(Node* h, const char a[]) { Node* t = (Node*)malloc(sizeof(Node)); strcpy(t->Name, a); t->next = NULL; // printf("%s\n",t->Name); Node* p = h; if(p == NULL) p = t; else { while(p->next != NULL) { p = p->next; // printf("234"); } p->next = t; } // printf("%s\n",t->Name); } 改正 这段代码
时间: 2024-03-13 20:48:02 浏览: 18
为了解决这段代码的问题,需要在链表非空时将新节点插入到链表尾部。可以通过在while循环中找到链表的尾部来实现这一目标。此外,还需要添加错误检查来确保内存分配成功。下面是改正后的代码:
```
void insert(Node** h, const char a[]) {
Node* t = (Node*)malloc(sizeof(Node));
if (t == NULL) {
printf("Memory allocation failed!");
return;
}
strcpy(t->Name, a);
t->next = NULL;
Node* p = *h;
if (p == NULL) {
*h = t;
} else {
while (p->next != NULL) {
p = p->next;
}
p->next = t;
}
}
```
需要注意的是,由于需要修改链表头部指针的值,因此需要将链表头部指针的地址传递给函数,即使用指向指针的指针(double pointer)。
相关问题
请参考我给出的代码框架,实现对EMPLOYEE结构体为数据的双向链表的排序算法,要求按照按employeeId升序排列 typedef struct linkNode { void* data; //使用空指针使得NODE适配多种数据结构 struct linkNode* preNode; struct linkNode* nextNode; }LINKED_NODE; /*Define the struct of double linked list.*/ typedef struct { LINKED_NODE* head; LINKED_NODE* tail; size_t size; }DOUBLE_LINK_LIST; typedef struct { int employeeId; char name[20]; char ipAddress[30]; char seatNumber[20]; char group[10]; } EMPLOYEE; DOUBLE_LINK_LIST* createDoubleLinkedList() { DOUBLE_LINK_LIST* newList = (DOUBLE_LINK_LIST*)malloc(sizeof(DOUBLE_LINK_LIST)); newList->head = NULL; newList->tail = NULL; newList->size = 0; return newList; } void destroyDoubleLinkedList(DOUBLE_LINK_LIST* list) {} /*Add a new node before the head.*/ void insertHead(DOUBLE_LINK_LIST* list, void* data) // void执政适配其他data类型? {} /*Add a new node after tail.*/ void insertTail(DOUBLE_LINK_LIST* list, void* data) // 如何适配其他data类型? {} /*Insert a new node.*/ void insertNode(DOUBLE_LINK_LIST* list, void* data,int index) // 如何适配其他data类型? {} void deleteHead(DOUBLE_LINK_LIST* list) {} void deleteTail(DOUBLE_LINK_LIST* list) {} void deleteNode(DOUBLE_LINK_LIST* list, int index) {} LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index) {} /* 遍历链表,对每个节点执行指定操作*/ void traverseList(DOUBLE_LINK_LIST* list, void (*callback)(void*)) { LINKED_NODE* currentNode = list->head; while (currentNode != NULL) { callback(currentNode->data); currentNode = currentNode->nextNode; } } void printEmployee(void* data) {}
你可以使用以下代码来实现对EMPLOYEE结构体为数据的双向链表的排序算法,按照按employeeId升序排列:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct linkNode
{
void* data; //使用空指针使得NODE适配多种数据结构
struct linkNode* preNode;
struct linkNode* nextNode;
} LINKED_NODE;
/*Define the struct of double linked list.*/
typedef struct
{
LINKED_NODE* head;
LINKED_NODE* tail;
size_t size;
} DOUBLE_LINK_LIST;
typedef struct
{
int employeeId;
char name[20];
char ipAddress[30];
char seatNumber[20];
char group[10];
} EMPLOYEE;
DOUBLE_LINK_LIST* createDoubleLinkedList()
{
DOUBLE_LINK_LIST* newList = (DOUBLE_LINK_LIST*)malloc(sizeof(DOUBLE_LINK_LIST));
newList->head = NULL;
newList->tail = NULL;
newList->size = 0;
return newList;
}
void destroyDoubleLinkedList(DOUBLE_LINK_LIST* list)
{
LINKED_NODE* currentNode = list->head;
while (currentNode != NULL)
{
LINKED_NODE* nextNode = currentNode->nextNode;
free(currentNode);
currentNode = nextNode;
}
free(list);
}
/*Add a new node before the head.*/
void insertHead(DOUBLE_LINK_LIST* list, void* data)
{
LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE));
newNode->data = data;
newNode->preNode = NULL;
newNode->nextNode = list->head;
if (list->head != NULL)
list->head->preNode = newNode;
list->head = newNode;
if (list->tail == NULL)
list->tail = newNode;
list->size++;
}
/*Add a new node after tail.*/
void insertTail(DOUBLE_LINK_LIST* list, void* data)
{
LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE));
newNode->data = data;
newNode->preNode = list->tail;
newNode->nextNode = NULL;
if (list->tail != NULL)
list->tail->nextNode = newNode;
list->tail = newNode;
if (list->head == NULL)
list->head = newNode;
list->size++;
}
/*Insert a new node.*/
void insertNode(DOUBLE_LINK_LIST* list, void* data, int index)
{
if (index < 0 || index > list->size)
{
printf("Invalid index\n");
return;
}
if (index == 0)
{
insertHead(list, data);
return;
}
if (index == list->size)
{
insertTail(list, data);
return;
}
LINKED_NODE* currentNode = getNode(list, index);
LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE));
newNode->data = data;
newNode->preNode = currentNode->preNode;
newNode->nextNode = currentNode;
currentNode->preNode->nextNode = newNode;
currentNode->preNode = newNode;
list->size++;
}
void deleteHead(DOUBLE_LINK_LIST* list)
{
if (list->head == NULL)
return;
LINKED_NODE* oldHead = list->head;
list->head = oldHead->nextNode;
if (list->head != NULL)
list->head->preNode = NULL;
free(oldHead);
list->size--;
if (list->size == 0)
list->tail = NULL;
}
void deleteTail(DOUBLE_LINK_LIST* list)
{
if (list->tail == NULL)
return;
LINKED_NODE* oldTail = list->tail;
list->tail = oldTail->preNode;
if (list->tail != NULL)
list->tail->nextNode = NULL;
free(oldTail);
list->size--;
if (list->size == 0)
list->head = NULL;
}
void deleteNode(DOUBLE_LINK_LIST* list, int index)
{
if (index < 0 || index >= list->size)
{
printf("Invalid index\n");
return;
}
if (index == 0)
{
deleteHead(list);
return;
}
if (index == list->size - 1)
{
deleteTail(list);
return;
}
LINKED_NODE* currentNode = getNode(list, index);
currentNode->preNode->nextNode = currentNode->nextNode;
currentNode->nextNode->preNode = currentNode->preNode;
free(currentNode);
list->size--;
}
LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index)
{
if (index < 0 || index >= list->size)
{
printf("Invalid index\n");
return NULL;
}
LINKED_NODE* currentNode = list->head;
int i = 0;
while (i < index)
{
currentNode = currentNode->nextNode;
i++;
}
return currentNode;
}
void traverseList(DOUBLE_LINK_LIST* list, void (*callback)(void*))
{
LINKED_NODE* currentNode = list->head;
while (currentNode != NULL)
{
callback(currentNode->data);
currentNode = currentNode->nextNode;
}
}
void printEmployee(void* data)
{
EMPLOYEE* employee = (EMPLOYEE*)data;
printf("Employee ID: %d, Name: %s\n", employee->employeeId, employee->name);
}
int compareEmployee(const void* a, const void* b)
{
EMPLOYEE* employeeA = (EMPLOYEE*)a;
EMPLOYEE* employeeB = (EMPLOYEE*)b;
return employeeA->employeeId - employeeB->employeeId;
}
void sortList(DOUBLE_LINK_LIST* list)
{
size_t dataSize = sizeof(EMPLOYEE);
EMPLOYEE** employeeArray = (EMPLOYEE**)malloc(list->size * sizeof(EMPLOYEE*));
LINKED_NODE* currentNode = list->head;
size_t i = 0;
while (currentNode != NULL)
{
employeeArray[i] = (EMPLOYEE*)currentNode->data;
currentNode = currentNode->nextNode;
i++;
}
qsort(employeeArray, list->size, dataSize, compareEmployee);
currentNode = list->head;
i = 0;
while (currentNode != NULL)
{
currentNode->data = employeeArray[i];
currentNode = currentNode->nextNode;
i++;
}
free(employeeArray);
}
int main()
{
DOUBLE_LINK_LIST* list = createDoubleLinkedList();
EMPLOYEE* employee1 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
employee1->employeeId = 2;
strcpy(employee1->name, "John");
insertHead(list, employee1);
EMPLOYEE* employee2 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
employee2->employeeId = 1;
strcpy(employee2->name, "Alice");
insertHead(list, employee2);
EMPLOYEE* employee3 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
employee3->employeeId = 3;
strcpy(employee3->name, "Bob");
insertHead(list, employee3);
printf("Before sorting:\n");
traverseList(list, printEmployee);
sortList(list);
printf("\nAfter sorting:\n");
traverseList(list, printEmployee);
destroyDoubleLinkedList(list);
return 0;
}
```
这段代码首先定义了双向链表的结构体和EMPLOYEE结构体,然后实现了双向链表的创建、销毁、插入、删除、遍历等操作。其中,`sortList`函数使用了快速排序算法对双向链表中的EMPLOYEE结构体按照employeeId升序进行排序。在`main`函数中,创建了一个双向链表并插入了三个EMPLOYEE结构体,然后调用`sortList`函数对链表进行排序并输出结果。
请注意,在代码中使用了动态内存分配(`malloc`)来分配内存,并在适当的时候使用了`free`来释放内存,以防止内存泄漏。
#pragma GCC optimize ("O3") #pragma pack (16)//所有的存储都是以16个字节为单位的 #include <stdio.h> #include <stdlib.h> #include <string.h> #define tolower(c) (c>='A'&&c<='Z')?c-'A'+'a':c #define DATA 5200000 #define SIZE 1000005 int trie[4200000][26]; typedef struct node { int cnt; int logo; struct node *child[26]; } Node; Node *root; char str[35000000]; typedef struct word { char wor[85]; int cnt; } Word; Word w[300000]; struct node *creat() { Node *Root = (Node *)malloc(sizeof(Node)); Root->logo = 0; Root->cnt = 0; for (int i = 0; i < 26; i++) { Root->child[i] = NULL; } return Root; } void insert(Node *root, char *word, int flag) { struct node *leaf = root; for (int i = 0; word[i] != '\0'; i++) { int index = word[i] - 'a'; if (!leaf->child[index]) { leaf->child[index] = creat(); } leaf = leaf->child[index]; } if (leaf->logo != -1) leaf->logo = flag; leaf->cnt++; } int count = 0; void dfs(Node *leaf, char *word, int level) { if (leaf->logo == 1) { word[level] = '\0'; strcpy(w[count++].wor, word); w[count - 1].cnt = leaf->cnt; } for (int i = 0; i < 26; i++) { if (leaf->child[i]) { word[level] = i + 'a'; dfs(leaf->child[i], word, level + 1); } } } int cmp(const void *p1, const void *p2) { Word *v1, *v2; v1 = (Word *)p1; v2 = (Word *)p2; if (v1->cnt != v2->cnt) return v2->cnt - v1->cnt; else return strcmp(v1->wor, v2->wor); } int main(int argc, char *argv[]) { char s[1024]; int temp; int n, m;//读入n,m; //n = atoi(argv[1]); //m = atoi(argv[2]); scanf("%d%d", &n, &m); //读入stopwords中的元素,并令末序数组值为0,即该单词不计入 root = creat(); FILE *stopwords = fopen("stopwords.txt", "r"); while (fscanf(stopwords, "%s", s) != EOF) { insert(root, s, -1); } int cnt; FILE *article = fopen("article.txt", "r"); cnt = fread(str, sizeof(char), 35000000, article); char word[85]; int w_cnt = 0; for (int i = 0; i < cnt; i++) { char c = tolower(str[i]); if (c >= 'a' && c <= 'z') { word[w_cnt++] = c; } else { word[w_cnt] = '\0'; p = 0; w_cnt = 0; if (strlen(word) > 0) { insert(root, word, 1); } }//对article中的所有单词进行计数 } dfs(root, word, 0); qsort(w, count, sizeof(w[0]), cmp); printf("%s", w[0].cnt); return 0; }
这段代码是一个基于Trie树的单词统计程序,在给定的文章中统计出现频率最高的单词。程序从文件中读入stopwords(不计入统计的常用单词)和article(待统计的文章),使用Trie树统计文章中出现的单词频率,并按照出现频率从高到低输出结果。
具体实现中,程序使用了一个结构体Node表示Trie树中的节点,其中包含了该节点出现次数cnt和单词编号logo。同时,程序定义了一个结构体Word表示单词及其出现次数。程序中的insert函数用于向Trie树中插入单词,dfs函数用于遍历Trie树并统计单词出现次数,cmp函数用于按照出现频率从高到低排序输出结果。
注意,程序中的一些细节需要注意,比如将所有字母转换为小写字母,以及对于长度为0的单词不进行计数等。