1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include<stdio.h>
#include<stdlib.h>
struct student
{
int data;
struct student *prev;
struct student *next;
};
typedef struct student TLDE;
TLDE* create(void);
TLDE* insert_start(TLDE* l, int elem);
TLDE* insert_end(TLDE* l, int elem);
TLDE* search(TLDE* l, int elem);
TLDE* to_remove(TLDE* l, int elem);
TLDE* inverts(TLDE* l);
TLDE* create(void)
{
return NULL;
}
TLDE* insert_start(TLDE* l, int elem)
{
TLDE* l2 = (TLDE*) malloc(sizeof(TLDE));
l2->data = elem;
l2->next = l;
l2->prev = NULL;
if(l != NULL)
l->prev = l2;
return l2;
}
TLDE* insert_end(TLDE* l, int elem)
{
TLDE* l2 = (TLDE*) malloc(sizeof(TLDE));
l2->data = elem;
l2->next = NULL;
if (l==NULL)
l = l2;
else
{
TLDE* current = l;
while(current->next != NULL){
current = current->next;
}
current->next = l2;
}
return l;
}
TLDE* search(TLDE* l, int elem)
{
TLDE* l2;
for(l2=l;l2!=NULL;l2=l2->next)
{
if(l2->data == elem)
{
return l2;
}
}
return NULL;
}
TLDE* to_print(TLDE* l)
{
TLDE* l2 = l;
for (l2 = l; l2 != NULL; l2 = l2->next)
{
printf("data = %d\n", l2->data);
}
return NULL;
}
TLDE* to_remove(TLDE* l, int elem)
{
TLDE* l2 = search(l,elem);
if(l2 == NULL)
return l;
if(l == l2)
l = l2->next;
else
l2->prev->next = l2->next;
if(l2->next != NULL)
l2->next->prev = l2->prev;
free(l2);
return l;
}
void releases_memory(TLDE *l)
{
TLDE *l2 = l;
while (l2!= NULL)
{
TLDE *t = l2->next;
free (l2);
l2=t;
}
}
TLDE* inverts(TLDE* l){
TLDE *l2 = l;
TLDE *aux=NULL;
l2=l->next;
l->next=NULL;
while(l2!=NULL){
aux=l2->next;
l2->next=l;
l=l2;
l2=aux;
}
return l;
}
void to_count(TLDE* l)
{
TLDE* l2 = l;
int count = 0;
for (l2 = l; l2 != NULL; l2 = l2->next)
{
count++;
}
printf("%d \n",count);
}
int main(void)
{
int elem;
TLDE *l2;
l2=create();
l2=insert_start(l2,10);
l2=insert_start(l2,2);
l2=insert_start(l2,3);
l2=insert_start(l2,10365658);
printf("created list! \n");
to_print(l2);
l2=inverts(l2);
printf("inverted list! \n");
to_print(l2);
system("PAUSE");
return (0);
}
|