Need to drive my linked list
Apr 14, 2016 at 3:54am UTC
I am in need of help with my main function. I need to drive three functions. Push, pop and print. What is the simpleset way to do so from main. I am trying to prepare for an exam. Thanks for the help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include<iostream>
using namespace std;
typedef struct _NODE_
{
_NODE_ *next;
int data;
} NODE, *PNODE;
void printList(NODE*pList);
NODE* pushFront(NODE*pList, NODE*pNode);
NODE* popFront(NODE*pList);
NODE* createNode(int value);
void createList(NODE **pList);
int main()
{
NODE* list;
NODE* node;
createList(&list);
createNode(3);
createNode(5);
createNode(7);
createNode(9);
printList(list);
}
NODE* createNode(int value)
{
NODE* temp = new NODE;
temp->data = value;
temp->next = 0;
return temp;
}
void createList(NODE **pList)
{
(*pList) = NULL; // empty list
}
void printList(NODE*pList)
{
if (pList=NULL)return ;
NODE* temp = pList;
while (temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
}
NODE* pushFront(NODE*pList, NODE*pNode)
{
if (pList == 0)
{
return pNode;
}
else
{
pNode->next = pList;
pList = pNode;
return pList;
}
}
NODE* popFront(NODE*pList)
{
if (!pList) return 0;
NODE* temp = pList;
pList = pList->next;
delete temp;
return pList;
}
Apr 14, 2016 at 5:32am UTC
How to use the functions to build a list in main()?
createNode returns a new node for the list
pushFront adds the new node to the list, so...
1 2
node = createNode(3);
pushFront(list,node);
or, you could skip the middleman (node) and do...
pushFront( list, createNode(3) );
Apr 14, 2016 at 10:33am UTC
i understand. the above code builds but does not put out anything to the consol.....
Topic archived. No new replies allowed.