Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Simple Circular Linked List

c++ linked list

  • Please log in to reply
1 reply to this topic

#1 unknown24


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C++
  • Learning:C++

Posted 09 May 2012 - 04:52 PM

Hallo guy,,, pls help me , do you have a simple example of single linked list circular in C++?
i search in google, it has acomplicated and long example of single linked list circular...
I hope someone reply me,,,,Thx
  • 0

#2 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 09 May 2012 - 07:52 PM

Do you know what is linked list is? What is single way / double way linked list?

First let me help you to understand with an example what is linked list is -- Suppose your 3 friends (including you) gather together in a place. Now you stand first in a line (so your id is 1). Then one of your friends (id is 2) stands in front of you facing his back to you. Now you will put your right hand on his shoulder. Similarly the last friend stand in front of friend whose id is 2 facing his back to him. Friend with 2 will put his right hand on shoulder friend with id 3. This is the single way (or one way) linked list. You are the root of the link list, your friend with id 2 is linked to you through your right hand, similarly your friend with id 3 is linked to the friend with id 2 through his right hand. And no one is linked to the last friend (with id 3). Now if you put your left hand too on the shoulder of the friend next to you (id 2) and if this means you are linked to him through your left hand. Similarly friend with id 2 will put his left hand on the shoulder of the friend with id 3 and if this means friend with id 2 is linked to friend with id 3 through friend with id 2's left hand. This is double -way linked list.

Now what is single way circular linked list?
In our 'single way' linked list example, if somehow you can be linked to the last friend (with id 3), then the linked will be a circular single way linked list. So you 3 can make a triangle so that your right hand is on shoulder of friend with id 2. Friend with id 2's right hand is on shoulder of friend with id 3 and friend with id 3's right hand is on you. This is circular single way linked list.

Please read all of them from http://en.wikipedia....iki/Linked_list.

Now let us see in code how to create single way linked list.
First we need to define the structure for the linked list.

struct __OneWayLinkedList__ {
int id;
struct __OneWayLinkedList__ *next;

typedef struct __OneWayLinkedList__ OWLL;

Now let us create the single way linked list we talked above.
void createOneWayLinkedList()
// The first node which is root is you.
OWLL* you = (OWLL*)malloc(sizeof(OWLL));
you->id = 1;

// create second friend
OWLL* secondFriend = (OWLL*)malloc(sizeof(OWLL));
secondFriend->id = 2;

// linked second friend to you.
you->next = secondFriend;

// create third friend
OWLL* thirdFriend = (OWLL*)malloc(sizeof(OWLL));
thirdFriend->id = 3;

// linked third friend to second friend.
secondFriend->next = thirdFriend;

// Now link no body to the 3'rd friend
thirdFriend->next = 0;

To create circular single way linked list from the above code, you just need to link your self to the friend with id 3.
// Now link yourself to third friend.
thirdFriend->next = you;

I hope you will be able to create now double way linked list as well as circular double way linked list.

Note that, I haven't compiled the code against any compiler but I just typed here in the forum editor.
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download