السلام عليكم ورحمة الله وبركاته
احتاج مساعدتكم في برنامج
يقرأ اسماء ويحفظها فيdouble linked list
حسب الترتيب الابجدي
انا عملت البرنامج وهذا الكود تبعي:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char str[20];
struct node *next;
struct node *pre;
}*ptr,*temp,*prvious,*cur;//pointers to the nodee
void printlist(struct node *,struct node *);
int main(){
char value[20];
int num,i;
printf("\n hi..this program well sort the strings that u well type\n");
printf(" in ascending order\n");
printf(" (( from the big to the small ))\n");
printf("\n plz.. enter the numbers of string that u well type:");
scanf("%d",&num);
printf("\n now..enter the strings\n");
gets(value);
ptr=malloc(sizeof(struct node));
strcpy(ptr->str,value);//put the 1st string in the 1st node
ptr->next=NULL;
ptr->pre=NULL;
for(i=0;i<num;i++){
scanf("\n");
gets(value);
temp=malloc(sizeof(struct node));
strcpy(temp->str,value);//put the string in the temp node
temp->next=NULL;
temp->pre=NULL;
prvious=NULL;
cur=ptr;
while(cur!=NULL && strcmp(value,cur->str)<0){
//while not end of the list & the value>cur string
prvious=cur;
cur=cur->next;}
if(prvious==NULL){//put in the start of the list
temp->next=cur;
cur->pre=temp;
ptr=temp;}
else{//put in the middle or the end of the list
prvious->next=temp;
cur->pre=temp;
temp->pre=prvious;
temp->next=cur;
}
}
printlist(ptr,cur);
return 0;
}
//---------------funciton print-------------------
void printlist(struct node *ptr,struct node *cur){
cur=ptr;
printf("\n\n *********** the list: ***********\n");
while(cur!=NULL){
printf(" %s\n",cur->str);
cur=cur->next;}
}المشكلة
انه يرتب لي الاسماء ولكن بالعكس
يعني بدل ما يطبع
ahmad
basma
dana
يطبع:
dana
basma
ahmad
وين المشكلة؟؟؟؟
ومشكورين مقدماً على المساعدة