Wednesday, January 22, 2014

Linked List Generator and Editor in C

Alright, so I know this code is really long and windy and complicated, but I am so proud of it! I didn't quite make it to 200 lines, but I'm still claiming that this is my first 200 line C code. I was on pins and needles to hear back about this code and I wound up getting 100% on it in my data structures class. 

Copy-pasting it into this blog has kind of ruined my comment boxes and some of my formatting, but I think it's still pretty readable for this application. 
Enjoy!

1 //Hailey Hanson
  2 //CS 222 Programming Assignment #2
  3 //January 21, 2014
  4
  5 //This program should prompt the user with a '>' to enter a series of commands which can be found under the help menu
  6 //If the user enters an integer, that integer will be added to their list. They can then search their list, delete things from the list and print the list out
  7
  8 #include<stdio.h>
  9 #include<string.h>
 10 #include<math.h>
 11 #include<stdlib.h>
 12
 13 //Node Structure
 14 /*********************************************************/
 15 struct node{                                            //
 16         int data;                                       //
 17         struct node *next;                              //
 18 };                                                      //
 19 /********************************************************/
 20
 21
 22 //Traverse List Function
 23 /********************************************************/
 24 //traverses list and prints each part                   //
 25 void traverse (struct node *list){                      //
 26         struct node *temp_node;                         //
 27         temp_node=list->next;                           //
 28         while (temp_node != NULL){                      //
 29                 printf("%d ", temp_node->data);         //
 30                 temp_node = temp_node->next;            //
 31         }//end of while                                 //
 32         printf("\n");                                   //
 33 }//end of traverse function                             //
 34 /********************************************************/
 35
 36
 37 //Search Function
 38 /********************************************************************************************************************************************************/
 39 //searches list for an integer                                                                                                                          //
 40 int search(struct node *list, int num){ //ftn takes in the list and a number to search for the number in the list                                       //
 41         struct node *temp_node;                                                                                                                         //
 42         temp_node=list->next;                                                                                                                                   //
 43         while (temp_node != NULL){                                                                                                                      //
 44                 if (temp_node->data == num){                                                                                                            //
 45                         printf("%d is already in the list\n", num);                                                                                     //
 46                         return(1);      //this will go to the main function to let it know that the number was found and doesn't need to be inserted    //
 47                 }//end of if                                                                                                                            //
 48                 temp_node=temp_node->next;                                                                                                              //
 49         }//end of while                                                                                                                                 //
 50         printf("%d is not in the current list\n", num);                                                                                                 //
 51         return(0);                      //this will return a 0 to the main ftn, indicating that the number needs to be added to the list                //
 52                                                                                                                                                         //
 53 }//end of search function                                                                                                                               //
 54 /********************************************************************************************************************************************************/
 55
 56
 57 //Insert Function
 58 /********************************************************************************************************************************************************/
 59 void insert(struct node *list, int num){                                                                                                                //
 60         struct node *newNode;                                                                                                                           //
 61         struct node *current_node;                                                                                                                      //
 62         newNode=malloc(sizeof(struct node));                                                                                                            //
 63         current_node=list;                                                                                                                              //
 64                                                                                                                                                         //
 65         newNode->next=NULL;                     //go to end of list                                                                                     //
 66         newNode->data=num;                                                                                                                              //
 67         while (current_node->next != NULL){     //look at one node at a time, if not at end of list, then go to next node.                              //
 68                 current_node=current_node->next;                                                                                                        //
 69         }//end of while                                                                                                                                 //
 70         current_node->next=newNode;             //make last node in list point to newNode, making newNode the new footer                                //
 71                                                                                                                                                         //
 72 }//end of insert ftn                                                                                                                                    //
 73 /********************************************************************************************************************************************************/
 74
 75
 76 //Delete Function
 77 /************************************************************************/
 78 //this function will delete a node from the list                        //
 79 void delete(struct node *list, int num){                                //
 80         struct node *newNode;                                           //
 81         struct node *current_node;                                      //
 82         current_node=list;                                              //
 83         newNode=current_node->next;             //definiton of newNode  //
 84         int flag=0;                                                     //
 85                                                                         //
 86         while (newNode != NULL){                                        //
 87                 if (newNode->data == num){                              //
 88                         current_node->next = newNode->next;//change pointer from current to new node                                    //
 89                         printf("%d has been deleted\n", num);           //
 90                         flag=1;                                         //
 91                         free(newNode);                                  //
 92                         return;                                         //
 93                 }//end of if                                            //
 94                 else{                                                   //
 95                         current_node=newNode;                           //
 96                         newNode = newNode->next; //keep looking through the list
 97                 }//end of else                                          //
 98         }//end of while                                                 //
 99                                                                         //
100         if (flag == 0 ){                                                //
101                 printf("%d was not found.\n", num);                     //
102                 }//end of if                                            //
103 }//end of delete function                                               //
104 /************************************************************************/
105
106
107 //HELP MENU
108 /****************************************************************************************************************************/
109 //helps a brutha out
110 void help(){
111
112         printf("******************************************HELP MENU*************************************************\n");
113         printf("If you wish to EXIT the program, type 'Q' at any time (with no quotes)\n");
114         printf("If you wish to ADD a number to the list, just type an integer and then hit enter\n");
115         printf("If you wish to SEARCH for a number in the list, type an s followed by a space and then the integer.\n");
116         printf("If you wish to DELETE a number from the list, type a d followed by a space and then the integer.\n");
117         printf("If you wish to see the entire list, type a p and then hit enter\n");
118         printf("To return to this help menu, type a ?\n");
119         printf("****************************************************************************************************\n");
120         }//end of help menu
121
122 /******************************************************************************************************************************/
123
124 int main(){
125
126 //Linked List Formation
127 /************************************************************************/
128         struct node *list,*header;                                      //
129                 list=malloc(sizeof(struct node));                       //
130                 header=malloc(sizeof(struct node));                     //
131         list=header;                                                    //
132         list->next=NULL;                                                        //
133 /************************************************************************/
134
135
136 //Recieving Input from User
137 /*********************************************************************************************************************************************/
138         while (1){                                              //This will keep my loop going so the user puts multiple inputs in the list
139                 char input[6];
140
141                 printf(">");                                    //prompt
142                 fgets(input, 256, stdin);
143
144                 char mode;                                       //choice of Q, s, d, p or ?
145                 int num;                                        //number in list
146
147                 int type_1, type_2, type_3;
148
149                 type_2 = sscanf(input, "%c %d", &mode, &num);   //user entered a mode, then a number
150                 type_1 = sscanf(input, "%d", &num);             //user entered just a number
151                 type_3 = sscanf(input, "%c", &mode);            //should be a ? or p
152
153                 if((type_3 == 1) && (type_2 != 2) && (type_1 != 1)){    //If user enters a character, not two characters or 1 number
154                         switch (mode)
155                         {
156                                 case 'p': traverse(list);       //if user enters p, print full list
157                                         break;
158                                 case '?': help();               //if user enters ?, send to help menu
159                                         break;
160                                 case 'Q': exit(0);              //if user enters Q, quit program
161                                         break;
162                                 default: printf("\nYou have entered an incorrect input. Proceed to the help menu. Do not pass go. Do not collect $200.\n\n");
163                                         help();                 //if user is incompetent, send to help menu
164                                         break;
165                         }//end of switch
166                 }//end of if statement
167                 else if ((type_2 == 2) && (type_1 != 1)) {                              //if user enters a char and a num
168                         switch (mode)
169                         {
170                                 case 's': search(list, num);    //if user enters s followed by number, search list for that number
171                                         break;
172                                 case 'd': delete(list, num);    //if user enters d followed by number, delete that number
173                                         break;
174                                 default: printf("\nYou have entered an incorrect input. Proceed to the help menu. Do not pass go. Do not collect $200.\n\n");
175                                         help();                 //if user is incompetent, send to help menu
176                                         break;
177                         }//end of switch
178                 }//end of else if
179                 else if (type_1 == 1){                          //if user enters 1 number
180                         if((search(list, num)) == 1){
181
182                         }//end of if
183
184                         else{
185                                 insert(list, num);
186                                 printf("%d has been added to the list\n", num);
187                         }//end of else
188                 }//end of else if
189
190                 else{                                           //else if user is completely disoriented and overwhelmed, send to help menu.
191                         printf("\nYou have entered an incorrect input. Proceed to the help menu. Do not pass go. Do not collect $200.\n\n");
192                         help();
193                 }//end of else
194
195         }//end of while(1)
196 }//end of main

No comments:

Post a Comment