GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 16-Jan-2008, 23:08
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


I try to malloc memory for several node. After mallacoation, i want formed a list depends on what user input.


What is the correct method of memory management for linked list ?

Quote:

The problem is that head is (struct node *) type and dynamicNode is (struct node **)
Solutions are many:
1. maybe you should change the type of "type"
2. maybe you should change the type of "dynamicNode"
3. maybe you could write something like "type = *(dynamicNode+offset);"
4. most likely re-arrange logic and datatypes in many places


I not understand what u say here.

A billion thanks for oyur help.
  #12  
Old 17-Jan-2008, 02:57
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: Link List In C


Quote:
Originally Posted by Peter_APIIT
I try to malloc memory for several node. After mallacoation, i want formed a list depends on what user input.


What is the correct method of memory management for linked list ?

E.g. : http://cslibrary.stanford.edu/103/
explains it (download and read the pdf)
  #13  
Old 17-Jan-2008, 04:14
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


I have read this articles before but i do see any clue from there and what is the difference ?

My idea is like this.

Code:
if first node { do something; } else // second node { while (exist a node and available user input) { assign first node point to second node and .. dynamicNode[loop-1] = dynamic[loop]; } }
Therefore, this is my whole idea.
  #14  
Old 20-Jan-2008, 03:32
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


My latest progam is below :

CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "Linked_List.h"

// -------------------------------------------------------
/*
   1. Insert Behind
   2. Insert In Front
   3. Random Insert -nth 
   4. Remove Behind until zero or one only 
   5. Remove In Front
   6. Random Remove -nth
   7. Display 
   8. Length
   9. Palindrome
*/

 /* 
	Random must have a index to indicate the postion where
    almost same as array in order to achieve fast 
	processing
*/


// -------------------------------------------------------

// tail->next = head->next; Circular Link List

void draw();
char resume();

void Initialize(struct node *);
int validateIsAlloc(struct node *);

void InsertBehind(struct node *);
void userInput(int *);


// ------------------------------------------------------
int main(int argc, char *argv[]) // char **argv
{
	struct node *head = 0;

	draw();
	Initialize(head);
	
	InsertBehind(head);

	
	
	return 0;
}

