Skip to content

Instantly share code, notes, and snippets.

@ddellspe

ddellspe/hw6.c Secret

Created December 23, 2020 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddellspe/27448fd98e68e086d7b6709d7d183a97 to your computer and use it in GitHub Desktop.
Save ddellspe/27448fd98e68e086d7b6709d7d183a97 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
char data;
struct node *nextPtr;
};
struct node2{
char data;
struct node2 *nextPtr;
};
void print_list(struct node *cur_Ptr);
void insert_word(struct node **sPtr,char *value);
void delete_word(struct node **sPtr,char value);
void reverse_list();
main(){
char *value = (char*)malloc(sizeof(char));
struct node *cur_Ptr;
struct node *startPtr=NULL;
printf("Type in word please\n");
scanf("%s", value);
/*printf("value is %s\n",value);
insert_word(&startPtr,value);
print_list(startPtr);
printf("Type in letter please\n");
scanf("%s", value);*/
//if(*value=='\n'||*value=='\0'||*value=='\t'){
//printf("oops that's white space\n");
scanf("%s", value);
//}
printf("value is %s\n",value);
insert_word(&startPtr,value);
print_list(startPtr);
}
void insert_word(struct node **sPtr,char *value){
if(*sPtr==NULL){
*sPtr = (struct node *)malloc(sizeof(struct node));
if(*sPtr==NULL){
printf("error: memory not allocated\n");
return;
}
(*sPtr)->data = (char *)malloc(sizeof(char)*(strlen(value)+1));
if(((*sPtr)->data)==NULL){
printf("error: memory not allocated\n");
return;
}
strcpy((*sPtr)->data, value);
(*sPtr)->nextPtr=NULL;
return;
}
struct node *cur_Ptr;
struct node *prev_Ptr;
struct node *new_Ptr;
cur_Ptr=*sPtr;
prev_Ptr=NULL;
while((cur_Ptr!=NULL) && (strcmp(cur_Ptr->data, value)<0){
prev_Ptr=cur_Ptr;
cur_Ptr=cur_Ptr->nextPtr;
}
new_Ptr= (struct node *)malloc(sizeof(struct node));
if(new_Ptr==NULL){
printf("error allocating memory for new_Ptr\n");
return;
}
new_Ptr->data=*value;
new_Ptr->nextPtr=NULL;
if(prev_Ptr==NULL){
new_Ptr->nextPtr=*sPtr;
*sPtr=new_Ptr;
return;
}
prev_Ptr->nextPtr=new_Ptr;
new_Ptr->nextPtr=cur_Ptr;
return;
}
void print_list(struct node *cur_Ptr){
while(cur_Ptr!=NULL){
printf("%s->",cur_Ptr->data);
cur_Ptr=cur_Ptr->nextPtr;
}
printf("\n");
return;
}
void delete_word(struct node **sPtr,char value){
if(*sPtr==NULL){
printf("List is empty");
return;
}
struct node *cur_Ptr;
struct node *prev_Ptr;
cur_Ptr=*sPtr;
prev_Ptr=NULL;
while((cur_Ptr!=NULL) && (cur_Ptr->data!=value)){
prev_Ptr=cur_Ptr;
cur_Ptr=cur_Ptr->nextPtr;
}
if(cur_Ptr==NULL){
printf("Element not found\n");
return;
}
if(prev_Ptr==NULL){
*sPtr=cur_Ptr->nextPtr;
free(cur_Ptr);
return;
}
if(cur_Ptr->nextPtr==NULL){
prev_Ptr->nextPtr=NULL;
free(cur_Ptr);
return;
}
prev_Ptr->nextPtr=cur_Ptr->nextPtr;
free(cur_Ptr);
return;
}
/*void reverse_list(stuct node **sPtr, struct node2 **sPtr2);{
if(*sPtr==NULL){
printf("List is empty");
return;
}
struct node *cur_Ptr;
struct node *prev_Ptr;
cur_Ptr=*sPtr;
while(cur_Ptr->nextPtr !=NULL){
prev_Ptr=cur_Ptr;
cur_Ptr=cur_Ptr->nextPtr;
}
new_Ptr=(struct node2 *)malloc(sizeof(struct node2));
if(new_Ptr==NULL){
printf("error allocating memory for new pointer\n");
return;
}
new_Ptr=cur_ptr;
new_Ptr->nextPtr=NULL;
while(cur_Ptr!=NULL){
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment