c - C删除函数中的循环双向链表
问题描述
我有一个循环双向链表。
该deletefront()
功能不起作用:输出错误。错误是什么?
其他功能正在运行。deletefront
但是在调用函数后显示后我得到错误的输出。应该删除的 100 值仍然出现。请改正。
我已经包含了 C 源代码:
// circular doubly linked list
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *rlink;
struct node *llink;
} node;
node *head = NULL;
node *getnode(int ele) {
node *ptr;
ptr = (node *)malloc(sizeof(node));
if (ptr == NULL) {
printf("memory not alloc");
exit(0);
}
if (ptr != NULL) {
ptr->data = ele;
ptr->rlink = NULL;
ptr->llink = NULL;
}
return ptr;
}
void insertfront(int ele) {
node *newnode;
newnode = getnode(ele);
if (head == NULL) {
head = newnode;
head->rlink = head;
head->llink = head;
} else {
head->llink = newnode;
newnode->rlink = head;
head = newnode;
}
}
void insertend(int ele) {
node *newnode;
newnode = getnode(ele);
if (head == NULL) {
head = newnode;
head->rlink = head;
head->llink = head;
} else {
node *temp = head;
do {
temp = temp->rlink;
} while (temp != head->llink);
newnode->rlink = temp->rlink;
temp->rlink = newnode;
newnode->llink = temp;
}
}
int lenlist() {
node *temp;
int count = 0;
temp = head;
do {
temp = temp->rlink;
count++;
} while (temp != head);
return count;
}
void insertatpos(int ele,int pos) {
if (pos == 1) {
insertfront(ele);
} else
if (pos == (lenlist() + 1)) {
insertend(ele);
} else
if (pos > 1 && pos <= (lenlist() + 1)) {
node *prev, *curr;
node *newnode = getnode(ele);
int count = 1;
curr = head;//curr points to 1st node
do {
prev = curr;
count++;
curr = curr->rlink;
if (count == pos) {
prev->rlink = newnode;
newnode->llink = prev;
newnode->rlink = curr;
curr->llink = newnode;
}
} while (curr != head);
} else {
printf("invalid position");
}
}
void delfront() {
if (head == NULL)
printf("empty list");
node *aux;
node *lastnode, *secondnode;
aux = head;
lastnode = head->llink;
secondnode = head->rlink;
secondnode->llink = lastnode;
lastnode->rlink = secondnode;
free(aux);
head = secondnode;
}
void display() {
node *aux = head;
do {
printf("%d->", aux->data);
aux = aux->rlink;
} while (aux != head);
printf("\n");
}
int main() {
insertfront(100);
insertend(20);
printf("\n%d\n", lenlist());
insertatpos(45, 2);
display();
delfront();
display();
}
解决方案
The problem is not in the deletefront()
function, instead, you have missed updating a few links in the insertfront()
and insertend()
functions.
I have updated the code here and also added the comment where I made the changes. Try to visualise it using an example.
However, I suggest that you solve such issues using a debugger or go through the code with a sample test case. It will improve you debugging as well as coding skills!
推荐阅读
- python-2.7 - 使用python将句子列表与另一个列表匹配
- javascript - Java 8 lambda(来自 javascript 示例)
- matlab - 特定条件下系统ODE的慢时间积分
- python - 支持向量机是否将向量作为特征?
- ios - iOS 自定义应用程序 Salesforce - 尝试在 4G 上连接到 Salesforce 时出现 SSL 错误
- javascript - 如何在 react .js 文件中导入带有脚本的 html 文件?
- ruby - 有没有更有效的方法来重构 ruby 上的哈希迭代?
- python-3.x - Amazon DynamoDB [DDB] 并行扫描 - boto 3 - python
- reactjs - 在 Markdown Gatsby 中创建缩略图
- php - 在 td 中显示类别名称