Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Data Structures -- Linked List


16 replies to this topic

#11 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 20 October 2008 - 05:54 AM

running sample program using stack implementing link list
Data Structures -- Linked List

Hi to all!...Gud day!...
I'm new to this site, but I find it
Interesting to find out the
Opinion of others, specially regarding
Programming...

I really needed someone to
Help me with this,...
I've got to have a sample
Running program of "STACK IMPLEMENTING LINK LIST",
Even just a simple one!
I'm badly in need of that!!...
Pls...

Hope I can find solutions
To my probs through this site!!...

I'll be waiting for some reply through here
Or you can email me @ leane_crazycute18 at yahoo.Com
Thank you and God bless!

-question by leane

#12 vivek.m

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 24 November 2008 - 02:57 PM

View Postvaralu, on Oct 27 2007, 09:09 AM, said:

@sanjay0828
Good one... the expected solution.

Again... i want a simpler solution. What will be the basic answer that comes to your mind if i give this problem.??
Very basic.. :) ..

If no answer i will be posting it in my next post.

the most basic and the only answer that came to my mind (actually, my frnd's mind. I am too laazy to use mine)  is to find length and then to find the (l-n)th element with o(n2) complexity.


i am interested in your answer.. it's more than a month since you posted your question.  :)

#13 varalu

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 468 posts
  • Gender:Male
  • Location:India
  • Interests:1) Web Technologies, DBMS, Data Warehousing and mining.<br />2) Basketball, football, athletics<br />3) Movies<br />4) Short stories<br />5) Online...friends...forums...blogging... etc. [Anything online]<br />6) Enjoyment matters
  • myCENT:1.17

Posted 24 November 2008 - 05:19 PM

View Postvivek.m, on Nov 24 2008, 08:27 PM, said:

the most basic and the only answer that came to my mind (actually, my frnd's mind. I am too laazy to use mine)  is to find length and then to find the (l-n)th element with o(n2) complexity.
i am interested in your answer.. it's more than a month since you posted your question.  :)

Ok.. here is my answer...

This is similar to finding the middle element in the link list with slight variation..

1. Have two pointer initially ( first and second )
2. move the second pointer alone 5 nodes.
3. Now move both the pointers one node at a time until the second reaches the end.
when the second reaches the end , the first node will be the 5th last element..


node * first ,*second;

first = second = root;

// moves the second ptr 5 nodes..
int i =0;
while ( (i < 5) && (second != NULL) )
{
	second = second-> next;
	i++;
}

while( second->next != NULL )
{
	   first = first->next;
	   second = second->next;
}

return (first) ; // first ptr is the 5th node from the last..

The above solution is same as the one Sanjay gave. This algorithm can be even generalized to find the kth node from the last similarly....

But the answer that i expected is below..

Just scan the list twice.
You can get the kth element from the last.


This algorithm is known as hare and tortoise algorithm.

#14 vivek.m

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 25 November 2008 - 04:51 AM

Quote

But the answer that i expected is below..
Just scan the list twice.
You can get the kth element from the last.


This algorithm is known as hare and tortoise algorithm.


That's same as what I said... In first scan, find the length and in second scan, find the required element. This seems to be only tortoise-tortoise scan.
The other answer (your and sanjay's answer) is more efficient with worst case operations half of what is required in first answer. complexity will be o(n) in both cases. (my bad. i had to edit. i mentioned complexity o(n^2) incorrectly.)

Thanks for the problem.

Edited by vivek.m, 25 November 2008 - 02:13 PM.


#15 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 28 August 2009 - 12:49 PM

main class problem..Data Structures -- Linked List

hello..It's my first time here to visit..I want to ask for help in making a main class of my linked list in queue..Here's what is asking for:

  If the input satisfies partial ordering, then the algorithm will terminate when theQueue is empty– If a loop exists, it will also terminate but will not output the elements in theLoop. Instead, it will just inform the user that a loop is present:

 

here's the code:

interface Queue{   /* Insert an item */   void enqueue(Object item) throws QueueException;   /* Delete an item */   Object dequeue() throws QueueException;}Class QueueException extends RuntimeException{   public QueueException(String err){     super(err);   }}

 

another file:

class LinkedQueue implements Queue{   Node front, rear;   /* Create an empty queue */   LinkedQueue(){   }   /* Create a queue with node and initially */   LinkedQueue(Node and){     front = and;     rear = and;   }   /* Inserts an item onto the queue */   public void enqueue(Object item){     Node and = new Node(item, null);     if (front == null) {       front = and;       rear = and;     }     else{       rear.Link = and;       rear = and;     }   }   /* Deletes an item from the queue */   public Object dequeue() throws QueueException{     Object x;     if (front == null) throw new QueueException             ("Retrieving from an empty queue.");       x = front.Info;     front = front.Link;     return x;   }   /* Main method to test the queue */   public static void main(String args[]){     LinkedQueue q = new LinkedQueue();     for (int I=11; I>0; I--){       q.Enqueue(new Integer(I));       System.Out.Println(I +" inserted");     }     for (int I=12; I>0; I--)       System.Out.Println(q.Dequeue() + " retrieved");   }}

thank you...God Bless 

-reply by cathelyn

#16 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 14 January 2010 - 01:06 AM

import java.Util.LinkedList;public class LinkedListDelete {  public static void main(String args[]) {   LinkedList<String> ll = new LinkedList<String>();   ll.Add("B");   ll.Add("C");   ll.Add("D");   ll.Add("E");   ll.Add("F");   ll.AddLast("Z");   ll.AddFirst("A");   ll.Add(1, "A2");   System.Out.Println("Original contents of ll: " + ll);   ll.Remove("F");   ll.Remove(2);   System.Out.Println("Contents of ll after deletion: " + ll);   ll.RemoveFirst();   ll.RemoveLast();   System.Out.Println("ll after deleting first and last: " + ll);   String val = ll.Get(2);   ll.Set(2, val + " Changed");   System.Out.Println("ll after change: " + ll);  }}



#17 Guest_b.manohar_*

  • Guests

Posted 17 August 2010 - 11:40 AM

hai incase of doubly circle linked list we can traverse from startptr and endptr so if two traversals are done same time we can get the element in o(n/2) complexity
algorithm is

startptr=head->llink;
endptr= head->rlink;
while(startptr!=endptr)
{
if(head->llink!=NULL&&head->rlink!=NULL)
{
startptr=startptr->next;
endptr= endptr->llink;
while(endptr->llink!=endptr->rlink)
{
if(endptr->info==key)
{
printf("key is found",endptr->info);
}
else
{
endptr=endptr->llink;

if(endptr->llink->llink!=NULL)
{
printf("element is found %d"endptr->next->info);
}

startptr=startptr->rlink;
endptr= endptr->llink;
while(startptr->llink!=startptr->rlink)
{
if(startptr->info==key)
{
printf("key is found",startptr->info);
}
else
{
startptr=startptr->rlink;

if(startptr->rlink->rlink!=NULL)
{
printf("element is found %d",statptr->rlink->info);
}


hai buddy i think if you execute this code you can find 5 th element in o(n/2) complexity
but the linked list must be circularlinkedlist thanks

bye




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users