Kod: |
#include<stdio.h>
#include<stdlib.h> #define elementtype int typedef struct celltype { elementtype element; struct celltype *next; } celltype; typedef celltype *LIST; typedef celltype *position; position END (LIST L) { position q; for(q=L;q->next!=NULL; ) q=q->next; return q; } position MAKE_NULL (LIST *L) { *L = (celltype*)malloc(sizeof(celltype)); (*L)->next = NULL; return (*L); } void INSERT (elementtype x, position p, LIST *L) { position t; t = p->next; p->next = (celltype*)malloc(sizeof(celltype)); p->next->element = x; p->next->next= t; } void DELETE (position p, LIST *L) { position t; if(p->next==NULL) { printf("Kriva pozicija"); exit(1); } t = p->next; p->next = p->next->next; free(t); } position FIRST (LIST L) { return L; } position NEXT (position p, LIST L) { if(p->next == NULL) { printf("Kriva pozicija"); exit(1); } return p->next; } position PREVIOUS (position p, LIST L) { position q; for(q=L;q->next!=p;q=q->next) return q; } elementtype RETRIEVE (position p, LIST L) { return p->next->element; } void ELIMINATE (LIST *L, elementtype x) { position p; for(p=FIRST(*L);p!=END(*L);p=NEXT(p, *L)) { if(RETRIEVE(p, *L) > x) { position t = p; p = NEXT(p, *L); DELETE(t, L); } else p = NEXT(p, *L); } } int main(void) { elementtype y, x; LIST *L; position p; MAKE_NULL(L); int i, n; printf("Ucitaj element x: "); scanf("%d", &x); printf("Koliko ima elemenata u listi? "); scanf("%d", &n); for(i=0;i<n;i++) { scanf("%d", &y); INSERT(y, END(*L), L); } for(p=FIRST(*L);p!=END(*L);p=NEXT(p, *L)) { printf("%d ", RETRIEVE(p, *L)); } ELIMINATE(L, x); for(p=FIRST(*L);p!=END(*L);p=NEXT(p, *L)) { printf("%d ", RETRIEVE(p, *L)); } return 0; } |
output generated using printer-friendly topic mod. Vremenska zona: GMT + 01:00.