From: Mesutcan Kurt (mesutcank@gmail.com)
Date: Tue 30 Jan 2007 - 14:51:34 EET
bir liste veri yapisi icerisinde girilen degerleri buyukten kucuge dogru
siralamaya calisiyorum...
insertItems fonksiyonu findPlace i cagiriyor ve (olmasi gereken)
findPlace'in dogru yeri geri dondurup, insertItems in da o
yere veriyi girmesi gerekiyor...
kod su sekilde....
#include <stdio.h>
struct ITEM{
int value;
struct ITEM *next;
};
struct LIST{
struct ITEM *list;
};
/* functions starts */
struct LIST *create(struct LIST *pL);
void getItems(struct LIST *pL);
struct LIST *insertItems(int i, struct LIST *pIns);
void prnt(struct LIST *pPrnt);
struct LIST *findPlace(int i, struct LIST *place);
/* functions ends */
int main()
{
struct LIST *plist;
plist = create(plist);
/* so far so good ...*/
getItems(plist);
prnt(plist);
return 0;}
struct LIST *create(struct LIST *pL)
{
pL=(struct LIST *)malloc(sizeof(struct LIST));
if (pL)
printf ("list is created\n");
return pL; /* return the address of pL */
}
void getItems(struct LIST *pL)
{
int it;
printf ("enter new item (-1 for quit): ");
scanf(" %d",&it);
while (it != -1)
{
insertItems(it, pL);
printf ("enter new item (-1 for quit): ");
scanf(" %d",&it);
}
}
struct LIST *insertItems (int i, struct LIST *pIns)
{
struct ITEM *p = pIns->list;
struct ITEM *pIT = (struct ITEM *)malloc(sizeof(struct ITEM));
if (pIns->list == 0)
{
printf ("list was empty... ");
pIT->value=i; /* listeye ekleme */
pIT->next=pIns->list;
pIns->list=pIT;
printf ("inserted %d\n",pIT->value);
}
else
{
printf ("%d p\n",p->value);
pIT->next=findPlace(i, pIns);
/* after findPlace
we can now insert new item into
proper place, I HOPE */
pIT->value=i;
pIT->next=pIns->list;
pIns->list=pIT;
}
}
void prnt(struct LIST *pPrnt)
{
struct ITEM *y=pPrnt->list;
while (y)
{
printf("%d ",y->value);
y=y->next;
}
/* if (pPrnt->list!=0)
{
printf("%d ",y->value);
y=y->next;
}
prnt(pPrnt); */
}
struct LIST *findPlace (int i, struct LIST *place)
{
struct ITEM *curr = place->list;
/* here is we have while loop to find the proper place for new item
*/
if (curr->next == 0)
printf ("liste bos geliyor");
while ((curr->next) && (i < curr->value))
{curr = curr->next;
printf ("curr: %d ",curr->value);
}
return curr;
}
yardımcı olursanız sevinirim
_______________________________________________
Linux-programlama mailing list
Linux-programlama@liste.linux.org.tr
http://liste.linux.org.tr/mailman/listinfo/linux-programlama