Hi..Im writting a doubly link list in c++,compiled with Turbo c++, but my Add_at_first method seems not working,i couldnt find any problem in the code...
here is the code : -
#include <iostream.h>
-
class node
-
{ friend class linklist;
-
public : int num;
-
node *next;
-
node *prev;
-
};
-
class linklist{
-
public :
-
void add_at_first();
-
-
private :
-
node *first;
-
node *last;
-
};
-
//*******Add At First**
-
void linklist :: add_at_first()
-
{
-
node *temp;
-
temp = new node();
-
temp ->prev=temp ->next=NULL;
-
cout <<"Please Enter A Number :"<< endl;
-
cin >> temp -> num;
-
-
if(first ==last==NULL)// if the list is empty
-
{
-
first = last=temp;
-
}
-
else
-
{
-
first -> prev =temp;
-
temp -> next = first;
-
first = temp;
-
}
-
}
waitting for ur guidance, Thanksss..
4 1948 Ganon11 3,652
Recognized Expert Specialist
Well, in a doubly-linked list, you probably shouldn't need last - to find the last node, you code just use first->prev.
Next, if the list is not empty, you are adjusting first and temp - but you will need to change last (or, if you follow the first example, first->prev) to point forward to temp.
Banfa 9,065
Recognized Expert Moderator Expert
Well, in a doubly-linked list, you probably shouldn't need last - to find the last node, you code just use first->prev.
Only true if your list is circular as well as doubly linked.
Ganon11 3,652
Recognized Expert Specialist
Only true if your list is circular as well as doubly linked.
...right. I keep forgetting to distinguish the two.
Banfa 9,065
Recognized Expert Moderator Expert
...right. I keep forgetting to distinguish the two.
Ah, not all four legged animals are cows :D
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: D. Beckham |
last post by:
I wrote the following code to create a short doubly-link list of three
strings: "one", "two", and "three". I would like to know if I set them up
correctly, so that they point to one another.
DLLNode class has already been created. Basically, "one" is the head node,
"two" is the mid node, and "three" is the tail or the last node. I would
like to know if my algorithm for setting up doubly-linked node are correct
before I proceed to...
|
by: Jatinder |
last post by:
I found these questions on a web site and wish to share with all of u
out there,Can SomeOne Solve these Porgramming puzzles.
Programming Puzzles
Some companies certainly ask for these things. Specially Microsoft.
Here are my favorite puzzles. Don't send me emails asking for the
solutions.
|
by: surrealtrauma |
last post by:
I want to ask what's the differences between doubly liked list and linear
liked list, and also the circular doubly liked list in terms of
implementation. THX
|
by: dssuresh6 |
last post by:
Whether browsing forward or backward can be done using a singly linked
list. Is there any specific case where a doubly linked list is needed?
For people who say that singly linked list allows traversal only in one
direction, I would say that using appropriate loops/recursion, traversal
in opposite direction is also possible. Then why the need for doubly
linked list?
--
|
by: sudhirlko2001 |
last post by:
How to swap two nodes of doubly Linklist
| |
by: free2cric |
last post by:
Hi,
how to detect head and tail in cyclic doubly link list ?
Thanks,
Cric
|
by: drewy2k12 |
last post by:
Heres the story, I have to create a doubly linked list for
class, and i have no clue on how to do it, i can barely create a
single linked list. It has to have both a head and a tail pointer,
and each node in the list must contain two pointers, one pointing
forward and one pointing backwards.
Each node in the list will contain 3 data values: an item ID
(string), a quantity (integer) and a price (float). The ID will
contain only letters and...
|
by: maruf.syfullah |
last post by:
Consider the following Class definitions:
class AClass
{
int ai1;
int ai2;
public:
CClass* c;
AClass(){}
|
by: adam.kleinbaum |
last post by:
Hi there,
I'm a novice C programmer working with a series of large (30,000 x
30,000) sparse matrices on a Linux system using the GCC compiler. To
represent and store these matrices, I'd like to implement the sparse
matrices as a doubly-linked list, in which each non-zero cell is
stored roughly as follows:
int rownum
int colnum
|
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...
|
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,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |