Добавление, удаление, редактирование, чтение списков — Pascal(Паскаль)

program course9;

type
  link = ^kom;

  kom = record
    ini: integer;
    next: link;
  end;

var
  pn: link;
  arg, j, m: integer;

procedure add(var n: link; x, num: integer);
var
  neo, ind: link;
  i: integer;
begin
  new(neo);
  neo^.ini := x;
  if n = nil then
  begin
    n := neo;
    neo^.next := nil;
  end
  else if num = 1 then
  begin
    neo^.next := n;
    n := neo;
  end
  else
  begin
    i := 0;
    ind := n;
    while (i <> num - 2) and (ind^.next <> nil) do
    begin
      i := i + 1;
      ind := ind^.next;
    end;
    neo^.next := ind^.next;
    ind^.next := neo;
  end;
end; { add }

procedure del(var n: link; num: integer);
var
  neo, ind: link;
  i: integer;
begin
  if n <> nil then
  begin
    if num = 1 then
    begin
      neo := n;
      n := n^.next;
      dispose(neo);
    end
    else
    begin
      i := 0;
      ind := n;
      while (i <> num - 2) and (ind^.next <> nil) do
      begin
        i := i + 1;
        ind := ind^.next;
      end;
      if ind^.next <> nil then
      begin
        neo := ind^.next;
        ind^.next := neo^.next;
        dispose(neo);
      end;
    end;
  end;
end; { del }

procedure veiw(n: link);
var
  ind: link;
  i: integer;
begin
  ind := n;
  i := 0;
  if ind = nil then
    writeln('List is empty')
  else
  begin
    writeln;
    writeln('The list is');
    while ind <> nil do
    begin
      i := i + 1;
      writeln(i, ')  ', ind^.ini);
      ind := ind^.next;
    end;
  end;
end; { veiw }

procedure spec(var n: link; num: integer);
var
  ind: link;
  i: integer;
begin
  i := 0;
  ind := n;
  while ind <> nil do
  begin
    i := i + 1;
    ind := ind^.next;
  end;
  if i < num then
    writeln('The list consists of only ', i, ' elements')
  else
  begin
    i := 0;
    while i <> num do
    begin
      i := i + 1;
      ind := n;
      n := n^.next;
      dispose(ind);
    end;
  end;
end; { spec }

begin
  pn := nil;
  m := 1;
  while m <> 0 do
  begin
    writeln;
    writeln;
    writeln('Type "1" to ADD new element to the list');
    writeln('Type "2" to DELETE element from the list');
    writeln('Type "3" to VEIW the list');
    writeln('Type "4" to DELETE first k-elements');
    writeln('Type "0" to exit program');
    writeln;
    readln(m);
    case m of
      1:
        begin
          write('Enter new element : ');
          readln(arg);
          write('Enter place : ');
          readln(j);
          add(pn, arg, j);
        end;
      2:
        begin
          write('Enter number of the element : ');
          readln(j);
          del(pn, j);
        end;
      3:
        begin
          veiw(pn);
        end;
      4:
        begin
          writeln('Enter number of elements : ');
          readln(j);
          spec(pn, j);
        end;
    end; { case }
  end;

end.

Leave a Comment