-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem1.c
90 lines (81 loc) · 2.72 KB
/
problem1.c
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
/*
Luke Welna
Lab 13
4-15-14
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct nodeNode{
char nodeData; //S stores the char read in
struct nodeNode *nextNode; // references the next node
} ofTypeNode;
ofTypeNode *headNode = NULL;
ofTypeNode *insertData(char nodeData, ofTypeNode *headNode);
ofTypeNode *insertRecurse(ofTypeNode *curNode, ofTypeNode *headNode);
void printLinkedList(ofTypeNode *headNode);
void recursivePrint(ofTypeNode *headNode);
int main(int argc, char *argv[]){
int iCount;
int jCount;
for(iCount = 1; iCount < 3; iCount++){ // the number of words you have as arguments. Change this if you want more words as arguments, right now it is "Ada Lovelace"
for(jCount = 0; jCount < 10; jCount++){ // the max number of letters in your words (it's set to 10 because no words will be longer than 10) and my code handles if a word is shorter than the given int counter
if(argv[iCount][jCount] == '\0'){
break;
}
//printf("%c", argv[i][j]);
//puts("inserting a node");
printf("inserting nodeData %c \n", argv[iCount][jCount]);
headNode = insertData(argv[iCount][jCount], headNode);
}
puts("\n");
}
//printLinkedList(headNode);
recursivePrint(headNode);
puts("\nThis is for the Extra credit\n");
puts("printing the fourth letter");
printf("%c", headNode->nextNode->nextNode->nextNode->nodeData);
return 0;
}
ofTypeNode *insertData(char nodeData, ofTypeNode *headNode){
ofTypeNode *curNode;
curNode = (ofTypeNode*)malloc(sizeof(ofTypeNode));
curNode->nodeData = nodeData;
if(curNode == NULL){
puts("curNode is null for some reason this should never happen");
return;
}
if(headNode == NULL){ // there are no nodes in the linked list
curNode->nextNode = NULL;
return curNode;
}else{ // there is a node in the linked list
insertRecurse(headNode, curNode);
return;
}
return;
}
ofTypeNode *insertRecurse(ofTypeNode *headNode, ofTypeNode *curNode){
if(headNode->nextNode == NULL){ // the next letter can be inserted
printf("inserting curNode, curNode = %c\n", curNode->nodeData);
headNode->nextNode = curNode;
curNode->nextNode = NULL;
return headNode; // a node has been inserted, so exit
}else{ // an empty place has not been found. try to insert using the next node
insertRecurse(headNode->nextNode, curNode);
return headNode;
}
return headNode;
}
void printLinkedList(ofTypeNode *headNode){ // non-recursive print function
puts("printing linked list");
while(headNode != NULL){
printf("%c", headNode->nodeData);
headNode = headNode->nextNode;
}
}
void recursivePrint(ofTypeNode *headNode){ // recursive print function
puts("recursively printing the linked list");
if(headNode != NULL){
printf("%c \n", headNode->nodeData);
recursivePrint(headNode->nextNode);
}
}