Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with deleting a node in doubly-linked list. Help me plz) Posted by butter_fly on 18 Nov 2012 at 2:21 AM
Hey :P
Sorry for this silly questions, but I'm only beginner :D
I've written a program which suppose to work, but it doesn't :'(
Anyway. It should create doubly-linked list. And it should compress it.
It should leave only one element from few identical ones.


the data file looks like
1
2
2
2
5
6
7
7
7
0
1
2
3
4
5
6
7
18
19


And this is the code:
program silly;

type
        ListType=^ElementType;
        ElementType = record
            next:ListType;
            data:integer;
            prev:ListType;
            end;
var something,Cursor,TheFirst:ListType;
    f:text;
    i:integer;
procedure DeleteThoseSillyIdenticalElements();
    begin
        Cursor:=TheFirst;
        while Cursor^.next<>nil do
            begin
                writeln(Cursor^.data);
                    if Cursor^.data=Cursor^.next^.data then
                        begin
                            if Cursor^.next<>nil then
                                Cursor^.next^.prev:=Cursor^.prev;
                            if Cursor^.prev<>nil then
                                Cursor^.prev^.next:=Cursor^.next;
                            dispose(Cursor);

                        end;
                Cursor:=Cursor^.next;

            end;
    end;
begin
assign(f,'a.txt');
reset(f);
  new(something);
  something^.data:=4;
  something^.prev:=nil;
  something^.next:=nil;
  
  for i:=1 to 19 do
    begin
        New(Cursor);
        Readln(f,Cursor^.data);
        Cursor^.prev:=something;
        Cursor^.next:=nil;
        something^.next:=Cursor;
        if i=1 then TheFirst:=Cursor;
        something:=Cursor;
    end;
DeleteThoseSillyIdenticalElements();
  close(f);
  readln();
end.


It says that there is General Protection Fault
It always happens like this :(




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.