Односвязный список

aside
Алгоритм реализации односвязного списка с примером на C++ и Pascal

Односвязный список (связный список) — базовая структура, представляющая собой соединение узлов с однотипными данными.

Структура записи

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
listitem :
item: type, //type - тип хранимых данных
Plink : listitem; // указатель на следующий элемент списка
listitem : item: type, //type - тип хранимых данных Plink : listitem; // указатель на следующий элемент списка
listitem : 
   item: type,       //type - тип хранимых данных
   Plink : listitem;   // указатель на следующий элемент списка 

 

Структура для работы с односвязным списком

Структура списка

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
type
singlelistilist:
first:listitem,
last: listitem,
head: listitem,
var
list:singlelistite
type singlelistilist: first:listitem, last: listitem, head: listitem, var list:singlelistite
type
  singlelistilist:
    first:listitem,
    last: listitem,
    head: listitem,
var
 list:singlelistite

Функция иницилизации

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func singlelistitem->init
list.first:= NULL;
list.last:= NULL;
list. head:= NULL;
end;
func singlelistitem->init list.first:= NULL; list.last:= NULL; list. head:= NULL; end;
func singlelistitem->init
   list.first:= NULL;
   list.last:= NULL;
   list. head:= NULL;
end;

Добавление элемента в конец списка

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func singlelistlist->addItemBefore(item:type)
i:listitem := new listitem;
i->item := item;
i->Plink := NULL;
if first = NULL then
first := i;
last := i;
end;
else
last.Plink := i;
last := i;
end;
func singlelistlist->addItemBefore(item:type) i:listitem := new listitem; i->item := item; i->Plink := NULL; if first = NULL then first := i; last := i; end; else last.Plink := i; last := i; end;
func singlelistlist->addItemBefore(item:type)
  i:listitem := new listitem;
  i->item    := item;
  i->Plink   := NULL;
  if first = NULL then
     first := i;
     last  := i;
  end;
  else
     last.Plink := i;
     last := i;
end;

Добавление элемента в начало списка

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func singlelistlist->addItemAfter(item:type)
i:listitem := new listitem;
i->item := item;
i->Plink := NULL;
if first = NULL then
first := i;
last := i;
end;
else
i->Plink := first;
first := i;
end;
func singlelistlist->addItemAfter(item:type) i:listitem := new listitem; i->item := item; i->Plink := NULL; if first = NULL then first := i; last := i; end; else i->Plink := first; first := i; end;
func singlelistlist->addItemAfter(item:type)
  i:listitem := new listitem;
  i->item    := item;
  i->Plink   := NULL;
  if first = NULL then
     first := i;
     last  := i;
  end;
  else
     i->Plink := first;
     first := i;
end;

Удаление элемента с начала списка

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func singlelistlist->deleteItemAfter
t:listItem;
t:=first;
if first <> nil then
first:=t->PLink;
first.PLink:=NULL;
delete t;
head:=first;
end;
else
list.first:= NULL;
list.last:= NULL;
list. head:= NULL;
end;
end;
func singlelistlist->deleteItemAfter t:listItem; t:=first; if first <> nil then first:=t->PLink; first.PLink:=NULL; delete t; head:=first; end; else list.first:= NULL; list.last:= NULL; list. head:= NULL; end; end;
func singlelistlist->deleteItemAfter
   t:listItem;
   t:=first;
   if first <> nil then
      first:=t->PLink;
      first.PLink:=NULL;
      delete t;
      head:=first;
    end;
    else
     list.first:= NULL;
     list.last:= NULL;
     list. head:= NULL;
    end;

end;

Удаление элемента с конца списка

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func singlelistlist->deleteItemBefore
l,d: listitem;
l := first;
while l.Plink <> NULL do
d:=l;
l:=l.Plink;
end;
d.Plink:=NULL;
dispose(l);
end;
func singlelistlist->deleteItemBefore l,d: listitem; l := first; while l.Plink <> NULL do d:=l; l:=l.Plink; end; d.Plink:=NULL; dispose(l); end;
func singlelistlist->deleteItemBefore
   l,d: listitem;
   l := first;
   while l.Plink <> NULL do
     d:=l;
     l:=l.Plink;
   end;
   d.Plink:=NULL;
   dispose(l);     
end;

Примеры реализации

Примеры реализации у меня в ребозитории на GitLab

Поделиться ссылкой: