删除操作中的双链表 [英] doubly linked list in delete operation

查看:57
本文介绍了删除操作中的双链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

del(int num)
{
    struct node *tmp,*q;
    if(start->info==num)
    {
        tmp=start;
        start=start->next;  /*first element deleted*/
        start->prev = NULL;
        free(tmp);
        return;
    }
    q=start;
    while(q->next->next!=NULL)
    {
        if(q->next->info==num)     /*Element deleted in between*/
        {
            tmp=q->next;
            q->next=tmp->next;
            tmp->next->prev=q;
            free(tmp);
            return;
        }
        q=q->next;
    }
    if(q->next->info==num)    /*last element deleted*/
    {   tmp=q->next;
        free(tmp);
        q->next=NULL;
        return;
    }
    printf("Element %d not found\n",num);
}/*End of del()*/


的结尾
朋友你好

我正在尝试做双链表程序.我在 http://thecodecracker.com/c-programming/double-linked-list/ [ ^ ]这个网站,但我并未执行删除操作,而是执行了该概念下的图形处理.一些我听不懂的命令,请帮我的朋友们.
任何人都可以解释每一行.



Hello friends

I am trying to do doubly linked list program. I saw the code on http://thecodecracker.com/c-programming/double-linked-list/[^] this site, but I did not under stand the delete operation but graphical vice I under stand the concept. Some commands I could not understand please help me friends.
Any one explain each line.

推荐答案

您知道解释每一行"涉及多少工作吗?
每一行都需要一段文本来解释它!
如果您希望为您完成那么多工作,那么建议您在网站上查询您的代码...

它所做的一切非常简单:
如果列表的开头是您要查找的节点,请作为特殊情况将其删除.
如果不是,请遍历列表以查找要删除的元素.如果找到它,将其删除.
如果找不到它,请检查它是否是最后一个节点,并在特殊情况下将其删除.

这有点垃圾-我不会那样写-但只要列表中有多个条目,它就可以工作.如果列表中有一个或零个条目,我怀疑它会相当失败.我不确定最终测试是否确实需要,但是正如我所说,我不会那样写.
Do you have any idea how much work is involved in "explain each line"?
Every single line would need a paragraph of text to explain it!
Ifg you want that much work done for you, then I suggest you ask on the site you got the code from...

All it is doing is pretty simple:
If the start of the list is the node you are looking for, delete it as a special case.
If it isn''t, pass through the list to find the element to delete. If you find it, delete it.
If you don''t find it, check if it is the final node and delete it as a special case if it is.

It''s a bit rubbish - I wouldn''t have written it that way - but it should work, provided there is more than one entry in the list. If there is one or zero entries in the list, I suspect it will fail fairly spectacularly. I''m not convinced that the final test is really needed, but as I say, I wouldn''t have written it that way.


在这里,我写了很长的一段话以前是在C语言中编写的.我在编写代码时对代码进行了注释,因此不必记住它是如何工作的.您可能会发现有关如何修饰双链表的阅读说明:

头文件
Here''s something I wrote a long time ago in straight C. I annotated the code as I wrote it so I wouldn''t have to remember how it worked ever again. You might find the reading instructional on how to fiddle with double linked lists:

Header File
#pragma once

/* Useful Structures */

typedef struct tag_queue_entry {
    struct tag_queue_entry *qprev;  /* pointer to previous */
    struct tag_queue_entry *qnext;  /* pointer to next */
  } queue_entry_t;

typedef struct tag_queue_header {
    queue_entry_t *qfirst;      /* pointer to first */
    queue_entry_t *qlast;       /* pointer to last */
  } queue_header_t;


/* Function prototypes */

#ifdef _MSC_VER                                         /* MicroSoft C++ compiles, declare externals as "C" calling standard */
#ifdef __cplusplus
extern "C" {
#endif
#endif

void queue_insert(queue_entry_t *e, queue_header_t *h, queue_entry_t *t);   /* insert e into queue h after entry this */
void queue_remove(queue_entry_t *e, queue_header_t *h);             /* remove e from queue h */

#ifdef _MSC_VER
#ifdef __cplusplus
}
#endif
#endif



代码正文



Code Body

#include "StdAfx.h"
#include <stdlib.h>			/* some good stuff */
#include "queueV2.h"		/* get symbols */

/**
**	queue_insert()
**
**	Enters an element into a double threaded queue.
**	The queue header must be initialized to NULL and the
**	links are maintained so that a previous or next of NULL
**	indicates the end of the queue.
**
**	This always inserts after the "this" entry which permits:
**
**	  insert after this	queue_insert(e, h, this);
**	  insert at head	queue_insert(e, h, NULL);
**	  insert at tail	queue_insert(e, h, h->qlast);
**	  insert before this	queue_insert(e, h, this->qprev);
**/

void queue_insert(queue_entry_t *e, queue_header_t *h, queue_entry_t *t)
{
	queue_entry_t *n;

	if (t == NULL)		/* if inserting at the head of the queue */
	{
		n = h->qfirst;		/* next is old first */
		h->qfirst = e;		/* new is also first */
	}
	else
	{
		n = t->qnext;		/* get this''s next */
		t->qnext = e;		/* likewise, new is this''s next */
	}
	e->qprev = t;		/* new''s prev is this (or NULL if first) */
	if ((e->qnext = n) == NULL)	/* point to old next, if now last */
		h->qlast = e;		/* store new last in queue */
	else n->qprev = e;	/* else new is next''s prev */
} /* end of queue_insert() */

/**
**	queue_remove()
**
**	Removes an element into a double threaded queue.
**/

void queue_remove(queue_entry_t *e, queue_header_t *h)
{
	queue_entry_t *p;
	queue_entry_t *n;

	n = e->qnext;			/* remember next */
	if ((p = e->qprev) == NULL)		/* if this was the first (or only) */
		h->qfirst = n;			/* next is now first */
	else p->qnext = n;		/* else prev''s next is old next */
	if (n == NULL)			/* if this was last (or only) */
		h->qlast = p;			/* prev is now last */
	else n->qprev = p;		/* else next''s prev is old prev */
} /* end of queue_remove() */
</stdlib.h>


这篇关于删除操作中的双链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