PDA

View Full Version : چگونگی برگرداندن آدرس یک گره از لیست پیوندی



rainlover
پنج شنبه 22 شهریور 1386, 13:54 عصر
با سلام.
برنامه ای که در زیر آورده می شود باید دو لیست پیوندی از کاراکتر ها را تشکیل داده و آدرس گره اول هر لیست را به تابع الحاق(insert) ارسال کند.
تابع insert پس از الحاق دو لیست در یک لیست جدید آدرس گره first یعنی گره اول لیست جدید را به main بر می گرداند. main هم گره اول را گرفته و لیست جدید را چاپ می کند.
در خط 49 که آن را کامنت کرده ام، 4 error وجود دارد که مبین عدم mach بودن اعلان تابع و... است.
برای رفع اشکال برنامه چه باید کنم؟
در واقع می خوام بدونم اگر بخوام تابع من آدرس یک گره از لیست را برگرداند باید چطور بنویسم؟ کمکم کنید.
با تشکر


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct list{
char ch;
struct list *next;
};
struct list *insert(struct insert*,struct insert*);
main()
{
struct list *first1 , *last1 , *node1 ;
struct list *first2 , *last2 , *node2 ;
char ans='y';
printf("list 1:\n");
first1 = NULL;
while(ans == 'y')
{
node1 = (struct list *) malloc(sizeof(struct list));
if(first1 == NULL)
first1 = last1 = node1 ;
else
{
last1 -> next = node1;
last1 = node1;
}
printf("Enter a character:");
last1 -> ch = getche();
node1 -> next=NULL;
printf("\NDo you want to continue?(y/n)");
ans = getche();
}//end of while
printf("list 2:\n");
while(ans == 'y')
{
node2 = (struct list *) malloc(sizeof(struct list));
if(first2 == NULL)
first2 = last2 = node2 ;
else
{
last2 -> next = node2;
last2 = node2;
}
printf("Enter a character:");
last2 -> ch = getche();
node2 -> next = NULL;
printf("Do you want to continue?(y/n)");
ans = getche();
}//end of while
// node1 = insert(first1,first2);
while(node1 != NULL)
{
printf("%c",node1 -> ch);
node1 = node1->next;
}
}
//*******************
struct list *insert(struct list *f1,struct list *f2)
{
struct list *node,*first;
node = f1;
first = node;
while(1)
{
node = node->next;
if(node -> next = NULL)
{
node -> next = f2;
break;
}
}
return first;
}
//pr 10-9 *** fail ***

emad_67
پنج شنبه 22 شهریور 1386, 14:10 عصر
prototype تابع رو اشتباه نوشتی . اینجوری بنویس:


struct list *insert(struct list*,struct list*);

البته بازم برنامت موقع گرفتن لیست 2 ارور میده ولی اشکال match نبودن تابع به خاطر prototype هست