hello. In my project the students are added into two levels(1 & 2) each level has it's own linked list so if you choose to add a student in level 1 it will be added to list1, if you choose to add him in level 2 it will be added to list2. how can I make two linked list from the same structure and add to each one separately .
I would appreciate any help .please!!

Recommended Answers

All 5 Replies

http://www.cs.usfca.edu/~wolber/SoftwareDev/C/CStructs.htm

Use a Struct, put both of your linked lists in there. I forget how to make a linked list in C but I think all you need is two pointers, one for each linked list. Beyond that I can't help you, because if your linked list had to have more than one piece of data I would think that each Node in the list would also have to be a Struct. But it sounds like you already know how to handle the linked lists and just need somewhere to store them both... if that is the case, then like I said, use a Struct.

I have already used a structure . but when I want to store each list in a file It doesn't work so I thought the problem will be in the add section . Any ideas?

Well two different linked list just means you are holding two different head pointers to those lists what else? You can use the same structure for constructing their nodes unless you want the information to be held in s1 and s2 are different.Post in your code if you still have a problem.

If anyone still here
Here is my code to make 2 lists and add to them .I used two add function for each list, is that right ?
"What is the problem ??"

typedef struct school {
      int id;
	  char name[80]; 
	  char address[80];
	  int phone;
	  char sex[10];
	  int level;
	  float average;
      struct school *next;
      

}school;

 
 
 struct school*last=NULL,*head=NULL;
 struct school *student1;//pointer to list one
 struct school *student2;//pointer to list two

void add(struct school *one)
{
	/*struct*/ 
        one = (school *) malloc (sizeof(school));
		printf("\n\t\t Add The Information To system " );
		printf("\n\t\t\tID : ");
		scanf(" %d", &one->id);
        
	
		printf("\t\t\tNAME : ");
		scanf("%s" , one->name);
        
		
		printf("\t\t\tAddress : ");
		scanf(" %s",one->address);
        
		
		printf("\t\t\tphone : ");
		scanf("%d" , &one->phone);
	
		
		printf("\t\t\tsex : ");
		scanf("%s" , one->sex);

		
		printf("\t\t\tlevel : ");
		scanf("%d" , &one->level);
	
		
		printf("\t\t\taverage : ");
		scanf("%f" , &one->average);
      

         one->next = NULL;
	
		
      if (head == NULL)
      {
       last = one;
       head = one;
       }
       else
       {
        last->next = one;
        last = one;
        }

	   
		 
		 printf("\n\n******************************");

		 return;
}

Inside a loop use if condition to check the level, and proceed as long as the list exhausts.
for eg.

/*Inside a loop */
if(node->level==1)
list1->next=node

The loop terminates when node->next==NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.