473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

delete node using only one pointer

4
i m trying to solve a textbook problem
to delete a node using one pointer
my code looks like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node{
  4.    int first ;
  5.    struct node *next ;
  6. } ;
  7.  
  8. struct node *add_to_list(struct node *lst, int n){
  9.    struct node *newnode ;
  10.    newnode = malloc(sizeof(struct node)) ;
  11.    if(newnode == NULL){
  12.       printf("malloc failed to add\n") ;
  13.       exit(EXIT_FAILURE) ;
  14.    }
  15.    newnode->first = n ;
  16.    newnode->next = lst ;
  17.    return newnode ;
  18. }
  19.  
  20. void free_list(struct node *lst) {
  21. struct node *p;
  22.    while (lst != NULL) {
  23.    p = lst->next;
  24.    free(lst);
  25.    lst = p;
  26.    }
  27. }
  28.  
  29. void print_list(struct node *lst) {
  30. struct node *p;
  31.    printf("( ");
  32.    for (p = lst; p != NULL; p = p->next)
  33.       printf("%d ", p->first);
  34.    printf(")\n");
  35. }
  36.  
  37. //use only one pointer!
  38. struct node * delete_from_list(struct node *lst, int key){
  39.    struct node *temp ;
  40.    struct node *helper ;
  41.    if(lst == NULL)
  42.       return NULL ;
  43.    if(lst->first == key){
  44.       temp = lst ;
  45.       lst = lst->next ;
  46.       free(temp) ;
  47.       return lst ;
  48.       }
  49.    for(temp = lst ; temp != NULL && temp->first != key ; temp = temp->next){
  50.          ;
  51.    }        
  52.          printf("%d\n",temp->first) ;
  53.       if (temp == NULL) // key is not in the lst
  54.          return NULL ;
  55.       if (temp->first == key){
  56.          temp = temp->next ;
  57.          return lst ;    
  58.       }
  59.  
  60.  
  61.       helper = temp->next ;
  62.       temp->next = helper->next ;
  63.       //temp->next = temp->next->next ;
  64.       free(helper) ;
  65.       return lst ;
  66.  
  67. }
  68.  
  69. int main(void){
  70.    struct node *l = NULL ;
  71.  
  72.    l = add_to_list(l,56) ;
  73.    l = add_to_list(l,45) ;
  74.    l = add_to_list(l,5) ;
  75.    l = add_to_list(l,2) ;
  76.  
  77.    print_list(l) ;
  78.    l = delete_from_list(l,5) ;
  79.  
  80.    print_list(l) ;
  81.    free_list(l) ;
  82.    return 0 ;
  83. }
  84.  
i tested my function on the following base cases -
if the list is empty (NULL),
contains one element 4,NULL ;
but when i try to pass a list 2,5,45,56, NULL and delete
5 it just returns the whole list

help?
i don't really know if there is a way to use only 1 pointer - so i used 2
thanks in advance
Jun 25 '11 #1

✓ answered by weaknessforcats

You have a single linked list. So if there are nodes A*,B* and C* then to delete B you must set A->next to C. That means you need to know B->prev which you don't have available. So when you walk the list to find your key you need to keep the address of the prevoius node handy.

Note that if you just delete B all that does is return the memory occuied by *B back to the free store. So if you walk your list again B will still show up as part of the list until that memory is reused and then you eill probably crash.

1 2416
weaknessforcats
9,208 Expert Mod 8TB
You have a single linked list. So if there are nodes A*,B* and C* then to delete B you must set A->next to C. That means you need to know B->prev which you don't have available. So when you walk the list to find your key you need to keep the address of the prevoius node handy.

Note that if you just delete B all that does is return the memory occuied by *B back to the free store. So if you walk your list again B will still show up as part of the list until that memory is reused and then you eill probably crash.
Jun 25 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Scott Simpson | last post by:
Can you query from a non-root node using XPathAPI's function selectNodeList(Node contextNode, java.lang.String str)? I'm trying this using the XPath expression "//*" and I'm getting nothing back. I...
6
by: Eddy C | last post by:
Hi, I'm trying to get the value of another node using the position of another node or the name of the tag. Such that the current node is one of the contacts child nodes sec or prim and doing...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
5
by: wongjoekmeu | last post by:
Hiya all, I am trying to use function pointers. I know that usually if you use a pointer to an object, you have to release the memory at the end by calling the delete keyword. I was wondering if...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
2
by: dolphin | last post by:
Can I delete a static pointer?
2
by: paragpdoke | last post by:
Hello Everyone. I'm new to XML and was trying to get my first DTD configured. I could not find an example of a constraint on the value of a node using the DTD. I'm trying to build a very simple...
11
by: amollokhande1 | last post by:
Hi All, I am having xml in following format <Form> <Panel id="1" name="t1"> <Field1/> <Field2/> </Panel>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.