// ------------------------------------------------------
void draw()
{
	int loop;

	printf("\n\n\n\t\t");

	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	
	printf("\n\n\t\t\t  Welcome to newly Link List Simulation Program");
	printf("\n\n\n\t\t");
	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	printf("\n\n\n\n");
	
}
// -------------------------------------------------------
void Initialize(struct node *head)
{
	head = malloc(sizeof(struct node *));
	assert(head!=0);
	if (validateIsAlloc(head) != 0) // Not NULL
	{
		head->value = 1;
		head->next = NULL;
	}
	else
	{
		exit(0);
	}
}
// -------------------------------------------------------
int validateIsAlloc(struct node *node)
{
	if (node!=NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
// -------------------------------------------------------
void InsertBehind(struct node *head)
{
	struct node *newNode;
	struct node *thirdNode;
	struct node *traversal;

	int *numberptr, number, length = 1;

	// Point to head

	numberptr = &number;

	do
	{
		if (head == 0)
		{
			newNode = malloc(sizeof(struct node *));
			assert(newNode != 0);

			if (validateIsAlloc(newNode) != 0)
			{
				head = newNode;
				userInput(numberptr);
	//			printf("%d", *numberptr);
			
				newNode->value = *numberptr;
				newNode->next = NULL;
			}
			else
			{
				exit(0);
			}
		}
		else
		{
			do
			{
				thirdNode = malloc(sizeof(struct node *));
				assert(thirdNode != NULL);

				if (validateIsAlloc(thirdNode) != 0)
				{
					if (newNode->next == NULL)
					{
						newNode->next = thirdNode;
						userInput(numberptr);
						
						thirdNode->value = *numberptr;
						thirdNode->next = NULL;
					}
					else
					{
						// Previous node point to newly created node
						thirdNode->next = thirdNode;
						userInput(numberptr);
						thirdNode->value = *numberptr;
						thirdNode->next = NULL;
					}		
				}
				else
				{
					exit(0);
				}
			}while(resume() == 'Y' || resume() == 'y');
		}
		fflush(stdout);
	}while(resume() == 'Y' || resume() == 'y');

	// Find length of List

	length = 1;
	traversal = newNode;
	while(traversal->next != NULL)
	{
		traversal = traversal->next;
		length++;
	}

	printf("\n\n\t\t\tLength of list is %d", length);


	// -------------Display-------------

	traversal = newNode;
	while(traversal->next != NULL)
	{
		printf("\n%d\n", traversal->value);
		traversal = traversal->next;
	}

}
// ------------------------------------------------------
void userInput(int *numberptr)
{
	int number;

	printf("\n\n\t\t\t\tEnter a value : ");
	fflush(stdin);
	scanf("%d", &number);

	*numberptr = number;
}
// ------------------------------------------------------
char resume()
{
	char answer = 'y';
	

	fprintf(stdout, "\n\n\t\tDo you want to continue form list (y or n) : ");
	fflush(stdin);
	fscanf(stdin, "%c", &answer);
	goto next;
	
    next:
		return answer;
}
// ------------------------------------------------------





#ifndef _LList_
#define _LList_

struct node
{
	int value;
	struct node *next;
};


	
// Add index to struct

#endif



There are some logic error there.

My idea is like this :
1. create first node called newNode where head point to it;
2. create another node name thirdNode point by newnOde->next;
3. after create certain amount of thirdnode.
I only have two node which length prove it.

How to solve this problem ?

Another problem is i want declare a pointer to point to first node and traversal the node and print its content.

Should i use ptr to ptr or something else ?

A billion thanks for your help.
  #15  
Old 20-Jan-2008, 04:57
davis
 
Posts: n/a

Re: Link List In C


Quote:
Originally Posted by Peter_APIIT
As far as i know, the struct node to represent a single node and struc linked list is to represent a whole linked list.

Am i correct ?

The head pointer inside struct LList is pointing to first node.

Thanks for your help.

This is essentially correct, but it isn't a "completion" of the thoughts that go into understanding why one creates a LIST structure and a NODE structure as separate entities.

Part of the reason, and one of the most important, is so that "list functions" can be easily separated from "node functions" so that the API for dealing with lists and nodes are more organized and managed.

Here is an example of the differences between LIST functions and NODE functions.

CPP / C++ / C Code:
/* Functions that work on nodes */
NODE* node_allocate();
int node_deallocate( NODE* p_node );

/* Functions that work on lists (of nodes) */
LIST* list_allocate();
int list_deallocate( LIST* p_list );
int list_add_node( LIST* p_list, NODE* p_node );
int list_append_node( LIST* p_list, NODE* p_node );
int list_insert_node_at( LIST* p_list, const int index );
int list_delete_node_at( LIST* p_list, const int index );
int list_get_node_at( LIST* p_list, const int index, NODE* p_node_out );
int list_empty( LIST* p_list );
int list_sort( LIST* p_list, int(*compare)(const NODE* p_node_1, const NODE* p_node_2 ) );
int list_swap_nodes_at( LIST* p_list, const int idx_1, const int idx_2 );
int list_reverse( LIST* p_list );
int list_print( const LIST* p_list );

Most of the "work" that we do to nodes in a list are "list functions," and work on some set of the nodes contained in the list or nodes that are inserted or removed from the list. This is an abbreviated set of list/node functions. There are many others.

There are those who feel compelled to write API that takes LIST** and NODE**, but these are very rarely necessary and tend to over-complicate the ease of understanding the code by novices.


:davis:
  #16  
Old 21-Jan-2008, 00:42
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


CPP / C++ / C Code:
int list_add_node( LIST* p_list, NODE* p_node );
NODE* node_allocate();
LIST* list_allocate();


I think i understand better than before.

CPP / C++ / C Code:
NODE* node_allocate();


1. First, allocate memory for a node, reutn back to main.

CPP / C++ / C Code:
LIST* list_allocate();
2. You also allocate memory for list.

CPP / C++ / C Code:
int list_add_node( LIST* p_list, NODE* p_node );

3. After that, u pass those parameter to list function, and let th end list to tail->next point to newNode;

By the way, why u return integer. Is it to indicate success to form a list ? Sorry for my stupidity.
I very happy what u told me right now.
As every IT professional know, Linux guys is better than someone else.

I very admire davis. Davis is my star.


In contrast, how about my idea. Is it a bad idea ? Post 14.

Imagine struct List contain a struct node *head ptr.
ptr
|
firstnode, then call again the function to allocate memory for second node, pass both list and node to append function.

Am i correct ? I feel this is much more easy when separate the problem(break into small pieces).


A billion thanks for your help.
A billion thanks for your help.
  #17  
Old 21-Jan-2008, 18:53
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


My latest link List program :

CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "Link_List.h"

// -------------------------------------------------------
/*
   1. Insert Behind
   2. Insert In Front
   3. Random Insert -nth 
   4. Remove Behind until zero or one only 
   5. Remove In Front
   6. Random Remove -nth
   7. Display 
   8. Length   Index
   9. Palindrome
*/

 /* 
	Random must have a index to indicate the postion where
    almost same as array in order to achieve fast 
	processing
*/


// -------------------------------------------------------

// tail->next = head->next; Circular Link List

void draw();

// --------------------List Function----------------------

void list_allocate(struct LList *);// Done
void list_deallocate(struct LList *);// Done

void Initialize_List(struct LList *);// Done

int LList_ValidateIsAlloc(struct LList *); // Done

void InsertBehind(struct LList *, struct node *);


// --------------------Node Function----------------------

int node_allocate(struct node *); // Done
//void node_deallocate(struct node *); // Done


void Initialize_node(struct node *); // Done

int node_validateIsAlloc(struct node *);// Done


void userInput(int *);





// ------------------------------------------------------
int main(int argc, char *argv[]) // char **argv
{
	struct LList myList;
	struct node aNode = {0, NULL};
	struct node *node;

	int memory_Flag = 0, number, *numberptr;

	node = &aNode;
	numberptr = &number;
	

	draw();
	                  
	list_allocate(&myList); // Call by reference
	memory_Flag = node_allocate(node);
//	fprintf(stdout, "\n\n\n\t\t\t\tMemory Flag is %d\n\n\t\t\t", memory_Flag);

	
	InsertBehind(&myList, node);

	
	list_deallocate(&myList);
	return 0;
}

// ------------------------------------------------------
void draw()
{
	int loop;

	printf("\n\n\n\t\t");

	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	
	printf("\n\n\t\t\t  Welcome to newly Link List Simulation Program");
	printf("\n\n\n\t\t");
	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	printf("\n\n\n\n");
	
}
// -------------------------------------------------------
void list_allocate(struct LList *myList)
{
	int count = 1;

	do
	{
		myList = (struct LList *)malloc(sizeof(struct LList *));
		assert(myList != NULL);
		Initialize_List(myList); // Ptr must initialize No dangling ptr

		if (count == 2)
		{
			fprintf(stdout, "\n\t\t\tList Memory Allocation unsuccessful");
			exit(0);
		}
		count++;
		fflush(stdout);
	}while( LList_ValidateIsAlloc(myList) == 0 && count < 3);

	/* 
	   Continue to loop even though memory is exhausted 
	   for first time but not over second times because
	   it is unrealistic
	*/
}

// -------------------------------------------------------
void list_deallocate(struct LList *myList)
{
//	free(myList);
}

// -------------------------------------------------------
void Initialize_List(struct LList *myList)
{
	if (LList_ValidateIsAlloc(myList) != 0) // Not NULL
	{
		myList->head = NULL;
	/*	head->next = NULL;
		head->value = 0;*/
	}
	else
	{
		exit(0);
	}
}
// -------------------------------------------------------
int LList_ValidateIsAlloc(struct LList *myList)
{
	assert(myList != NULL);

	// If allocation is successful, then return true
	if (myList != NULL)
	{
//		fprintf(stdout, "Memory Allocation successful");
		return 1;
	}// Three control structure
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// -------------------------------------------------------
int node_allocate(struct node *node)
{
	int count = 1;

	do
	{
		node = (struct node *)malloc(sizeof(struct node *));
		assert(node != 0);

		Initialize_node(node);
		count++;

	}while(	node_validateIsAlloc(node) == 0 && count < 3);

	

	if (node != NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

// -------------------------------------------------------
void Initialize_node(struct node *node)
{
	int *numberptr, number;

	numberptr = &number;

	node->next = NULL;
	userInput(numberptr);
	node->value = *numberptr;

}
// -------------------------------------------------------
int node_validateIsAlloc(struct node *node)
{
	// If allocation is successful, then return true
	if (node != NULL)
	{
		fprintf(stdout, "\n\n\t\t\tNode Memory Allocation successful");
		return 1;
	}
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// ------------------------------------------------------
void userInput(int *numberptr)
{
	int number;
	
	fflush(stdin);
	printf("\n\n\t\t\t\tEnter a value : ");
	fflush(stdin);
	scanf("%d", &number);

	*numberptr = number;
}
// ------------------------------------------------------
void InsertBehind(struct LList *myList, struct node *node)
{

	if (myList->head = NULL)
	{
		myList->head = node;
		node->next = NULL;
	}
	else
	{
//		node->next = node;
	}


/*
	// Find length of List

	length = 1;
	traversal = newNode;
	while(traversal->next != NULL)
	{
		traversal = traversal->next;
		length++;
	}

	printf("\n\n\t\t\tLength of list is %d", length);


	// -------------Display-------------

	traversal = newNode;
	while(traversal->next != NULL)
	{
		printf("\n%d\n", traversal->value);
		traversal = traversal->next;
	}
*/
}
// ------------------------------------------------------

  


// ------------------------------------------------------




Header File
CPP / C++ / C Code:
#ifndef _LList_
#define _LList_

// Single Node
struct node
{
	int value;
	struct node *next;
};


// Consists of many nodes
struct LList
{
	struct node *head;
};

	
// Add index to struct

#endif



How to initialize struct node*head in struc LList ?

CPP / C++ / C Code:
struct LList
{
	struct node *head;
};


Morevoer, i have idea where InsertBehind function pass LLIst and also a node.

I can attach first node but not the second.
I want declare a pointer(current) then travesal until end of list the what pointed by travesal next point to new node.
Quote:
void InsertBehind(struct LList *myList, struct node *node)
{
struct node *current;

if (myList->head == NULL)
{
myList->head = node;
current = node; // Traversal ptr

current->next = NULL;
node->next = NULL;
}
else
{
// node->next = node;
}



Quote:
if (myList->head == NULL)

This statement is never be execute because i store other address.

I want declare a pointer current then point to what head pointed to. I think i have did that.

How to do this ?

A billion thanks for your help.
  #18  
Old 21-Jan-2008, 20:35
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "Link_List.h"

// -------------------------------------------------------
/*
   1. Insert Behind
   2. Insert In Front
   3. Random Insert -nth 
   4. Remove Behind until zero or one only 
   5. Remove In Front
   6. Random Remove -nth
   7. Display 
   8. Length   Index
   9. Palindrome
*/

 /* 
	Random must have a index to indicate the postion where
    almost same as array in order to achieve fast 
	processing
*/


// -------------------------------------------------------

// tail->next = head->next; Circular Link List

void draw();

// --------------------List Function----------------------

void list_allocate(struct LList *);// Done
void list_deallocate(struct LList *);// Done

void Initialize_List(struct LList *);// Done

int LList_ValidateIsAlloc(struct LList *); // Done

void InsertBehind(struct LList *, struct node *, struct node *);


// --------------------Node Function----------------------

int node_allocate(struct node *); // Done
//void node_deallocate(struct node *); // Done


void Initialize_node(struct node *, int *); // Done

int node_validateIsAlloc(struct node *);// Done


void userInput(int *);



// ------------------------------------------------------
int main(int argc, char *argv[]) // char **argv
{
	struct LList myList;
	struct node aNode = {0, NULL};
	struct node *node;
	struct node *current;

	int memory_Flag = 0, number, *numberptr;

	node = &aNode;
	current = node;

	numberptr = &number;
	
	draw();
	                  
	list_allocate(&myList); // Call by reference
	Initialize_List(&myList);

	memory_Flag = node_allocate(node);
	Initialize_node(node, numberptr);

//	fprintf(stdout, "\n\n\n\t\t\t\tMemory Flag is %d\n\n\t\t\t", memory_Flag);

	
	InsertBehind(&myList, node, current);

	memory_Flag = node_allocate(node);

	Initialize_node(node, numberptr);


	InsertBehind(&myList, node, current);

	
//	list_deallocate(&myList);
	return 0;
}

// ------------------------------------------------------
void draw()
{
	int loop;

	printf("\n\n\n\t\t");

	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	
	printf("\n\n\t\t\t  Welcome to newly Link List Simulation Program");
	printf("\n\n\n\t\t");
	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	printf("\n\n\n\n");
	
}
// -------------------------------------------------------
void list_allocate(struct LList *myList)
{
	int count = 1;

	do
	{
		myList = (struct LList *)malloc(sizeof(struct LList *));
		assert(myList != NULL);
	 // Ptr must initialize No dangling ptr

		if (count == 2)
		{
			fprintf(stdout, "\n\t\t\tList Memory Allocation unsuccessful");
			exit(0);
		}
		count++;
		fflush(stdout);
	}while( LList_ValidateIsAlloc(myList) == 0 && count < 3);

	/* 
	   Continue to loop even though memory is exhausted 
	   for first time but not over second times because
	   it is unrealistic
	*/
}

// -------------------------------------------------------
void list_deallocate(struct LList *myList)
{
//	free(myList);
}

// -------------------------------------------------------
void Initialize_List(struct LList *myList)
{
	if (LList_ValidateIsAlloc(myList) != 0) // Not NULL
	{
		myList->head = NULL;
	/*	head->next = NULL;
		head->value = 0;*/
	}
	else
	{
		exit(0);
	}
}
// -------------------------------------------------------
int LList_ValidateIsAlloc(struct LList *myList)
{
	assert(myList != NULL);

	// If allocation is successful, then return true
	if (myList != NULL)
	{
//		fprintf(stdout, "Memory Allocation successful");
		return 1;
	}// Three control structure
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// -------------------------------------------------------
int node_allocate(struct node *node)
{
	int count = 1;

	do
	{
		node = (struct node *)malloc(sizeof(struct node *));
		assert(node != 0);

		count++;

	}while(	node_validateIsAlloc(node) == 0 && count < 3);


	if (node != NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

// -------------------------------------------------------
void Initialize_node(struct node *node, int * numberptr)
{
	node->next = NULL;
	userInput(numberptr);
	node->value = *numberptr;
}
// -------------------------------------------------------
int node_validateIsAlloc(struct node *node)
{
	// If allocation is successful, then return true
	if (node != NULL)
	{
		fprintf(stdout, "\n\n\t\t\tNode Memory Allocation successful");
		return 1;
	}
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// ------------------------------------------------------
void userInput(int *numberptr)
{
	int number;
	
	fflush(stdin);
	printf("\n\n\t\t\t\tEnter a value : ");
	fflush(stdin);
	scanf("%d", &number);

	*numberptr = number;
}
// ------------------------------------------------------
void InsertBehind(struct LList *myList, struct node *node,
				  struct node *current)
{	
	if (myList->head == NULL)
	{
		myList->head = node;
		current = node;          // Traversal ptr 

		current->next = NULL;
		node->next = NULL;
	}
	else
	{
		while (node->next != NULL)
		{
			current->next = node;
		}
	}


/*
	// Find length of List

	length = 1;
	traversal = newNode;
	while(traversal->next != NULL)
	{
		traversal = traversal->next;
		length++;
	}

	printf("\n\n\t\t\tLength of list is %d", length);


	// -------------Display-------------

	traversal = newNode;
	while(traversal->next != NULL)
	{
		printf("\n%d\n", traversal->value);
		traversal = traversal->next;
	}
*/
}
// ------------------------------------------------------

  


// ------------------------------------------------------



When i second call insertBehind, then insert a new value to the node all the node become second node value already,
  #19  
Old 23-Jan-2008, 00:56
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


CPP / C++ / C Code:
NODE* node_allocate();
LIST* list_allocate();

I know why you want to return the pointer after memory allocation rather than using void.

Even though, variable names is same but the memory addresses totally different.

Therefore, you can form a link list but this.

You also return List ptr where you can form a two or more list which you can differentiate at later time.

A billion thanks for your help.
  #20  
Old 26-Jan-2008, 06:32
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Link List In C


I want to know how to implement deallocate function because i didn't have any clue.

I not asking you all to wrote for me but at least give me some hint.

CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "Link_List.h"

// -------------------------------------------------------
/*
   1. Insert Behind    // Done
   2. Insert In Front
   3. Random Insert Front-nth 
   4. Remove Behind until zero or one only 
   5. Remove In Front
   6. Random Remove -nth
   7. Display          // Done
   8. Length   Index   // Done
   9. Palindrome
   10. Reverse
*/

 /* 
	Random must have a index to indicate the postion where
    almost same as array in order to achieve fast 
	processing
*/


// -------------------------------------------------------

// tail->next = head->next; Circular Link List

void draw();

// --------------------List Function----------------------

struct LList *list_allocate(struct LList *);// Done
void list_deallocate(struct LList *);// Done

void Initialize_List(struct LList *);// Done

int LList_ValidateIsAlloc(struct LList *); // Done


struct LList* InsertBehind(struct LList *, struct node *);
struct LList* InsertFront(struct LList *, struct node*);
void length_of_List(struct LList *);


// --------------------Node Function----------------------

struct node* node_allocate(); // Done
void node_deallocate(struct node *); // Done

void InitializeCurrent(struct LList *, struct node *);

void Initialize_node(struct node *, int *); // Done

int node_validateIsAlloc(struct node *);   // Done


void userInput(int *);
void display(const struct LList *);



// ------------------------------------------------------
int main(int argc, char *argv[]) // char **argv
{
	struct LList *myList = 0;
	struct node *node;

	int length = 0, number, *numberptr;

	numberptr = &number;
	
	draw();
	                  
	myList = list_allocate(myList); // Call by reference
	Initialize_List(myList);


// 1 node
	node = node_allocate();
	Initialize_node(node, numberptr);//	InitializeCurrent(myList, node);
	myList = InsertBehind(myList, node);

// 2 node
	node = node_allocate();
	Initialize_node(node, numberptr);
	myList = InsertBehind(myList, node);

// 3 node
	node = node_allocate();
	Initialize_node(node, numberptr);
	myList = InsertBehind(myList, node);

// 4 node
	node = node_allocate();
	Initialize_node(node, numberptr);
	myList = InsertBehind(myList, node);


	length_of_List(myList);
	display(myList);

//	node_deallocate(node);
//	list_deallocate(myList);
	return 0;
}

// ------------------------------------------------------
void draw()
{
	int loop;

	printf("\n\n\n\t\t");

	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	
	printf("\n\n\t\t\t  Welcome to newly Link List Simulation Program");
	printf("\n\n\n\t\t");
	for (loop=0;loop<60;++loop)
	{
		printf("-");
	}
	printf("\n\n\n\n");
	
}
// -------------------------------------------------------
struct LList* list_allocate(struct LList *myList)
{
	int count = 1;

	do
	{
		myList = (struct LList *)malloc(sizeof(struct LList *));
		assert(myList != NULL);
	 // Ptr must initialize No dangling ptr

		if (count == 2)
		{
			fprintf(stdout, "\n\t\t\tList Memory Allocation unsuccessful");
			exit(0);
		}
		count++;
		fflush(stdout);
	}while( LList_ValidateIsAlloc(myList) == 0 && count < 3);

	/* 
	   Continue to loop even though memory is exhausted 
	   for first time but not over second times because
	   this is unrealistic
	*/
	return myList;
}

// -------------------------------------------------------
void list_deallocate(struct LList *myList)
{
	free(myList);
}

// -------------------------------------------------------
void Initialize_List(struct LList *myList)
{
	if (LList_ValidateIsAlloc(myList) != 0) // Not NULL
	{
		myList->head = NULL;
		myList->tail = NULL;
	/*	head->next = NULL;
		head->value = 0;*/
	}
	else
	{
		exit(0);
	}
}
// -------------------------------------------------------
int LList_ValidateIsAlloc(struct LList *myList)
{
	assert(myList != NULL);

	// If allocation is successful, then return true
	if (myList != NULL)
	{
//		fprintf(stdout, "Memory Allocation successful");
		return 1;
	}// Three control structure
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// -------------------------------------------------------
struct node* node_allocate()
{
	struct node *node;
	int count = 1;

	do
	{
		node = (struct node *)malloc(sizeof(struct node *));
		assert(node != 0);

		count++;

	}while(	node_validateIsAlloc(node) == 0 && count < 3);


	if (node != NULL)
	{
		return node;
	}
	else
	{
		exit(0);
	}
}

// -------------------------------------------------------
void node_deallocate(struct node *node)
{
	free(node);
}
// -------------------------------------------------------
void Initialize_node(struct node *node, int * numberptr)
{
	node->next = NULL;
	userInput(numberptr);
	node->value = *numberptr;
}
// -------------------------------------------------------
void InitializeCurrent(struct LList *myList,
							   struct node *node)
{
	if (myList->tail != NULL)
	{
		myList->tail = node;
		myList->head = node;
	}

}
// -------------------------------------------------------
int node_validateIsAlloc(struct node *node)
{
	// If allocation is successful, then return true
	if (node != NULL)
	{
		fprintf(stdout, "\n\n\t\t\tNode Memory Allocation successful");
		return 1;
	}
	else
	{
		fprintf(stdout, "Memory exhausted");
		return 0;
	}
}
// ------------------------------------------------------
void userInput(int *numberptr)
{
	int number;
	
	fflush(stdin);
	printf("\n\n\t\t\t\tEnter a value : ");
	fflush(stdin);
	scanf("%d", &number);

	*numberptr = number;
}
// ------------------------------------------------------
struct LList* InsertBehind(struct LList *myList, struct node *node)
{	
	if (myList->head == NULL)
	{
		myList->head = node;
		myList->tail = node;	// Traversal ptr          

		myList->tail->next = NULL;
		node->next = NULL;
		return myList;
	}
	else
	{
		while (myList->tail != NULL)
		{
			myList->tail->next = node;  // Previous node point to newly node
			myList->tail = node;  // Traversal to new node	
			return myList;
		}
	}

	
/*
	// Find length of List

	while (current != NULL)
	{
		current = node->next;
		length++;
	}

	printf("\n\n\t\t\tLength of list is %d", length);
*/


}
// ------------------------------------------------------
void length_of_List(struct LList *myList)
{
	struct node *traversal;
	int length = 1;
	
	traversal = myList->head;

	length = 1;
	while (traversal->next != NULL)
	{
		traversal = traversal->next;
		length++;
	}

	printf("\n\n\t\t\t\tLength of List is %d", length);
}
// ------------------------------------------------------
void display(const struct LList * myList)
{
	printf("\n\n\n");

	struct node *traversal;
	int length;

	traversal = myList->head;

	length = 1;
	while(traversal != NULL)
	{
		printf("\t\t\t\tNode %d of List : %d\n", length, traversal->value);
		traversal = traversal->next;
		length++;
	}
}
// ------------------------------------------------------



CPP / C++ / C Code:

#ifndef _LList_
#define _LList_

// Single Node
struct node
{
	int value;
	struct node *next;
};


// Consists of many nodes
struct LList
{
	struct node *head;
	struct node *tail;
};

	
// Add index to struct

#endif




Thanks.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
game problem using linkedListType.h and random function eureka360 C++ Forum 5 16-Feb-2007 11:48
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 13:31
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:50.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.