0

Help me to use nested LinkedList for expressing Matrix. Thanks.
Where is error in my code?

#include  <stdio.h>
#include  <stdlib.h>
#include  <time.h>
struct  Node  { 
 	int Data;  
 	struct Node  *Next; 
};  
struct  LinkedList  { 
 	Node *Head;  Node *Tail;  
 	struct  LinkedList  *Next1; 
};  
struct  LinkedList1  { 
 	LinkedList *Head1;  LinkedList *Tail1;  
 	int n; 
};  

void  insertTail  (LinkedList &List, int x)  {  
 	Node *p;  p = new Node;  
 	p->Data = x;  p -> Next = NULL;  
 	if (List.Head == NULL)  { 
 		List.Head = p;  List.Tail = List.Head; }
 	else  { 
 		List.Tail->Next = p;  List.Tail = p; }  
}
void  insertTail  (LinkedList1 &a, LinkedList &b)  {  
 	LinkedList *p;  p = new LinkedList;  
 	p->Head = b.Head;  p -> Next1 = NULL;  
 	if (a.Head1 == NULL)  { 
 		a.Head1 = p;  a.Tail1 = a.Head1; }
 	else  { 
 		a.Tail1->Next1 = p;  a.Tail1 = p; }  
}
void  Output  (LinkedList1 & List)  {
	LinkedList *q;  q = List.Head1;  
 	Node *p;  p = q->Head;  int a, n = List.n;  
	for (int i = 0; i < n; i++)  	{  
		for (int j = 0; j < n; j++)  		{
			if (j == 0)  printf ("\n");
 			printf ("%d  ", p->Data);		
 			p = p->Next;  			}
 		q = q->Next1;  p = q->Head; 
	}	}
void  createList  (LinkedList1 &List, int *x, int n)  {
 	int k = 0;  List.Head1 = NULL;  List.n = n;  
 	LinkedList  *a;  a = new LinkedList [n]; 
	for (int i = 0; i < n; i++)  {
 		a[i].Head = NULL;  insertTail (List, a[i]);
		for (int j = 0; j < n; j++)             {
			insertTail (a[i], x[k]);  k++;	}
 	}	}
int  getData  (LinkedList1 &List, int i, int j)  { 
 	LinkedList *q;  q = List.Head1;  int b; 
	Node *p;  p = q->Head;  
 	for (int i1 = 0; i1 < i; i1++)  	   { 
		for (int j1 = 0; j1 < j; j1++)  	{ 
			b = p->Data;  p = p->Next;  }
		q = q->Next1;  p = q->Head;   }
 	return b; 
}
void main()  {
 	int x1[9] = { 3, 7, 9, 11, 17, 13, 15, 23, 26 }; 
 	int x2[9] = { 6, 8, 15, 14, 19, 23, 25, 27, 21 }; 
 	LinkedList1  a;  createList (a, x1, 3); 
 	Output (a);  printf ("\n");  
	system (“pause”);
}

Edited by chuong3a: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Lerner
1

First, decide if you are doing this in C or C++. Your headers and output statement indicate C, but your memory allocation using the keyword new is C++.

If this is supposed to be in C, then you would be best advised to post it on the C board.

The return type of main() should be int, not void. Even if your current compiler allows you to use type void, don't do it.

I'd also recommend compiling after writing no more than than few lines, or at most a small funciton, and not try debugging the whole program at once.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.