Jump to content

linked list

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
how do I enter a value v in a list of increasing order in the correct position, in linked list ?

Using the formulations of the list I got, but linked list not

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It will depend on the details of your linked list implementation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

It will depend on the details of your linked list implementation.

The type node is:
node = ref (
info: element "element of a node"
next: list "successor of the node or the rest of the list"
);
Example function cons: "Produces a list where v is the first element"
Algorithm:
func cons(v: element, L: list) return list;

   var aux: ref node;

    begin

         allocate(aux);

         aux^.info := v;

         aux^.next := L;

         return aux

    end;

end.


#4
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
you understand the type node?

#5
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
I do not know how:
Linked list

1) Enter a value in a certain position
2) remove an element from the beginning without eliminating
3)

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Generally, you have to scan through your list node by node until you find the correct position. Then you create a new node and update pointers.
before: "x" --> "z"

after: "x" --> "y" --> "z"

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

Generally, you have to scan through your list node by node until you find the correct position. Then you create a new node and update pointers.

before: "x" --> "z"


after: "x" --> "y" --> "z"


1) Suppose the list L in ascending order inserts the value v in its rightful place

ins_ord(L: list, v: value) return value;


How can I browse through the list, linked list, to find a value greater than v, to insert v before the value?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You need a pointer to step through your list. if temp.next.info < "y" then temp=temp.next.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

You need a pointer to step through your list. if temp.next.info < "y" then temp=temp.next.

Please look:
It is ?

func ins_ord(L: list, v: value) return value;

   var temp: ref node;

   begin

       if temp^.info < v

               then  temp^.prox = v;

       else L^.prox = v;

   end;



#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That doesn't look right. BTW, what language is this?
These C++ tutorials may help:
http://forum.codecal...sing-lists.html
http://forum.codecal...nked-lists.html
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

That doesn't look right. BTW, what language is this?
These C++ tutorials may help:
http://forum.codecal...sing-lists.html
http://forum.codecal...nked-lists.html

Not the language, is only an algorithm

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I know, but there are a LOT of algorithms you can pull out of the tutorials.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog