473,503 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding node from the back in a linked list and deleting a node

4 New Member
Hello,

am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program

Expand|Select|Wrap|Line Numbers
  1. void del(node **record)
  2. {
  3. char name[20];
  4.  
  5. node *current, *previous;
  6.  
  7. current = *record;
  8.  
  9.  
  10. cout<<"\n\nEnter name to delete: ";
  11. cin.getline(name,20);
  12.  
  13.  
  14. if ( strcmp(current->name,name) == 0) //if the target is the first node
  15. {
  16. *record = current->nextadd;
  17.  
  18. delete(current);
  19. }
  20.  
  21. else
  22. {
  23.  
  24.  
  25. while ( strcmp(current->name,name) != 0)
  26. {
  27. previous = current;
  28. current = current->nextadd;
  29.  
  30. }
  31.  
  32. previous->nextadd = current->nextadd;
  33. cout<<"\n\nDeleted";
  34. delete(current);
  35. }
Mar 22 '07 #1
4 2835
sicarie
4,677 Recognized Expert Moderator Specialist
Hello,

am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program
dabbakal-

Glad to hear we've helped, and even more happy to have you with us and contributing! As per our FAQ, I removed the portions of code that were not relevant to your questions. Also, if you could use code tags, it would be appreciated!

2. May I ask you to post what methods you have tried to create for the insertBack(node) ?
Mar 22 '07 #2
dabbakal
4 New Member
dabbakal-

Glad to hear we've helped, and even more happy to have you with us and contributing! As per our FAQ, I removed the portions of code that were not relevant to your questions. Also, if you could use code tags, it would be appreciated!

2. May I ask you to post what methods you have tried to create for the insertBack(node) ?
I do not know any other method because am following a text book. As i have said earlier am having limited resources and am just doing this on my own so i was frusturated and discourage when i couldn't solve it. I decided to stop but i thought of this forum. Hope someone will help me make it work so that my interest will not die.
Mar 23 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
I do not know any other method because am following a text book. As i have said earlier am having limited resources and am just doing this on my own so i was frusturated and discourage when i couldn't solve it. I decided to stop but i thought of this forum. Hope someone will help me make it work so that my interest will not die.
Well, we can't write your code for you, but I can say that you can look at the method to add to the front and use that as a guide.

As for the delete problem, you use record, current, previous, and nextadd as variables interchangeably (or so it seems to me). What are 'nextadd' and 'record'?
Mar 23 '07 #4
dabbakal
4 New Member
Well, we can't write your code for you, but I can say that you can look at the method to add to the front and use that as a guide.

As for the delete problem, you use record, current, previous, and nextadd as variables interchangeably (or so it seems to me). What are 'nextadd' and 'record'?
I appreciate your replies and responses but i just want to clarify a little bit. I am a beginner and still struggle with way to write the codes. I have a book that define the algorithm but i cannot implement it. That is why i posted it here.

Secondly, the variables refered to wouldn't have cause any difficulty in understanding if the whole program were there, but it was edited and only the functions part were left. Therefore I will post it again.

I still look up to someone who can help me with the coding part because to the best of my ability that is the far i can reach.

#include <iostream.h> //Creating a dynamic linked list
#include <iomanip.h>

#include <string.h>
#include <stdlib.h>

struct node {

char name[20];
int id_num;
node *nextadd;
};

void insert(node **); //function prototype
void display( node * );
void del(node **); //function prototype


node *list;

main()
{
int i,option;
char ans;
bool cont = true;
//two pointers to structure



//get a pointer to the first structure

list = NULL;





//insert the current structure and create the remaining structures
do {
system("cls");
cout<<"\n\n1 : Add record";
cout<<"\n\n2 : Delete record";
cout<<"\n\n3 : Display record";
cout<<"\n\n4 : Exit";

cout<<"\n\n\nEnter your option: ";
cin>>option;
cin.get();

switch (option) {

case 1: insert(&list);
cont = true;
break;
case 2: del(&list);
cont = true;
break;
case 3: display(list);
cont = true;
break;
case 4: exit(1);

cont = false;
};

}while(true);

}



void insert(node **list) //record is pointer to a structure
{

node *newrec = new node;


cout<<"Enter the a name: "; //inserting infronth of a list
cin.getline(newrec->name,20);
cout<<"Enter the id number: ";
cin>>newrec->id_num;
cin.get();

newrec->nextadd = NULL;


if( list== NULL)

*list = newrec;

else
{
newrec->nextadd = *list;
*list = newrec;
}
}



void display(node *contents)

{

if (contents == NULL)
cout<<"\n\nList is empty"<<endl;
else
{

while(contents != NULL)
{
cout<<setw(30)<<contents->name
<<setw(20)<<contents->id_num<<endl;
contents = contents->nextadd;
}
}

cin.get();
}



void del(node **record)
{
char name[20];

node *current, *previous;

current = *record;


cout<<"\n\nEnter name to delete: ";
cin.getline(name,20);


if ( strcmp(current->name,name) == 0) //if the target is the first node
{
*record = current->nextadd;

delete(current);
}

else
{


while ( current && ( strcmp(current->name, name) != 0 ) )
{
previous = current;
current = current->nextadd;

}

previous->nextadd = current->nextadd;
cout<<"\n\nDeleted";
delete(current);
}
}
Mar 24 '07 #5

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

Similar topics

10
5457
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
5
4244
by: CR | last post by:
I've been to figure out how to get AddSortLast function to add nodes in acsending order (a b c d) I figured it out how to get it to sort in decending order but I can't get it to sort in acsending...
4
3916
by: Sumika | last post by:
Hello, I'm a newbie here, so don't know much friends. I've problem deleting my node at the tail, so could you all help me to solve my error,I worked on it for quite sometime but it just can't...
13
6533
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
9
4032
by: Daniel Vukadinovic | last post by:
I want to implement an index/counter to my linked list. Why? I wrote a search function which searches the list for elements based on their values (say I add an element and I assign the value 54...
1
6551
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream>...
11
5608
by: hotice | last post by:
How to write code to delete a specific node in a single link list that takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the time deleting a node is the same (independent from...
42
4506
by: Avon | last post by:
Hello there. I have a problem regarding node deletion in singly-linked lists in C. What happens is that I can't hold the previous node efficiently. Here is the code I use. struct lista *search...
60
2626
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
0
7204
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
7091
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...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.