删除后显示在用C链表 [英] Display after deletion in linked list in C
问题描述
事实上,这是另外一个问题,但它改变了,所以我决定开一个新的问题。
我的code是
typedef结构inner_list
{
诠释计数;
字符字[100];
inner_list *接下来的;
} inner_list;
typedef结构outer_list
{
字符字[100];
inner_list *头;
诠释计数;
outer_list *接下来的;
} outer_list;
无效delnode(outer_list **头,炭NUM [100])//感谢尼尔·利维和杰里米P.都
{
outer_list *温度,*米;
M = TEMP = *头; / *#FIX 1 * /
而(温度!= NULL){
如果(STRCMP(TEMP-GT&;文字,NUM)== 0){
如果(临时== *头){
delinner(TEMP-GT&;头); / *#FIX 2 * /
*头= TEMP->接下来, 免费(TEMP);
返回;
}其他{
delinner(TEMP-GT&;头); / *#FIX 2 * /
M->接下来= TEMP->接下来, 免费(TEMP);
返回;
}
}其他{
M =温度;
TEMP = TEMP->接下来,
}
}
的printf(元素%S NOT FOUND,NUM);
}
无效delinner(inner_list *头){/ *#FIX 2 * /
inner_list *温度;
TEMP =头;
而(温度!= NULL){
头= TEMP->接下来,
免费(TEMP);
TEMP =头;
}
}
无效delnode2(outer_list *了,inner_list **头,炭NUM [100])
{
inner_list * TEMP2,*温度,*米;
outer_list * P;
P =起来; 而(P!= NULL){M = TEMP = = TEMP2 P->头;
而(温度!= NULL){
如果(STRCMP(TEMP-GT&;文字,NUM)== 0){
如果(临时==(*头)){
*头= TEMP->接下来, 免费(TEMP);
返回;
}其他{
M->接下来= TEMP->接下来,
免费(TEMP);
返回;
}
}其他{
M =温度;
TEMP = TEMP->接下来,
}
}
p值=对 - >接着,
}
的printf(元素%S NOT FOUND,NUM);
}
无效print_node(outer_list * PARENT_NODE)
{
而(PARENT_NODE!= NULL){
的printf(%S \\ t%d个\\ t的,parent_node->的话,parent_node->计数); inner_list * child_node = parent_node->头;
的printf(名单);
如果(child_node == NULL){printf的(BUARADA);} 而(child_node!= NULL){
的printf(%S-%D,child_node->的话,child_node->计数); child_node = child_node->接下来, 如果(child_node!= NULL){
的printf( - >中);
}
}
的printf(\\ n);
PARENT_NODE = parent_node->接下来,
}
}
虽然从外部列表中删除一个元素,我也试图从inner_list的删除相同的元素了。
例如: - 比方说,AAA是outer_list链表中的一个元素,让我们点它与outer_list * P - 这个AAA也可在inner_list链表了。 (也可以是在p->头或其他innerlist。)现在,棘手的部分一次。我试图用outer_list删除,但每当我删除inner_list它给出了一个错误的头元素的运用相同的规则。哪里是在print_node或delnode2错误的事情?
编辑:
实际上,如果它一outer_list节点删除inner_list的内部应该过于除去链表。这就是为什么使用delinner方法。
例如:
外内
AAA BB-> CC
BB&AAA- GT; CC当我想删除AAA的结果应该是:
外内
BB CC
什么是 inner_list **头
参数delnode()
?我认为最高
是外单节点上,从它的内部列表要删除包含 NUM $ C $指定的一个字符串节点C>。
头
只是不适合这张图片。而你似乎没有使用它无论如何正确。我重写功能了一下,省略该参数,注释行发生变化,并给予更有意义的名称:
无效del_inner_node(outer_list *了,烧焦NUM [100])
{
inner_list *温度,*米;
outer_list * P;
P =起来; 而(P!= NULL){
M = TEMP = P->头;
而(温度!= NULL){
如果(STRCMP(TEMP-GT&;文字,NUM)== 0){
如果(临时== p型>头){//指对 - >头
P->头= TEMP->接下来, //指对 - >头
免费(TEMP);
返回;
}其他{
M->接下来= TEMP->接下来,
免费(TEMP);
返回;
}
}其他{
M =温度;
TEMP = TEMP->接下来,
}
}
p值=对 - >接着,
}
的printf(元素%S NOT FOUND,NUM);
}
注意 TEMP2
未使用,所以我删除了。
现在,在你的code还有就是 delnode2
没有呼叫( del_inner_node
)。其实你可以在 delnode
调用它,以防查找的字符串未在当前外节点发现:
无效del_all_nodes(outer_list **头,炭NUM [100])//感谢尼尔·利维和杰里米P.都
{
...
而(温度!= NULL){
如果(STRCMP(TEMP-GT&;文字,NUM)== 0){
...
}其他{
del_inner_node(温度,NUM);
M =温度;
TEMP = TEMP->接下来,
}
}
...
}
这样你可以删除包含AAA通过一个调用的所有节点:
outer_list *头;
//设置列表
del_all_nodes(安培;头,AAA);
Actually this was another problem but it changed so I decided to open a new question.
My code is
typedef struct inner_list
{
int count;
char word[100];
inner_list*next;
} inner_list;
typedef struct outer_list
{
char word [100];
inner_list * head;
int count;
outer_list * next;
} outer_list;
void delnode(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P.
{
outer_list *temp, *m;
m=temp=*head; /*FIX #1*/
while(temp!=NULL) {
if(strcmp(temp->word,num)==0) {
if(temp==*head) {
delinner(temp->head); /* FIX#2 */
*head=temp->next;
free(temp);
return;
} else {
delinner(temp->head); /* FIX#2 */
m->next=temp->next;
free(temp);
return;
}
} else {
m=temp;
temp= temp->next;
}
}
printf(" ELEMENT %s NOT FOUND ", num);
}
void delinner(inner_list *head) { /* FIX#2 */
inner_list *temp;
temp=head;
while(temp!=NULL) {
head=temp->next;
free(temp);
temp=head;
}
}
void delnode2(outer_list *up,inner_list **head,char num[100])
{
inner_list *temp2,*temp, *m;
outer_list *p;
p = up;
while(p!=NULL){m=temp=temp2=p->head;
while(temp!=NULL) {
if(strcmp(temp->word,num)==0) {
if(temp==(*head)) {
*head=temp->next;
free(temp);
return;
} else {
m->next=temp->next;
free(temp);
return;
}
} else {
m=temp;
temp= temp->next;
}
}
p=p->next;
}
printf(" ELEMENT %s NOT FOUND ", num);
}
void print_node(outer_list *parent_node)
{
while(parent_node!=NULL){
printf("%s\t%d\t", parent_node->word, parent_node->count);
inner_list *child_node = parent_node->head;
printf("list: ");
if(child_node ==NULL){printf("BUARADA");}
while (child_node != NULL) {
printf("%s-%d", child_node->word,child_node->count);
child_node = child_node->next;
if (child_node != NULL) {
printf("->");
}
}
printf("\n");
parent_node = parent_node->next;
}
}
While deleting an element from outer list I am also trying the delete the same element from inner_list too.
For example: - Let's say aaa is an element of outer_list linked list and let's point it with outer_list *p - This aaa can also be in an inner_list linked list too. (it can be in p->head or another innerlist.) Now, the tricky part again. I tried to apply the same rules with outer_list deletion but whenever i delete the head element of inner_list it gives an error. Where is the wrong thing in print_node or delnode2?
Edit: actually if it a outer_list node deleted the linked list of inner_list inside of it should be removed too. This is why delinner method used.
For example:
outer inner
aaa bb->cc
bb aaa->cc
when i wanted to delete "aaa" The result should be:
outer inner
bb cc
What is the inner_list **head
parameter of delnode()
? I assume that up
is the outer list node, from whose inner list you want to remove the node containing the string given in num
. head
just doesn't fit into this picture. And you don't seem to be using it properly anyway. I rewrote the function a bit, omitting the parameter, changing the commented lines, and giving more meaningful names:
void del_inner_node(outer_list *up, char num[100])
{
inner_list *temp, *m;
outer_list *p;
p = up;
while (p != NULL) {
m = temp = p->head;
while(temp!=NULL) {
if(strcmp(temp->word,num)==0) {
if(temp==p->head) { // refer to p->head
p->head=temp->next; // refer to p->head
free(temp);
return;
} else {
m->next=temp->next;
free(temp);
return;
}
} else {
m=temp;
temp= temp->next;
}
}
p=p->next;
}
printf(" ELEMENT %s NOT FOUND ", num);
}
Note that temp2
is not used, so I removed it.
Now, in your code there is no call to delnode2
(del_inner_node
). You could actually call it within delnode
, in case the searched string was not found in the current outer node:
void del_all_nodes(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P.
{
...
while(temp!=NULL) {
if(strcmp(temp->word,num)==0) {
...
} else {
del_inner_node(temp,num);
m=temp;
temp= temp->next;
}
}
...
}
This way you can remove all nodes containing "aaa" with a single call:
outer_list *head;
// set up the lists
del_all_nodes(&head, "aaa");
这篇关于删除后显示在用C链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!