You are on page 1of 4

Λύσεις

Άσκηση 2-1

#include <stdio.h>
#include <stdlib.h>

/* domi apla syndedemenhs listas */


struct listNode {
int data; /* data member (int) */
struct listNode *nextPtr; /* listNode pointer */
};

typedef struct listNode ListNode;

/* protypa synarthsewn */
int isEmpty(ListNode*);
int length(ListNode*);
void insert(ListNode*, int);
void delete(ListNode*, int);
void insertAt(ListNode*, int , int);
void deleteAt(ListNode*, int);
void printList(const ListNode*);

int main()
{
/* dimiourgia prwtou komvou */
ListNode firstNode;
/* arxikopoihsh deikth toy prwtou komvou se NULL */
firstNode.nextPtr = NULL;
printList(&firstNode);
insert(&firstNode, 5);
insert(&firstNode, 7);
insert(&firstNode, 9);
printList(&firstNode);
delete(&firstNode, 7);
printList(&firstNode);
insertAt(&firstNode, 99, 1);
printList(&firstNode);
printf("length = %d\n", length(&firstNode));
deleteAt(&firstNode, 2);
printList(&firstNode);
printf("length = %d\n", length(&firstNode));
return 0;
}

/* Epistrefei 1 an h lista einai adeia, alliws 0 */


int isEmpty(ListNode *sPtr)
{
if(sPtr->nextPtr == NULL)
return 1;
else
return 0;
} /* end function isEmpty */

3
/* Epistrefei ton ari8mo komvwn ths listas */
int length(ListNode *sPtr)
{
int len = 0;
while(sPtr->nextPtr != NULL)
{
sPtr = sPtr->nextPtr;
len++;
}
return len;
} /* end function length */

/* ektypwsh twn timwn tis listas */


void printList(const ListNode *currentPtr)
{
/* if list is empty */
if (currentPtr == NULL)
printf("List is empty.\n\n");
else
{
printf("The list is:\n");

/* while not the end of the list */


while (currentPtr != NULL) {
currentPtr = currentPtr->nextPtr;
if(currentPtr != NULL)
printf("%d --> ", currentPtr->data);
}
printf("NULL\n\n");
}
}

/* Eisagwgh neas timhs sto telos ths listas */


void insert(ListNode *sPtr, int value)
{
ListNode *newPtr; /* deikths sto neo komvo */
/* desmeysh mnhmhs neou komvou */
newPtr = malloc(sizeof(ListNode));

/* an yparxei xwros sth mnhmh */


if (newPtr != NULL)
{
/* ftase sto telos ths listas */
while(sPtr->nextPtr != NULL)
sPtr = sPtr->nextPtr;
newPtr->data = value;
newPtr->nextPtr = NULL;
sPtr->nextPtr = newPtr;
}
else
printf("%d not inserted. No memory available.\n", value);
} /* end function insert */

/* diagrafh mias timhs apo th lista */


void delete(ListNode *sPtr, int value)

4
{
ListNode *previousPtr; /* deikths ston prohgoumeno komvo ths listas */
ListNode *currentPtr; /* deikths ston trexonta komvo ths listas */
ListNode *tempPtr; /* deikths gia proswrinh apo8hkeysh */

previousPtr = sPtr;
currentPtr = sPtr->nextPtr;
/* ftase sto swsto komvo sth lista */
while (currentPtr != NULL && currentPtr->data != value)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}

/* diagrapse to swsto komvo */


if (currentPtr != NULL)
{
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free(tempPtr);
}
} /* end function delete */

/* Eisagwgh neas timhs se sygkekrimenh 8esh sth lista */


void insertAt(ListNode *sPtr, int value, int index)
{
ListNode *newPtr; /* deikths sto neo komvo */
ListNode *previousPtr; /* deikths ston prohgoumeno komvo ths listas */
ListNode *currentPtr; /* deikths ston trexonta komvo ths listas */

int i;

/* elegxos an to index einai egkyro */


if(length(sPtr) < index || index <= 0)
printf("index out of bounds\n");
else
{
/* desmeysh mnhmhs neou komvou */
newPtr = malloc(sizeof(ListNode));

/* an yparxei xwros sth mnhmh */


if (newPtr != NULL)
{
previousPtr = sPtr;
currentPtr = sPtr->nextPtr;
/* ftase ston komvo pou brisketai sto index pou 8eloume */
for(i = 1; i < index; i++)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
newPtr->data = value;
}
else
printf("%d not inserted. No memory available.\n", value);
}
} /* end function insertAt */

5
/* Diagrafh timhs apo sygkekrimenh 8esh sth lista */
void deleteAt(ListNode *sPtr, int index)
{
ListNode *previousPtr; /* deikths ston prohgoumeno komvo ths listas */
ListNode *currentPtr; /* deikths ston trexonta komvo ths listas */
ListNode *tempPtr; /* deikths gia proswrinh apo8hkeysh */

int i;

/* elegxos an to index einai egkyro */


if(length(sPtr) < index || index <= 0)
printf("index out of bounds\n");
else
{
previousPtr = sPtr;
currentPtr = sPtr->nextPtr;
/* ftase ston komvo pou brisketai sto index pou 8eloume */
for(i = 1; i < index; i++)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
/* diagrapse ton komvo sto sygkekrimeno index */
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free(tempPtr);
}
} /* end function deleteAt */

You might also like