Professional Documents
Culture Documents
Άσκηση 2-1
#include <stdio.h>
#include <stdlib.h>
/* 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;
}
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 */
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;
}
int i;
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;