Startpunkten
This commit is contained in:
242
Linked_List/clasfile.cpp
Normal file
242
Linked_List/clasfile.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
/***************************************************/
|
||||
/* IMPLEMENTERINGSFIL FVR LAB 0 I PUMA */
|
||||
/***************************************************/
|
||||
|
||||
#include "lab_0.h"
|
||||
|
||||
struct listNode
|
||||
{
|
||||
listItemType listContent;
|
||||
ptrType next;
|
||||
};
|
||||
|
||||
/***************************************************/
|
||||
/*SDTTER LISTSTORLEKEN TILL NOLL */
|
||||
/***************************************************/
|
||||
listClass::listClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
listClass::listClass(const listClass &L):
|
||||
size(L.size)
|
||||
{
|
||||
if (L.head == NULL)
|
||||
head = NULL;
|
||||
else
|
||||
{
|
||||
head = new listNode;
|
||||
assert(head != NULL);
|
||||
head ->listContent = L.head -> listContent;
|
||||
ptrType newPrev = head;
|
||||
for (ptrType origCur = L.head -> next ; origCur != NULL ; origCur = origCur -> next)
|
||||
{
|
||||
newPrev -> next = new listNode;
|
||||
assert(newPrev -> next != NULL);
|
||||
newPrev = newPrev -> next;
|
||||
newPrev -> listContent = origCur -> listContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*GVR INGET, MEN MEN ANROPAS NDR LISTAN DVR */
|
||||
/***************************************************/
|
||||
listClass::~listClass()
|
||||
{
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM LISTAN DR TOM */
|
||||
/***************************************************/
|
||||
int listClass::listIsEmpty()
|
||||
{
|
||||
return int (size == 0);
|
||||
}
|
||||
|
||||
int listClass::listLength()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
ptrType listClass::ptrTo(int position)
|
||||
{
|
||||
if ((position < 1) || (position > listLength()))
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
ptrType trav = head;
|
||||
for (int skip = 1 ; skip < position ; skip++)
|
||||
trav = trav -> next;
|
||||
return trav;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*LDGGER TILL ETT NYTT ELEMENT I LISTAN */
|
||||
/*precondition: att listelementet dr ett heltal*/
|
||||
/* precondition: att listelementet inte finns */
|
||||
/* postcondition: listan har ett nytt element */
|
||||
/***************************************************/
|
||||
void listClass::listInsert(listItemType newItem)
|
||||
{
|
||||
int newPosition=1;
|
||||
int newLength = listLength() + 1;
|
||||
{
|
||||
size = newLength;
|
||||
ptrType newPtr = new listNode;
|
||||
if (int(newPtr != NULL))
|
||||
{
|
||||
newPtr -> listContent = newItem;
|
||||
if(newPosition == 1)
|
||||
{
|
||||
newPtr ->next = head;
|
||||
head = newPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptrType prev = ptrTo(newPosition-1);
|
||||
newPtr -> next = prev -> next;
|
||||
prev -> next = newPtr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*TAR BORT ELEMENT I LISTAN */
|
||||
/*precondition: att positionen finns */
|
||||
/* precondition: att positionen dr ett heltal */
|
||||
/* precondition*: att positionen >0 */
|
||||
/*postcondition: elementet har tagits bort */
|
||||
/***************************************************/
|
||||
void listClass::listDel(listItemType position)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
if ((position >=1) && (position <= listLength()))
|
||||
{
|
||||
size--;
|
||||
if (position == 1)
|
||||
{
|
||||
cur = head;
|
||||
head = head -> next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptrType prev = ptrTo(position-1);
|
||||
cur = prev -> next;
|
||||
prev -> next = cur ->next;
|
||||
}
|
||||
cur -> next = NULL;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SKRIVER UT LISTAN PE SKDRMEN */
|
||||
/*precondition: TRUE */
|
||||
/*postcondition: listan har skrivits ut */
|
||||
/***************************************************/
|
||||
void listClass::listDisplay()
|
||||
{
|
||||
|
||||
cout << "Elementen dr: \n";
|
||||
for(ptrType cur = head ; cur != NULL ; cur = cur->next)
|
||||
cout << cur->listContent << endl;
|
||||
getch();
|
||||
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SORTERAR LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/*postcondition: elementen ligger i ordning */
|
||||
/***************************************************/
|
||||
void listClass::listSort()
|
||||
{
|
||||
listItemType temp;
|
||||
int bytt;
|
||||
|
||||
do
|
||||
{
|
||||
bytt=FALSE;
|
||||
for(ptrType cur=head ; cur->next != NULL ; cur = cur->next)
|
||||
if(cur->listContent > cur->next->listContent)
|
||||
{
|
||||
temp = cur->listContent;
|
||||
cur->listContent = cur->next->listContent;
|
||||
cur->next->listContent = temp;
|
||||
bytt=TRUE;
|
||||
}
|
||||
}while(bytt);
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SVKER EFTER ETT ELEMENT I LISTAN */
|
||||
/* precondition: att svkta vdrdet dr ett heltal */
|
||||
/***************************************************/
|
||||
void listClass::listSeek(listItemType soktVarde)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == soktVarde)
|
||||
{
|
||||
cout <<"Elementet fanns med!";
|
||||
getch();
|
||||
return;
|
||||
}
|
||||
cout <<"Sorry, det fanns inte...";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM ETT ELEMENT FINNS */
|
||||
/*precondition: att elementet dr ett heltal */
|
||||
/*postcondition: returnerar FALSE om ej finns */
|
||||
/***************************************************/
|
||||
int listClass::newDuplicate(newItem)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == newItem)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KONTROLLERAR OM DET FINNS ELEMENT I LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/***************************************************/
|
||||
void listClass::listIsItEmpty()
|
||||
{
|
||||
if(head == NULL)
|
||||
cout <<"Listan dr tom..." << endl;
|
||||
else
|
||||
cout <<"Det finns element i listan...";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*VISAR ANTALET ELEMENT I LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/***************************************************/
|
||||
void listClass::listAntal()
|
||||
{
|
||||
cout <<"Det finns " << size << " element i listan.";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KONTROLLERAR OM EN POSITION FINNS */
|
||||
/*precondition: att positionen dr ett heltal */
|
||||
/***************************************************/
|
||||
int listClass::posIsOk(int position)
|
||||
{
|
||||
if((position <= size) && (position > 0))
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
Reference in New Issue
Block a user