Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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()*/
 
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.
Posted 8-Oct-11 4:04am
Edited 8-Oct-11 4:09am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Mehdi Gholam at 8-Oct-11 10:34am
   
My 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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>
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 RyanDev 270
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 185
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 8 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100