Liste fini
This commit is contained in:
parent
29706fe8be
commit
28aa63db4d
140
TP3/List/Liste.c
Normal file
140
TP3/List/Liste.c
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "Liste.h"
|
||||||
|
|
||||||
|
struct SList
|
||||||
|
{
|
||||||
|
SCell *first;
|
||||||
|
SCell *last;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SCell
|
||||||
|
{
|
||||||
|
int data;
|
||||||
|
SCell *next;
|
||||||
|
SCell *prev;
|
||||||
|
};
|
||||||
|
|
||||||
|
SList *CreateList()
|
||||||
|
{
|
||||||
|
SList *list = (SList *)malloc(sizeof(SList));
|
||||||
|
list->first = NULL;
|
||||||
|
list->last = NULL;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeleteList(SList *list)
|
||||||
|
{
|
||||||
|
SCell *cell = GetFirstElement(list);
|
||||||
|
while (cell != NULL)
|
||||||
|
{
|
||||||
|
SCell *next = GetNextElement(cell);
|
||||||
|
DeleteCell(list, cell);
|
||||||
|
cell = next;
|
||||||
|
}
|
||||||
|
free(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *AddElementBegin(SList *list, int data)
|
||||||
|
{
|
||||||
|
SCell *cell = (SCell *)malloc(sizeof(SCell));
|
||||||
|
cell->data = data;
|
||||||
|
cell->next = list->first;
|
||||||
|
cell->prev = NULL;
|
||||||
|
if (list->first != NULL)
|
||||||
|
{
|
||||||
|
list->first->prev = cell;
|
||||||
|
}
|
||||||
|
list->first = cell;
|
||||||
|
if (list->last == NULL)
|
||||||
|
{
|
||||||
|
list->last = cell;
|
||||||
|
}
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *AddElementEnd(SList *list, int data)
|
||||||
|
{
|
||||||
|
SCell *cell = (SCell *)malloc(sizeof(SCell));
|
||||||
|
cell->data = data;
|
||||||
|
cell->next = NULL;
|
||||||
|
cell->prev = list->last;
|
||||||
|
if (list->last != NULL)
|
||||||
|
{
|
||||||
|
list->last->next = cell;
|
||||||
|
}
|
||||||
|
list->last = cell;
|
||||||
|
if (list->first == NULL)
|
||||||
|
{
|
||||||
|
list->first = cell;
|
||||||
|
}
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *AddElementAfter(SList *list, SCell *cell, int data)
|
||||||
|
{
|
||||||
|
if (cell == NULL)
|
||||||
|
{
|
||||||
|
return AddElementBegin(list, data);
|
||||||
|
}
|
||||||
|
else if (cell == list->last)
|
||||||
|
{
|
||||||
|
return AddElementEnd(list, data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SCell *newCell = (SCell *)malloc(sizeof(SCell));
|
||||||
|
newCell->data = data;
|
||||||
|
newCell->next = cell->next;
|
||||||
|
newCell->prev = cell;
|
||||||
|
cell->next->prev = newCell;
|
||||||
|
cell->next = newCell;
|
||||||
|
return newCell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeleteCell(SList *list, SCell *cell)
|
||||||
|
{
|
||||||
|
if (cell == list->first)
|
||||||
|
{
|
||||||
|
list->first = cell->next;
|
||||||
|
}
|
||||||
|
if (cell == list->last)
|
||||||
|
{
|
||||||
|
list->last = cell->prev;
|
||||||
|
}
|
||||||
|
if (cell->prev != NULL)
|
||||||
|
{
|
||||||
|
cell->prev->next = cell->next;
|
||||||
|
}
|
||||||
|
if (cell->next != NULL)
|
||||||
|
{
|
||||||
|
cell->next->prev = cell->prev;
|
||||||
|
}
|
||||||
|
free(cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *GetFirstElement(SList *list)
|
||||||
|
{
|
||||||
|
return list->first;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *GetLastElement(SList *list)
|
||||||
|
{
|
||||||
|
return list->last;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *GetNextElement(SCell *cell)
|
||||||
|
{
|
||||||
|
return cell->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCell *GetPrevElement(SCell *cell)
|
||||||
|
{
|
||||||
|
return cell->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetData(SCell *cell)
|
||||||
|
{
|
||||||
|
return cell->data;
|
||||||
|
}
|
22
TP3/List/Liste.h
Normal file
22
TP3/List/Liste.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef _LISTE_H
|
||||||
|
#define _LISTE_H
|
||||||
|
|
||||||
|
typedef int Data;
|
||||||
|
typedef struct SCell SCell;
|
||||||
|
typedef struct SList SList;
|
||||||
|
|
||||||
|
SList *CreateList();
|
||||||
|
void DeleteList(SList *list);
|
||||||
|
|
||||||
|
SCell *AddElementBegin(SList *list, Data elem);
|
||||||
|
SCell *AddElementEnd(SList *list, Data elem);
|
||||||
|
SCell *AddElementAfter(SList *list, SCell *cell, Data elem);
|
||||||
|
void DeleteCell(SList *list, SCell *cell);
|
||||||
|
|
||||||
|
SCell *GetFirstElement(SList *list);
|
||||||
|
SCell *GetLastElement(SList *list);
|
||||||
|
SCell *GetPrevElement(SCell *cell);
|
||||||
|
SCell *GetNextElement(SCell *cell);
|
||||||
|
Data GetData(SCell *cell);
|
||||||
|
|
||||||
|
#endif
|
64
TP3/List/main.c
Normal file
64
TP3/List/main.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "Liste.h"
|
||||||
|
|
||||||
|
void PrintList(SList *list);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
SList *list;
|
||||||
|
SCell *cell;
|
||||||
|
|
||||||
|
list = CreateList();
|
||||||
|
|
||||||
|
printf("Add 5, 3, 1\n");
|
||||||
|
AddElementBegin(list, 5);
|
||||||
|
cell = AddElementBegin(list, 3);
|
||||||
|
AddElementBegin(list, 1);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Add 6, 8\n");
|
||||||
|
AddElementEnd(list, 6);
|
||||||
|
AddElementEnd(list, 8);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Add 4\n");
|
||||||
|
AddElementAfter(list, cell, 4);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Add 2\n");
|
||||||
|
AddElementAfter(list, GetFirstElement(list), 2);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Delete 3\n");
|
||||||
|
DeleteCell(list, cell);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Add 7\n");
|
||||||
|
AddElementAfter(list, GetPrevElement(GetLastElement(list)), 7);
|
||||||
|
PrintList(list);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
DeleteList(list);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintList(SList *list)
|
||||||
|
{
|
||||||
|
if (list)
|
||||||
|
{
|
||||||
|
SCell *cell = GetFirstElement(list);
|
||||||
|
while (cell != NULL)
|
||||||
|
{
|
||||||
|
printf("[%d] -> ", GetData(cell));
|
||||||
|
cell = GetNextElement(cell);
|
||||||
|
}
|
||||||
|
printf("NULL\n");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user