Не получаеться вывести список на экран.Список неоднородный.Сама функция вывода на экран, кажется,написано правильно. Посмотрите пожалуйста, где именно надо поправить.Слишком много времени потратил уже.
Код:
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
typedef struct node1 *link1;
typedef struct node2 *link2;
struct node1
{
int a;
link1 next1; // указатель на следующее звено в списке 1 типа
link2 next2; // 2 типа
};
struct node2
{
char c;
link1 next1;
link2 next2;
};
link1 create_list(link1 beg,int *p,int *q) //функция создания структуры beg- указатель на начало списка, р q- указатели на количество
{
int i,m,n;
*p=*q=0;
link1 next1,end1;
link2 next2,end2,beg2;
end1=NULL;
beg=NULL;
end2=NULL;
beg2=NULL;
while(1)
{
printf("vvedite kolichestvo elementov type1 (int) ");
scanf("%d",&m);
*p=m; //вводим количество элементов 1 типа
for(i=1;i<=m;i++)
{
next1=new (struct node1 );// выделяем память для звена
printf("vvedite celoe chislo\n");
scanf("%d",&next1->a);
if(beg==NULL && end1==NULL)//если это звено 1, то
{
beg=next1; //начинаем с начала
}
else
{
end1->next1=next1; //добавляем в конц
}
end1=next1;
end1->next2=NULL;
}
printf("vvedite kolichestvo elementov type2 (char) ");
scanf("%d",&n);
*q=n;
for(i=1;i<=n;i++)
{
next2= new (struct node2);
printf("vvedite simvol \n");
getchar();
scanf("%c",&next2->c);
if(beg2==NULL && end2==NULL)
{
beg2=next2;
}
else
{
end2->next2=next2;
}
end2=next2;
end2->next2=NULL;
end2->next1=NULL;
}
break;
}
end1->next2=beg2;// соединяем конец 1 и начало 2 типа
return(beg);// возращаем указатель на начало списка
}
void display( link1 beg) // вывода списка
{
link1 next1;// указатели на следующее звено 1 и 2 типов
link2 next2;
printf("soderzhimoe spiska\n");
next1=beg;
while(next1!=NULL) // пока указатель на следующее звено 1 типа не нулевой
{
printf("%d \n",next1->a);
next1=next1->next1;
}
next1=beg;
while(next1->next1!=NULL)
{
next1=next1->next1;
}
next2=next1->next2; //переход к началу звеньев 2ого типа
while(next2!=NULL) // пока указатель на следующий элемент 2 типа не нулевой
{
printf("%c \n",next2->c);
next2=next2->next2; //переход к следующему звену в списке
}
}
link1 add(link1 beg,int*p,int*q) // добавления нового звена p q - количество звеньев 1 2 типов
{
int m,n,i,j;
link1 next1,buf1,new1;
link2 next2,buf2,new2;
m=*p;
j=*q;
printf("vvedite nomer,kuda vi hotite dobavit' element \n");
scanf("%d",&n);
if(n==1)// в начало
{
next1=new (struct node1);
printf("vvedite celoe chislo \n");
scanf("%d",&next1->a); // вводим новое значение звена 1 типа
buf1=beg; // запоминаем старое начало
beg=next1; // заместо него вставляем новое начало
beg->next1=buf1; // и соединяем его со старым
m=m+1;
}
else
{
next1=beg; // если не в начало
if(n<=m) // если звено должно быть 1 типа
{
for(i=0;i<n-2;i++)
{
next1=next1->next1;
}
new1=new (struct node1);
printf("vvedite celoe chislo \n");
scanf("%d",&new1->a);
buf1=next1->next1;
next1->next1=new1;
new1->next1=buf1;
m=m+1;
*p=m;
return beg;
}
if(n>=m+1 && n<=j+m+1) // если должно быть 2 типа
{
next1=beg;
while(next1->next1!=NULL) // проходим все звенья 1 типа
{
next1=next1->next1;
}
if(n==m+1)
{
new2=(link2)malloc(sizeof(struct node2));
printf("vvedite simvol \n");
getchar();
scanf("%c",&new2->c);
buf2=next1->next2;
next1->next2=new2;
new2->next2=buf2;
j=j+1;
*q=j;
return beg;
}
//// если не m+1 то
next2=next1->next2;
for(i=0;i<n-m-2;i++)
{
next2=next2->next2;
}
new2=new (struct node2);
printf("vvedite simvol \n");
getchar();
scanf("%c",&new2->c);
buf2=next2->next2;
next2->next2=new2;
new2->next2=buf2;
j=j+1;
*q=j;
return beg;
}
}
*p=m;
*q=n;
return beg;
}
void menu()
{
int choise,m,n;
link1 beg=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add \n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice :");
printf(" ");
scanf("%d",&choise);
switch (choise)
{
case 1:
{
beg=create_list(beg,&m,&n);
break;
}
case 2:
{
if(beg==NULL)
{
beg=create_list(beg,&m,&n);
}
beg=add(beg,&m,&n);
break;
}
case 3:
{
if(beg==NULL)
{
beg=create_list(beg,&m,&n);
}
display(beg);
break;
}
case 4:
{
exit(0);
}
}
}
}
void main()
{
menu();
}