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;
|
||||
}
|
||||
47
Linked_List/lab_0.h
Normal file
47
Linked_List/lab_0.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*************************************************/
|
||||
/*HEADERFIL LABO.H */
|
||||
/*************************************************/
|
||||
|
||||
#ifndef _lab0_
|
||||
#define _lab0_
|
||||
#include <stddef.h> //för NULL
|
||||
#include <assert.h> //för assert()
|
||||
#include <iostream.h> //för in-ut matning
|
||||
#include <conio.h> //för getch();
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
|
||||
const int MAXLISTA = 20;
|
||||
const int BUFFERSIZE = 100;
|
||||
const int FALSE=0;
|
||||
const int TRUE=!FALSE;
|
||||
|
||||
typedef int listItemType;
|
||||
struct listNode;
|
||||
typedef listNode *ptrType;
|
||||
|
||||
class listClass
|
||||
{
|
||||
public:
|
||||
listClass();
|
||||
~listClass();
|
||||
listClass(const listClass &L);
|
||||
int listIsEmpty();
|
||||
int listLength();
|
||||
int posIsOk(position);
|
||||
int newDuplicate(newItem);
|
||||
void listInsert(newItem);
|
||||
void listDel(position);
|
||||
void listSort();
|
||||
void listDisplay();
|
||||
void listSeek(soktVarde);
|
||||
void listIsItEmpty();
|
||||
void listAntal();
|
||||
private:
|
||||
ptrType ptrTo(int position);
|
||||
int size;
|
||||
ptrType head;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
102
Linked_List/mainfile.cpp
Normal file
102
Linked_List/mainfile.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/***************************************************/
|
||||
/* LAB 0 I PUMA */
|
||||
/* CRILLE & ROBIN 971114 */
|
||||
/***************************************************/
|
||||
|
||||
#include "lab_0.h"
|
||||
|
||||
/***************************************************/
|
||||
/*SÄKER INMATNING */
|
||||
/***************************************************/
|
||||
int mataIn()
|
||||
{
|
||||
char buffer[BUFFERSIZE];
|
||||
int f;
|
||||
|
||||
do {
|
||||
cin >> buffer;
|
||||
if (strcmp(buffer,"0") == 0)
|
||||
return 0;
|
||||
else {
|
||||
f = atoi(buffer);
|
||||
if (f!=0)
|
||||
return f;
|
||||
else
|
||||
cout <<"Inget tal, försök igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*MENYVAL */
|
||||
/***************************************************/
|
||||
void menuItems()
|
||||
{
|
||||
clrscr();
|
||||
cout <<" 1. Lägg till ett element \n"
|
||||
<<" 2. Ta bort element \n"
|
||||
<<" 3. Sortera \n"
|
||||
<<" 4. Skriv ut alla element \n"
|
||||
<<" 5. Sök efter ett element \n"
|
||||
<<" 6. Kolla om listan är tom \n"
|
||||
<<" 7. Returnera antal element i listan \n"
|
||||
<<" 0. Avsluta \n\n\n"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*MENU TILL PROGRAMMET */
|
||||
/***************************************************/
|
||||
void main()
|
||||
{
|
||||
listClass minLista;
|
||||
listItemType newItem;
|
||||
|
||||
char val = TRUE;
|
||||
do {
|
||||
menuItems();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' :
|
||||
cout << "skriv in element!\n";
|
||||
newItem = mataIn();
|
||||
if(!minLista.newDuplicate(newItem))
|
||||
minLista.listInsert(newItem);
|
||||
else {
|
||||
cout <<"Elementet finns redan";
|
||||
getch();
|
||||
}
|
||||
break;
|
||||
case '2' :
|
||||
int position;
|
||||
cout << "Vilken position vill du ta bort? ";
|
||||
position = mataIn();
|
||||
if(minLista.posIsOk(position))
|
||||
minLista.listDel(position);
|
||||
else {
|
||||
cout <<"Posten finns inte...";
|
||||
getch();
|
||||
}
|
||||
break;
|
||||
case '3' :
|
||||
minLista.listSort();
|
||||
cout <<"Listan är sorterad!...";
|
||||
getch();break;
|
||||
case '4' :
|
||||
minLista.listDisplay();break;
|
||||
case '5' :
|
||||
listItemType soktVarde;
|
||||
cout << "Ange vad du vill söka efter: ";
|
||||
soktVarde = mataIn();
|
||||
minLista.listSeek(soktVarde);break;
|
||||
case '6' :
|
||||
minLista.listIsItEmpty();break;
|
||||
case '7' :
|
||||
minLista.listAntal();break;
|
||||
case '0' :
|
||||
cout <<"Programmet avslutat"<< endl;break;
|
||||
default: cout <<"Fel val"<< endl;
|
||||
}
|
||||
}while (val != '0');
|
||||
}
|
||||
Reference in New Issue
Block a user