3 Replies - 315 Views - Last Post: 18 April 2011 - 08:14 AM Rate Topic: -----

#1 usamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-April 11

i have some problem in a doubly linked list in C++

Posted 17 April 2011 - 12:37 PM

I have written a simple code for a doubly linked list. Here is the code:
#include <iostream>
#include<stdlib.h>
using namespace std;
struct linkedlist{
	linkedlist *next;
	int data;
	linkedlist *previous;
}*p,*q,*r;
linkedlist *getNode(){
	linkedlist *l = new linkedlist;
	return l;
}
void createNode(){
	if(p==NULL){
	p = new linkedlist;
	p->previous = NULL;
	p->next = NULL;
	cout<<"Please enter the data in the list\n";
	cin>>p->data;
	}
	else{
		cout<<"You have already created a node. To add a new node to the list, select the correct option from the main menu\n";
	}
}
void deletelist(){
	if(p==NULL){
		cout<<"You dont have a node, please create a node to proceed\n";
	}
	else{
		q=p;
		while(q!=NULL){
			q = q->next;
		}
		r = q;
		free(q);
		
	}
}
void printlist(){
	if(p==NULL){
		cout<<"You havent created any node, please create a node to proceed with this action\n";
	}
	else{
		r=p;
		while(r->next=NULL){
			cout<<"\t"<<r->data;
			r=r->next;
		}
		cout<<"\t"<<r->data;
		cout<<"\n";
	}
}
int main(){
	p=NULL;
	int choice;
	while(1){
		cout<<"This is the main menu:\n";
		cout<<"To create a node, press 1\n";
		cout<<"To add a node to the existing list, press 2\n";
		cout<<"To delete a node, press 3\n";
		cout<<"To print the list, press 4\n";
		cout<<"To exit, press 5\n";
		cin>>choice;
		switch(choice){
			case 1: createNode();
				break;
			case 2: if(p==NULL){
				cout<<"There is no node in the linkedlist, please create a new node\n";
				}
				else{
					int a;
					r=getNode();
					cout<<"Please enter the data in the node\n";
					cin>>a;
					r->data = a;
					p->next=r;
					r->previous = p;
					r->next = NULL;
				}
				break;
			case 3: deletelist();
				break;
			case 4: printlist();
				break;
			case 5: return 0;
				break;
			default: cout<<"You have entered an invalid option, please re-enter your option\n";
		}
	}
}

Now the problem is that whenever i add a new data in the existing linked list, and then print it, it doesn't print the value except for the first data i have entered. What mistake have i done?

Is This A Good Question/Topic? 0
  • +

Replies To: i have some problem in a doubly linked list in C++

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: i have some problem in a doubly linked list in C++

Posted 17 April 2011 - 12:45 PM

while(r->next=NULL){

this is assigning 0 to r->next.
maybe you want this :
while(r!=NULL){


Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: i have some problem in a doubly linked list in C++

Posted 17 April 2011 - 01:09 PM

A few comments:

Your program is mixing the new operator with free(). Since 'free' does not destroy obejcts, what you've got is undefined behaviour (This is bad). A call to new should have a corresponding call to delete in order to make sure that dynamically allocated objects are correctly destroyed before the memory is deallocated.

The opposite of the 'free' function is 'malloc'. both of these are old C functions which should not be used in C++, because they've been replaced by new/delete (They only exist to support legacy C code).


Also, I suggest you avoid global variables since they're unnecessary here, and there's no reason why you can't create p, q and r locally somewhere then pass them as function arguments as needed. This should help the flow of your program. You might also consider using a struct to store both the head and tail of your linked list together and pass that to your functions by-reference.

(Try using meaningful names aswell. single-character names do not make your code easy to understand. Strong nouns such as "head" and "tail" might be more meaningful for a linked list)

This post has been edited by Bench: 17 April 2011 - 01:14 PM

Was This Post Helpful? 2
  • +
  • -

#4 usamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-April 11

Re: i have some problem in a doubly linked list in C++

Posted 18 April 2011 - 08:14 AM

View Postsk1v3r, on 17 April 2011 - 12:45 PM, said:

while(r->next=NULL){

this is assigning 0 to r->next.
maybe you want this :
while(r!=NULL){


thank you so much. I did not note such an easy mistake as i am new to programming. I hope i can learn a lot more from you people.

View PostBench, on 17 April 2011 - 01:09 PM, said:

A few comments:

Your program is mixing the new operator with free(). Since 'free' does not destroy obejcts, what you've got is undefined behaviour (This is bad). A call to new should have a corresponding call to delete in order to make sure that dynamically allocated objects are correctly destroyed before the memory is deallocated.

The opposite of the 'free' function is 'malloc'. both of these are old C functions which should not be used in C++, because they've been replaced by new/delete (They only exist to support legacy C code).


Also, I suggest you avoid global variables since they're unnecessary here, and there's no reason why you can't create p, q and r locally somewhere then pass them as function arguments as needed. This should help the flow of your program. You might also consider using a struct to store both the head and tail of your linked list together and pass that to your functions by-reference.

(Try using meaningful names aswell. single-character names do not make your code easy to understand. Strong nouns such as "head" and "tail" might be more meaningful for a linked list)

Thank you so much sir. I am new to programming and have less knowledge regarding readability and the need to declare variables globally or non global. I am extremely thankful that you bothered to read my program and pointed out these mistakes. I hope i can learn a lot from you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1