473,657 Members | 2,397 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 2841
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\nEn ter 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(new rec->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)<<con tents->id_num<<endl ;
contents = contents->nextadd;
}
}

cin.get();
}



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

node *current, *previous;

current = *record;


cout<<"\n\nEnte r name to delete: ";
cin.getline(nam e,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\nDele ted";
delete(current) ;
}
}
Mar 24 '07 #5

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

Similar topics

10
5486
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 following: struct Node { int integer_value;
5
4254
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 order by flipping the < sign to a > sign like I thought I could. Any suggestions or improvements would be greatly appreciated. typedef struct /* file data structure */ { string_f firstName; string_l lastName;
4
3922
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 work. Thanks The following is my code: private void btnDeleteRear_Click(object sender, System.EventArgs e)
13
6555
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
4040
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 and the next element 789 and so on...) now I'd like to write a search function which would search the list for elements based on their index. Say first element is 54 and index 1, second element is 789 and index 2 so when I want to search, I'll do...
1
6559
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> #include "stdafx.h" using namespace std; class IntNode
11
5631
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 the length of the list. Show your c/c++ source code.
42
4545
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 (struct lista *list, int m, char *s, struct lista *previous) ......... Not problem-related code goes here....
60
2679
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 of these values and then average them in another column.
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
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 we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.