The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 364 Weeks Ago, 2 Days Ago |
| Questions Answered: | 1850 |
| Tutorials Posted: | 1850 |
Graduate in Biology and Nutrition, MBA Finance
Florida State University
Aug-2000 - Jul-2007
Ass. Relationship Manager
Penn-Florida
Mar-2009 - Feb-2016
When I try to compile this code in gcc I am getting multiple errors. Could somebody look at my code and tell me why? I've attached the .c files as well as the screenshot of my error.
here is the code:
/* Double linked List functions */
/*****************************************************
Name: DoubledLinked.c
version: 0.1
Description: Implementation of a DoubleLinkedList.
These functions provide functionality of a double linked
Change history:
0.1 Initial version List. License: GNU GPL v3
Lab Problem #2 Objective:
1. Given that pHead is a global variable declared in
DoubleLinkedList.c - rewrite all the functions that
use pHead to accept a passed parameter that points
to the head of the doubly-linked list used in the
code examples in the following modules:
a. DoubleLinkedList.c
b. DoubleLinkedList.h
c. doubleMain.c (if necessary)
d. doubleMain.h (if necessary)
Lab Problem #2 Suggestions:
1. Make sure the four modules listed in items 1a thru 1d
above are in the SAME directory.
2. Compile with the command:
gcc DoubleLinkedList.c doubleMain.c -o doubleListX
3. Test with a few simple steps.
4. Start working on the objective 1 above by editing the
appropriate modules.
******************************************************/
#include "DoubleLinkedList.h"
#include "stdlib.h"
#include "stdio.h"
/* Declare pHead */
/* Variable for storing error status */
unsigned int Error = NOERROR;
DoubleLinkedList* GetNodeData(DoubleLinkedList* pNode)
{
if(!(pNode))
{
Error = MEMALLOCERROR;
return NULL;
}
else
{
printf("\nEnter a number: ");
scanf("%d",&pNode->number);
return pNode;
}
}
/* Add a node forward */
DoubleLinkedList* AddNodeForward(DoubleLinkedList* pHead)
{
DoubleLinkedList* pNode = malloc(sizeof(DoubleLinkedList));
pNode = GetNodeData(pNode);
if(pNode)
{
DoubleLinkedList* pCurrent = pHead;
if (pHead== NULL) { pNode->pNext= NULL;
pNode->pPrevious= NULL;
pHead=pNode; }
else
{
while(pCurrent->pNext!=NULL)
{
pCurrent=pCurrent->pNext;
}
pCurrent->pNext= pNode;
pNode->pNext= NULL;
pNode->pPrevious= pCurrent;
}
}
else
{
Error = MEMALLOCERROR;
}
return pHead;
}
/* Function to add nodes in reverse direction,
Arguments; Node to be added.
Returns : Nothing
*/
DoubleLinkedList* AddNodeReverse(DoubleLinkedList* pHead)
{
DoubleLinkedList* pNode = malloc(sizeof(DoubleLinkedList));
pNode = GetNodeData(pNode);
if(pNode)
{
DoubleLinkedList* pCurrent = pHead;
if (pHead==NULL)
{
pNode->pPrevious= NULL;
pNode->pNext= NULL;
pHead=pNode;
}
else
{
while(pCurrent->pPrevious != NULL )
{
pCurrent=pCurrent->pPrevious;
}
pNode->pPrevious= NULL;
pNode->pNext= pCurrent;
pCurrent->pPrevious= pNode;
pHead=pNode;
}
}
else
{
Error = MEMALLOCERROR;
}
return pHead;
}
/* Display Double linked list data in forward direction */
void DisplayNodeForward(DoubleLinkedList* pHead)
{ DoubleLinkedList* pCurrent = pHead;
if (pCurrent)
{
while(pCurrent != NULL )
{
printf("\nNumber in forward direction is %d ",pCurrent->number);
pCurrent=pCurrent->pNext;
}
}
else
{
Error = LISTEMPTY;
ErrorMessage(Error);
}
}
/* Display Double linked list data in Reverse direction */
void DisplayNodeReverse(DoubleLinkedList* pHead )
{
DoubleLinkedList* pCurrent = pHead;
if (pCurrent)
{
while(pCurrent->pNext != NULL)
{
pCurrent=pCurrent->pNext;
}
while(pCurrent)
{
printf("\nNumber in Reverse direction is %d ",pCurrent->number);
pCurrent=pCurrent->pPrevious;
}
}
else
{
Error = LISTEMPTY;
ErrorMessage(Error);
}
}
/* Delete nodes in a double linked List */
DoubleLinkedList* DeleteNode(DoubleLinkedList* pHead ,const int SearchNumber)
{
unsigned int Nodefound = FALSE;
DoubleLinkedList* pCurrent = pHead;
if (pCurrent != NULL)
{
DoubleLinkedList* pNextNode = pCurrent->pNext;
DoubleLinkedList* pTemp = (DoubleLinkedList* ) NULL; */ if (pNextNode != NULL)
{
while((pNextNode != NULL) && (Nodefound==FALSE))
{
// If search entry is at the beginning
if(pHead->number== SearchNumber)
{
pCurrent=pHead->pNext;
pHead= pCurrent;
pHead->pPrevious= NULL;
Nodefound =TRUE;
}
/* if the search entry is somewhere in the DoubleLinkedList or at the end else if(pNextNode->number == SearchNumber)
{
Nodefound = TRUE;
pTemp = pNextNode->pNext;
pCurrent->pNext = pTemp;
/* if the node to be deleted is not NULL,,,
then point pNextnode->pNext to the previous node
which is pCurrent */
if(pTemp)
{
pTemp->pPrevious= pCurrent;
}
free(pNextNode);
}
/* iterate through the Double Linked List until next node is NULL
pNextNode=pNextNode->pNext;
pCurrent=pCurrent->pNext;
}
}
else if (pCurrent->number == SearchNumber)
{
/* add code to delete nodes allocated with other functions if
the search entry is found.
*/
Nodefound = TRUE;
free(pCurrent);
pCurrent= NULL;
pHead = pCurrent;
}
}
else if (pCurrent == NULL)
{
Error= LISTEMPTY;
ErrorMessage(Error);
}
if (Nodefound == FALSE && pCurrent!= NULL)
{
Error = NODENOTFOUND;
ErrorMessage(Error);
}
return pHead;
}
/* Function to detect cycle in double linked List */
unsigned int DetectCycleinList(DoubleLinkedList* pHead)
{
DoubleLinkedList* pCurrent = pHead;
DoubleLinkedList* pFast = pCurrent;
unsigned int cycle = FALSE;
while( (cycle==FALSE) && pCurrent->pNext != NULL)
{
if(!(pFast = pFast->pNext))
{
cycle= FALSE;
break;
}
else if (pFast == pCurrent)
{
cycle = TRUE;
break;
} */ else if (!(pFast = pFast->pNext))
{
cycle = FALSE;
break;
}
else if(pFast == pCurrent)
{
cycle = TRUE;
break;
}
pCurrent=pCurrent->pNext; }
if(cycle)
{
printf("\nDouble Linked list is cyclic");
}
else
{
Error=LISTEMPTY;
ErrorMessage(Error);
}
return cycle;
}
/*Function to reverse nodes in a double linked list */
DoubleLinkedList* ReverseNodes(DoubleLinkedList* pHead)
{
DoubleLinkedList *pCurrent= NULL, *pNextNode= NULL;
pCurrent = pHead;
if (pCurrent)
{
pHead = NULL;
while (pCurrent != NULL)
{
pNextNode = pCurrent->pNext;
pCurrent->pNext = pHead;
pCurrent->pPrevious=pNextNode;
pHead = pCurrent;
pCurrent = pNextNode;
}
}
else
{
Error= LISTEMPTY;
ErrorMessage(Error);
}
return pHead;
}
/*Function to sort the linked list*/
DoubleLinkedList* split(DoubleLinkedList *pHead)
{
if(pHead == NULL)
{
return NULL;
}
DoubleLinkedList *slow = pHead;
DoubleLinkedList *fast = pHead;
while(fast->pNext)
{
fast=fast->pNext;
if(fast->pNext)
{ slow=slow->pNext;
fast=fast->pNext;
}
}
DoubleLinkedList *t=slow->pNext;
slow->pNext=NULL;
return t; }
DoubleLinkedList* merge(DoubleLinkedList* head1, DoubleLinkedList* head2)
{
if(head1 == NULL)
{
return head2;
}
if(head2 == NULL)
{
return head1;
}
if(head1->number < head2->number)
{
head1->pNext=merge(head1->pNext, head2);
head1->pNext->pPrevious = head1;
head1->pPrevious=NULL;
return head1;
}
else
{
head2->pNext = merge(head1, head2->pNext);
head2->pNext->pPrevious = head2;
head2->pPrevious=NULL;
return head2;
}
}
DoubleLinkedList* SortNodes(DoubleLinkedList* pHead)
{
if(pHead==NULL || pHead->pNext==NULL)
{
return pHead;
}
DoubleLinkedList *second = split(pHead);
pHead = SortNodes(pHead);
second = SortNodes(second);
return merge(pHead, second);
}
/* Function to display diagnostic errors */
void ErrorMessage(int Error)
{
switch(Error)
{
case LISTEMPTY:
printf("\nError: Double linked list is empty!");
break;
case MEMALLOCERROR:
printf("\nMemory allocation error ");
break;
case NODENOTFOUND:
printf("\nThe searched node is not found ");
break;
default:
printf("\nError code missing\n");
break; } }
*
Description:
Double linked list header file
License: GNU GPL v3
*/
#ifndef DOUBLELINKEDLIST_H
#define DOUBLELINKEDLIST_H
/* Codes for various errors */
#define NOERROR 0x0
#define MEMALLOCERROR 0x01
#define LISTEMPTY 0x03
#define NODENOTFOUND 0x4
/* True or false */
#define TRUE 0x1
#define FALSE 0x0
/* Double linked DoubleLinkedList definition */
typedef struct DoubleLinkedList
{
int number;
struct DoubleLinkedList* pPrevious;
struct DoubleLinkedList* pNext;
}DoubleLinkedList;
/* Get data for each node */
extern DoubleLinkedList* GetNodeData(DoubleLinkedList* pNode);
/* Add a new node forward */
extern void AddNodeForward(DoubleLinkedList* pHead);
/* Add a new node in the reverse direction */
extern void AddNodeReverse(DoubleLinkedList* pHead);
/* Display nodes in forward direction */
extern void DisplayNodeForward(DoubleLinkedList* pHead);
/*Display nodes in reverse direction */
extern void DisplayNodeReverse(DoubleLinkedList* pHead);
/* Delete nodes in the DoubleLinkedList by searching for a node */
extern void DeleteNode(DoubleLinkedList* pHead, const int number);
/* Function to detect cycle in a DoubleLinkedList */
extern unsigned int DetectCycleinList(DoubleLinkedList* pHead);
/*Function to reverse nodes */
extern void ReverseNodes(DoubleLinkedList* pHead);
/* function to display error message that DoubleLinkedList is empty */
void ErrorMessage(int Error);
/* Sort nodes */
extern void DoubleLinkedList* SortNodes(DoubleLinkedList* pHead);
#endif
/***************************************************
Name: main.c
version: 0.1
Description: Implementation of a double linked list
Change history:
0.1 Initial version
License: GNU GPL v3
Lab Problem #2 Objective:
1. Given that pHead is a global variable declared in
DoubleLinkedList.c - rewrite all the functions that
use pHead to accept a passed parameter that points
to the head of the doubly-linked list used in the
code examples in the following modules:
a. DoubleLinkedList.c
b. DoubleLinkedList.h
c. doubleMain.c (if necessary)
d. doubleMain.h (if necessary)
Lab Problem #2 Suggestions:
1. Make sure the four modules listed in items 1a thru 1d
above are in the SAME directory.
2. Compile with the command:
gcc DoubleLinkedList.c doubleMain.c -o doubleListX
3. Test with a few simple steps.
4. Start working on the objective 1 above by editing the
appropriate modules.
****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "doubleMain.h"
int main(void)
{
int choice =0;
int InputNumber=0;
DoubleLinkedList* pHead = NULL;
printf("\nThis program creates a double linked list");
printf("\nYou can add nodes in forward and reverse directions");
do
{
printf("\n1.Create Node Forward");
printf("\n2.Create Node Reverse");
printf("\n3.Delete Node");
printf("\n4.Display Nodes in forward direction");
printf("\n5.Display Nodes in reverse direction");
printf("\n6.Reverse nodes");
printf("\n7.Sort nodes");
printf("\n8.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
pHead = AddNodeForward(pHead);
break;
case 2:
pHead = AddNodeReverse(pHead);
break; case 3:
printf("\nEnter the node you want to delete: ");
scanf("%d",&InputNumber);
pHead = DeleteNode(pHead,InputNumber);
break;
case 4:
printf("\nDisplaying node data in forward direction \n");
DisplayNodeForward(pHead);
break;
case 5:
printf("\nDisplaying node data in reverse direction\n");
DisplayNodeReverse(pHead);
break;
case 6:
pHead = ReverseNodes(pHead);
break;
case 7:
pHead = SortNodes(pHead);
break;
case 8:
printf("Exiting program");
break;
default:
printf("\nIncorrect choice\n");
}
} while (choice !=8);
return 0;
}
* main.h header file */
#ifndef MAIN_H
#define MAIN_H
#include "DoubleLinkedList.c"
/* Error code */
extern unsigned int Error;
#endif
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I