473,804 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list program in c++

Hi can anybody send me how to create a linked list program in C++.
Basically I want to know how a node is handled in c++. I know this in c
only.

in c we create a structure and use that structure for ctreating nodes.
how is this achieved in c++ .

my c structure is:

struct node
{
int info;
struct node *link;
};

please tell me how do we do this in c++.

Rgrds
Indra

Jan 30 '06 #1
7 19171
Indraseena wrote:
Hi can anybody send me how to create a linked list program in C++.
Basically I want to know how a node is handled in c++. I know this in c
only.

in c we create a structure and use that structure for ctreating nodes.
how is this achieved in c++ .

my c structure is:

struct node
{
int info;
struct node *link;
};

please tell me how do we do this in c++.

Rgrds
Indra

#include <list>
..
..
..

there is nothing magical in C++ to implement a linked list. Sure it can
be done - possibly more elegantly and safer but essentially it is the
same algorithm no matter what the language.

If you need a solid and reliable linked list use STL std::list
--dakka
Jan 30 '06 #2
Hi,

I wanna know the class definition for the linked list. how it is
created. Will the class contain both information and addres filed in a
class or how?

class node
{
into info;
node *link;

public:
void create_list();
void display_list();
};

Is this declaration fine??

Jan 30 '06 #3
I usually create linked list like this:

struct node
{
Data m_data;
struct node* m_pNext;
};

class CList
{
public:
CList();
~CList();
void Create();
void Add(Data& osData);
void Display();
void Delete(Data& osData);
void Destroy();
private:
struct node* m_pHead;
};

The implementation will be as per your requirement...

Jan 30 '06 #4
babu wrote:
I usually create linked list like this:

struct node
{
Data m_data;
struct node* m_pNext;
};

class CList
{
public:
CList();
~CList();
void Create();
void Add(Data& osData);
void Display();
void Delete(Data& osData);
void Destroy();
private:
struct node* m_pHead;
};

The implementation will be as per your requirement...


It's probably best to use the STL implementation unless you have a
specific need that requires you to roll your own since it's more
thoroughly tested. Although it's my opinion that everyone should try
creating the data structure(s) they use at least once to better
understand how it works behind the scenes.

--
Ben Radford
"Why is it drug addicts and computer aficionados are both called users?"
Jan 30 '06 #5
In C++ you do it this way (assuming you want your own implementation
instead of the STL one):

#include <iostream>

using namespace std;

template <class T>
class List {
protected:
struct Node {
Node* next;
T val;

// Constructor
Node() : next(0) {}
Node(Node *p_next) : next(p_next) {}
Node(Node *p_next, const T& p_val) : next(p_next), val(p_val) {}
};

Node* head;
static Node c_end;

public:
struct iterator {
Node* node;

iterator(Node *p_node) : node(p_node) {
}

bool operator!=(cons t iterator& another) {
return node != another.node;
}

void operator++() {
node = node->next;
}

T operator*() {
return node->val;
}
};

List() {
head = &c_end;
}

iterator begin() {
return iterator(head);
}

iterator end() {
return iterator(&c_end );
}

void push_front(cons t T& p_val) {
head = new Node(head, p_val);
}

void insert_after(it erator p_where, const T& p_val) {
p_where.node->next = new Node(p_where.no de->next, p_val);
}
};

template <typename T> typename List<T>::Node List<T>::c_end;

int main()
{
List<int> myList;

myList.push_fro nt(9);
myList.push_fro nt(5);
myList.push_fro nt(4);
myList.push_fro nt(1);
myList.push_fro nt(3);

List<int>::iter ator it = myList.begin();

++it;
++it;

myList.insert_a fter(it, 1);

for (List<int>::ite rator i = myList.begin(); i != myList.end(); ++i) {
cout << *i << ", ";
}
cout << endl;

return 0;
}
// Output: 3, 1, 4, 1, 5, 9,

Of course you can improve it a lot, adding a pointer to the tail node,
push_back's, pointer to previous element (double linked list), etc...
and as a homework, implement a destructor so that delete is called on
every element (otherwise it is leaking memory) :)

Regards,
Edson

Jan 30 '06 #6
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"Indraseena " <mu**********@g mail.com> wrote:
Hi can anybody send me how to create a linked list program in C++.
Basically I want to know how a node is handled in c++. I know this in c
only.

in c we create a structure and use that structure for ctreating nodes.
how is this achieved in c++ .

my c structure is:

struct node
{
int info;
struct node *link;
};

please tell me how do we do this in c++.

Rgrds
Indra


<http://www.sgi.com/tech/stl/Slist.html> Gives docs and a complete
implementation of a single-linked list.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 30 '06 #7
Linked list in C++ is better to be written to be datatype agnostic:

template <datatype>
class node
{
datatype m_data;
node* m_prev; // double linked list, ofcourse :)
node* m_next;
public:
// ... blabla ...
};

This way you can support much more types, usually in C similiar thing
is written using void* and the error prone and cumbersome ways to work
with what that implies.

Btw. the code you posted, is already C++. It is just not advantageous
to write list class which stores only objects of type int, hence, the
recommendation to use templates. The standard library does this with
std::list for example, check it out, pretty neat stuff.

FWIW, I have alternative std::list -like implementation draft at
www.liimatta.org, DL fusion the implementation is include/core/list.hpp
there are some (trivial and biased!) benchmarking results in the fusion
page on various compilers (with the standard library implementations
that go with them by default) and architechtures.

Jan 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4834
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
10
2521
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The problem I get is that I am trying to link a struct that I have defined and its refusing to link. I have tried casting my struct into char * but attempts to cast it back to its original struct to access its contents only seg faults.
4
3607
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
3
3330
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like identifiers of less than 256 characters long identified in the input file F. Each identifier will be represented by a triple (I, length, string) where I is used to identify the type of
1
4165
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like identifiers of less than 256 characters long identified in the input file F. Each identifier
3
472
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
6
16136
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: struct node { int x; struct node *next; };
9
2846
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node * nPtr; //the next node's pointer }
1
15560
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can. So far I have // list.cpp
6
3268
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
0
9712
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
10595
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...
0
10343
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 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...
1
10341
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
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
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();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3
3001
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.