Ovo je dosta davno pisano, pa nisam siguran u točnost, uglavnom :
[code:1]
//dvostruka vezana lista, pri cemu je head dummy node
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int nr;
_node *next,*prev;
}node;
node *brisi(node *head, node **tail,int el)//brise el iz liste
{
node *tmp,*osl;
tmp=head;
while (tmp->next!=NULL && tmp->next->nr!=el)
tmp=tmp->next;//dosli smo na element prije onog koji izbacujemo
osl=tmp->next;
if (osl==NULL) return head;//element nije u listi
if (osl==*tail)//izbacujemo zadnji element
{
tmp->next=NULL;
*tail=tmp;
}
else //ne izbacujemo zadnji
{
tmp->next=osl->next;
osl->next->prev=tmp;
}
free(osl);
return head;
}
void ispis_ld(node *head,node *tail)
{
node *tmp;
//ispis s lijeva na desno
tmp=head->next;
while (tmp!=tail)
{
tmp=tmp->next;
printf("%d -> ",tmp->nr);
}
printf("\n");
}
void ispis_dl(node *head,node *tail)
{
node *tmp;
//ispis s desna na lijevo
tmp=tail;
while (tmp->prev!=head)
{
printf("%d -> ",tmp->nr);
tmp=tmp->prev;
}
printf("\n");
}
void free_mem(node *head,node *tail)
{
node *tmp;
//free mem :
while (tail!=head)
{
tmp=tail;
tail=tail->prev;
free(tmp);
}
free(head);
}
int main(void)
{
node *head,*tail,*tmp;
int i;
int a[]={1,2,3,4,5,6,7};
head=(node *)malloc(sizeof(node));//sam head je dummy
tail=(node *)malloc(sizeof(node));
head->next=tail;
tail->prev=head;
tail->next=NULL;
for (i=0;i<7;i++)//umeci na kraj
{
tmp=(node *)malloc(sizeof(node));
tmp->nr=a[i];
tmp->next=NULL;
tmp->prev=tail;
tail->next=tmp;
tail=tmp;
}
ispis_ld(head,tail);
ispis_dl(head,tail);//<==>ispis_ld(tail,head)
head=brisi(head,&tail,7);
ispis_ld(head,tail);
free_mem(head,tail);
getchar();
return 0;
}
[/code:1]
Ovo je dosta davno pisano, pa nisam siguran u točnost, uglavnom :
Kod: |
//dvostruka vezana lista, pri cemu je head dummy node
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int nr;
_node *next,*prev;
}node;
node *brisi(node *head, node **tail,int el)//brise el iz liste
{
node *tmp,*osl;
tmp=head;
while (tmp->next!=NULL && tmp->next->nr!=el)
tmp=tmp->next;//dosli smo na element prije onog koji izbacujemo
osl=tmp->next;
if (osl==NULL) return head;//element nije u listi
if (osl==*tail)//izbacujemo zadnji element
{
tmp->next=NULL;
*tail=tmp;
}
else //ne izbacujemo zadnji
{
tmp->next=osl->next;
osl->next->prev=tmp;
}
free(osl);
return head;
}
void ispis_ld(node *head,node *tail)
{
node *tmp;
//ispis s lijeva na desno
tmp=head->next;
while (tmp!=tail)
{
tmp=tmp->next;
printf("%d -> ",tmp->nr);
}
printf("\n");
}
void ispis_dl(node *head,node *tail)
{
node *tmp;
//ispis s desna na lijevo
tmp=tail;
while (tmp->prev!=head)
{
printf("%d -> ",tmp->nr);
tmp=tmp->prev;
}
printf("\n");
}
void free_mem(node *head,node *tail)
{
node *tmp;
//free mem :
while (tail!=head)
{
tmp=tail;
tail=tail->prev;
free(tmp);
}
free(head);
}
int main(void)
{
node *head,*tail,*tmp;
int i;
int a[]={1,2,3,4,5,6,7};
head=(node *)malloc(sizeof(node));//sam head je dummy
tail=(node *)malloc(sizeof(node));
head->next=tail;
tail->prev=head;
tail->next=NULL;
for (i=0;i<7;i++)//umeci na kraj
{
tmp=(node *)malloc(sizeof(node));
tmp->nr=a[i];
tmp->next=NULL;
tmp->prev=tail;
tail->next=tmp;
tail=tmp;
}
ispis_ld(head,tail);
ispis_dl(head,tail);//<==>ispis_ld(tail,head)
head=brisi(head,&tail,7);
ispis_ld(head,tail);
free_mem(head,tail);
getchar();
return 0;
}
|
|