SlideShare a Scribd company logo
1 of 14
Download to read offline
Program In C You are required to write an interactive C program that prompts the user for
commands, accepts commands from the keyboard (stdin) and executes those commands. When a
command requires output, it must be written to stdout. The program must continue to accept and
process commands until the user types the end command ourput, it must be writen to stdout. The
program must continue to accept and proces.commands The program deals with linked lists.
Each node of such a list contains a string of length at most 10, a positive integer (i.e., an integer
value 2 1) and a pointer to the next node of the list. For any node, the string and the integer
stored in that node will be referred to as the symbol and count for that node respectively. At all
times, the list must satisfy the following two important properties. 1. The symbols appearing in
the list are all distinct; that is, no two nodes have the same symbol. 2. When the list is scanned
from left to right, the counts must be in non-increasing order. An example of such a linked list is
shown below. Search 4 Maxval4 begin/ Loop head
Solution
/**********************************************/
/****** NAMES ******************** COMMANDS ***/
/* Insert Command : ins str */
/* Delete Command : del str */
/* Forced Delete Command : fde val */
/* Print Statistics Command : pst */
/* Print List Command : prl */
/* Print using Count Range Command: pcr v1 v2 */
/* Print Prefix Command : ppr str */
/* Print Suffix Command : psu str */
/* End Command : end */
/***********************************************/
#include
#include
#include
#define CSIZE 4
#define SSIZE 11
/* Struct for each node in the linked list */
struct node
{
int count;
char symbol[SSIZE];
struct node *next;
};
struct node *head;
/* Prototypes for the functions */
void insert(char *);
void delete(char *);
void forced_delete(int value);
void print_stats();
void print_list();
void print_count(int, int);
void print_prefix(char pre[]);
void print_suffix(char suf[]);
void sort_list();
/****************
** Main Method **
****************/
int main(void)
{
int count;
char symbol[SSIZE];
char cmd[CSIZE];
int v1;
int v2;
/* Enter commands */
printf("Command? "); fflush(stdout);
scanf("%s", cmd);
/* Check for command*/
while(strcmp(cmd,"end") != 0)
{
/* check which function to use */
if(strcmp(cmd,"ins") == 0) { scanf("%s", symbol); insert(symbol); }
if(strcmp(cmd,"del") == 0) { scanf("%s", symbol); delete(symbol); }
else if(strcmp(cmd,"fde")==0){scanf("%d",&count); forced_delete(count); }
if(strcmp(cmd,"pst") == 0) { print_stats();}
if(strcmp(cmd,"prl") == 0) { print_list(); }
if(strcmp(cmd,"pcr") == 0) { scanf("%d %d",&v1,&v2); print_count(v1,v2); }
if(strcmp(cmd,"ppr") == 0) { scanf("%s", symbol); print_prefix(symbol);}
if(strcmp(cmd,"psu") == 0) { scanf("%s", symbol); print_suffix(symbol);}
printf("Command? "); fflush(stdout);
scanf("%s", cmd);
}// End of While Loop
return 0;
}// End of main method
/***************************
****** Insert Method *******
***************************/
void insert(char *sym)
{
/* Set current node to head, create new new node, and a boolean */
struct node *cur_node = head;
struct node *new_node;
int in_list = 0;
/* If list is empty, then insert in head */
if(cur_node == NULL)
{
// create memory for head, also test if head equal to NULL
head = (struct node *) malloc(sizeof(struct node));
if(head == NULL)
{
printf("Node allocation failed. "); fflush(stdout);
exit(1);
}
/* insert sym inside head->symbol and change count to one */
strncpy(head->symbol, sym, SSIZE);
head->count = 1;
head->next = NULL;
}
/* do this part if list is not empty */
else
{
// Insert inside of list
// Check if sym is in list
// Loop though all list
while(cur_node != NULL)
{
// if list is in middle add one count
if(strcmp(sym, cur_node->symbol) == 0)
{
cur_node->count++;
in_list++;
/***** Sort Method *****/
sort_list();
}
//cur_node is bottom of the list right now
cur_node = cur_node->next;
}// End of while loop
/* Do this part if you want to insert in end of list */
if(in_list == 0)
{
// put cur_node at top of the list
cur_node = head;
//insert at end of list
//go at at end of list and put it in cur_node
while(cur_node->next != NULL)
{
//cur_node is at bottom right now
cur_node = cur_node->next;
}
/* Create new memory and test if new_node is NULL */
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("Node allocation failed. "); fflush(stdout);
exit(1);
}
//link cur_node with new_node and insert inside new_node
cur_node->next = new_node;
strncpy(new_node->symbol,sym,SSIZE);
new_node->count = 1;
new_node->next = NULL;
}
}
}// End of insert method
/*******************************
******** Delete Method *********
*******************************/
void delete(char *sym)
{
struct node *cur_node = head;
struct node *to_delete;
/* Do this if list is empty */
if(cur_node == NULL)
{
printf("List is empty. "); fflush(stdout);
}
/* Do this is list is not empty */
else
{
/* del or minus head node*/
if(strcmp(sym, head->symbol) == 0)
{
if(head->count > 1)
{
head->count--;
/**** Sort Method ***/
}
//if cur_node equal one than remove it
else if(cur_node->count == 1)
{
to_delete = head;
head = cur_node->next;
free(to_delete);
to_delete = NULL;
}
}
//check rest of the list
while(cur_node != NULL && cur_node->next != NULL)
{
//If symbol is inside the list
if(strcmp(sym, cur_node->next->symbol) == 0)
{
// if symbol has more than one count
if(cur_node->next->count > 1)
{
cur_node->next->count--;
/******* Sort Method ********/
}
//if symbol has one count than del it
else if(cur_node->next->count == 1)
{
to_delete = cur_node->next;
cur_node->next = cur_node->next->next;
free(to_delete);
to_delete = NULL;
}
}
cur_node = cur_node->next;
}//End of while loop
sort_list();
}
}//End of delete method
/**********************
**Forced Delete Method*
***********************/
void forced_delete(int value)
{
//Set the struct node cur_node equal to the head
struct node *cur_node = head;
//If the current node is null return to cmd prompt
if(cur_node==NULL)
return;
//if the the head.next value is null set the head
//equal to null as well
if((head->next==NULL))
head = NULL;
//Else if the given count value of the head is less
//than the value stated set the head equal to null
else if(head-> count <= value)
head = NULL;
//Else if the nodes after the head count are less
//than the given value set the current.next value
//equal to null and traverse through the rest of the list
else
while(cur_node!=NULL){
if(cur_node-> next-> count <= value)
cur_node-> next = NULL;
cur_node = cur_node->next;
}
}
/**********************
******Prefix Method****
***********************/
void print_prefix(char pre[SSIZE]){
//Variables declared for the total size of the string.
//Num used as a boolean like value where 0 is false and 1 is true.
//I declared for use in the for loop
int i, size, num;
struct node *cur_node = head;
size = strlen(pre);
//If the head is null return that the list is empty
if(head == NULL)
printf("Empty!. ");
else
{
//While the head is not null traverse
while(cur_node != NULL)
{
// for loop to compare the full string to the string held in the array
// so it can determine if the string is held in the array value
// return 0 for false and 1 for true;
for(i = 0; i < size; i++)
{
if(cur_node->symbol[i] != pre[i]){
num = 0;
break;
}else
num = 1;
}
//If num equals 1(true) print the prefix values of the given string
//and it's integer value.
//update to the next node;
if(num==1)
printf("%s %i ", cur_node->symbol,
cur_node->count);fflush(stdout);
cur_node = cur_node->next;
}
}
}
/**********************
******Suffix Method****
***********************/
void print_suffix(char suf[SSIZE]){
//Variables declard for the size of string,
//a boolean type variable, curr to keep track of
//the current string value and suf for the given suffix value
int i, x , curr, size, num;
struct node *cur_node = head;
num = 0;
//Find the size of the given suffix
size = strlen(suf);
//If the head is equal to null the list is empty
if(head == NULL)
printf("Empty! ");
else
{
//While the current node is not null, traverse.
while(cur_node != NULL)
{
x =1;
curr = strlen(cur_node->symbol);
//Loop through the given string size, if the current
//string -1 and the given i value is equal to the suf string
//" " " " return true and get the suffix
for(i = 0; i < size; i++)
{
if(cur_node->symbol[curr - x - i] == suf[size - x - i])
num = 1;
else
{
num = 0;
break;
}
}
//If num equals 1(true) print the suffix values of the given string
//and it's integer value.
//update to the next node;
if(num==1)
printf("%s %i ", cur_node->symbol,
cur_node->count);fflush(stdout);
cur_node = cur_node->next;
}
}
}
/**********************
******Print Stats****
***********************/
void print_stats()
{
//Variables declared to find the maximum, minimum, size
// total number and the average
//Set a struct node *cur_node = to the head of the list
int max = 0, min = 0; int size = 0;
double num = 0,avg = 0;
struct node *cur_node = head;
// If the current node is null return all values of 0
if(cur_node==NULL){
printf("Number of nodes: %d ",size);fflush(stdout);
printf("Maximum: %d ",max);fflush(stdout);
printf("Minimum: %d ",min);fflush(stdout);
printf("Average: %d  ",avg);fflush(stdout);
return;
}else
//Increment size as we traverse the Linked List Nodes
//Number is added to the ammount of cur_node->count for a total number
//Increment to the next node of current
min = head->count;
while(cur_node!=NULL){
size++;
if(min>=cur_node->count)
min = cur_node->count;
num += cur_node->count;
cur_node = cur_node->next;
}
//Calculate the average and set the max = to head->count
//which returns the maximum value
avg = num/size;
max =head->count;
printf("Number of nodes: %d ",size);fflush(stdout);
printf("Maximum: %d ", max);fflush(stdout);
printf("Minimum: %d ",min);fflush(stdout);
printf("Average: %.2f  ", avg);fflush(stdout);
}
/**********************
** Print List Method **
***********************/
void print_list()
{
struct node *cur_node = head;
if(cur_node == NULL)
{
printf("The list is empty. ");fflush(stdout);
fflush(stdout);
}
else
{
while(cur_node != NULL)
{
printf(" %s %d ", cur_node->symbol, cur_node->count);fflush(stdout);
fflush(stdout);
cur_node = cur_node->next;
}//end of while loop
printf(" "); fflush(stdout);fflush(stdout);
}
}//end of print list method
/***********************
** Print Count Method **
***********************/
void print_count(int x1, int x2)
{
struct node *cur_node = head;
//print this is list is empty
if(cur_node == NULL)
{
printf("The list is empty. "); fflush(stdout);
printf(" ");
}
//print range list
else
{
//loop through list
while(cur_node != NULL)
{
//if its between range than print it
if ((cur_node->count >= x1) && (cur_node->count <= x2))
{
printf(" %s %d ", cur_node->symbol, cur_node->count);
fflush(stdout);
}
cur_node = cur_node->next;
}// End of While Loop
printf(" "); fflush(stdout);
}
}//End of Print Count Method
/***********************
****** Sort Method *****
***********************/
void sort_list()
{
struct node *cur_node, *temp;
int loop =1;
while(loop==1)
{
temp = cur_node = head;
loop = 0;
while(cur_node->next != NULL)
{
//case where the next node has a higher count that the cur_node
//the nodes will be swapped around to ensure descending order
if(cur_node->next->count > cur_node->count)
{ // mark that the nodes have been swapped
loop = 1;
//If the cur_node is equal to the head of the list
if(cur_node == head)
{
//The current node is equal to the current.next value
//Set the next temp value equal to next current value
//Store the next current value in the temp variable
//The head is equal to the temp variable and is the current node
cur_node = cur_node->next;
temp->next = cur_node->next;
cur_node->next = temp;
head = temp = cur_node;
//Swapping from other positions in the list
//The next temp value is equal to the next current value
//The next current value equals the next value of the current.next
//the current node is equal to the next temp node value
}else{
temp->next = cur_node->next;
cur_node->next = cur_node->next->next;
temp->next->next = cur_node;
cur_node = temp->next;
}
}
//If we need to move the temp node value
//and it doesn't equal the current node..
//The temp value is equal to the next temp value
//Finally update the list to keep going
if(temp != cur_node)
temp = temp->next;
cur_node = cur_node->next;
}
}
}

More Related Content

Similar to Program In C You are required to write an interactive C program that.pdf

File name a2.cppTaskFor this assignment, you are required to ei.pdf
File name a2.cppTaskFor this assignment, you are required to ei.pdfFile name a2.cppTaskFor this assignment, you are required to ei.pdf
File name a2.cppTaskFor this assignment, you are required to ei.pdfinfomalad
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docxajoy21
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfarpitcomputronics
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)Dmitry Zinoviev
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfjillisacebi75827
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfmaheshkumar12354
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdflakshmijewellery
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfbhim1213
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docxajoy21
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Discuss about Header Node And also write a program for unordered si.pdf
Discuss about Header Node  And also write a program for unordered si.pdfDiscuss about Header Node  And also write a program for unordered si.pdf
Discuss about Header Node And also write a program for unordered si.pdfeyevision3
 

Similar to Program In C You are required to write an interactive C program that.pdf (20)

File name a2.cppTaskFor this assignment, you are required to ei.pdf
File name a2.cppTaskFor this assignment, you are required to ei.pdfFile name a2.cppTaskFor this assignment, you are required to ei.pdf
File name a2.cppTaskFor this assignment, you are required to ei.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docx
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
 
Linked lists
Linked listsLinked lists
Linked lists
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Discuss about Header Node And also write a program for unordered si.pdf
Discuss about Header Node  And also write a program for unordered si.pdfDiscuss about Header Node  And also write a program for unordered si.pdf
Discuss about Header Node And also write a program for unordered si.pdf
 

More from amitbagga0808

Describe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfDescribe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfamitbagga0808
 
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfChapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfamitbagga0808
 
Compare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfCompare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfamitbagga0808
 
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfAnatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfamitbagga0808
 
A red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfA red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfamitbagga0808
 
You have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfYou have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfamitbagga0808
 
An organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfAn organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfamitbagga0808
 
Which level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfWhich level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfamitbagga0808
 
Why is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfWhy is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfamitbagga0808
 
This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfamitbagga0808
 
What factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfWhat factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfamitbagga0808
 
What is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfWhat is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfamitbagga0808
 
What are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfWhat are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfamitbagga0808
 
What are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfWhat are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfamitbagga0808
 
on the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfon the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfamitbagga0808
 
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfProve that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfamitbagga0808
 
Not sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfNot sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfamitbagga0808
 
In each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfIn each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfamitbagga0808
 
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfi need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfamitbagga0808
 
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdfamitbagga0808
 

More from amitbagga0808 (20)

Describe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfDescribe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdf
 
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfChapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
 
Compare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfCompare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdf
 
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfAnatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
 
A red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfA red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdf
 
You have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfYou have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdf
 
An organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfAn organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdf
 
Which level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfWhich level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdf
 
Why is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfWhy is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdf
 
This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdf
 
What factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfWhat factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdf
 
What is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfWhat is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdf
 
What are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfWhat are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdf
 
What are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfWhat are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdf
 
on the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfon the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdf
 
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfProve that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
 
Not sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfNot sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdf
 
In each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfIn each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdf
 
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfi need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
 
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf
18 Alicia climbs into the passenger side of her boyfriend Bos car. .pdf
 

Recently uploaded

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Program In C You are required to write an interactive C program that.pdf

  • 1. Program In C You are required to write an interactive C program that prompts the user for commands, accepts commands from the keyboard (stdin) and executes those commands. When a command requires output, it must be written to stdout. The program must continue to accept and process commands until the user types the end command ourput, it must be writen to stdout. The program must continue to accept and proces.commands The program deals with linked lists. Each node of such a list contains a string of length at most 10, a positive integer (i.e., an integer value 2 1) and a pointer to the next node of the list. For any node, the string and the integer stored in that node will be referred to as the symbol and count for that node respectively. At all times, the list must satisfy the following two important properties. 1. The symbols appearing in the list are all distinct; that is, no two nodes have the same symbol. 2. When the list is scanned from left to right, the counts must be in non-increasing order. An example of such a linked list is shown below. Search 4 Maxval4 begin/ Loop head Solution /**********************************************/ /****** NAMES ******************** COMMANDS ***/ /* Insert Command : ins str */ /* Delete Command : del str */ /* Forced Delete Command : fde val */ /* Print Statistics Command : pst */ /* Print List Command : prl */ /* Print using Count Range Command: pcr v1 v2 */ /* Print Prefix Command : ppr str */ /* Print Suffix Command : psu str */ /* End Command : end */ /***********************************************/ #include #include #include #define CSIZE 4 #define SSIZE 11 /* Struct for each node in the linked list */
  • 2. struct node { int count; char symbol[SSIZE]; struct node *next; }; struct node *head; /* Prototypes for the functions */ void insert(char *); void delete(char *); void forced_delete(int value); void print_stats(); void print_list(); void print_count(int, int); void print_prefix(char pre[]); void print_suffix(char suf[]); void sort_list(); /**************** ** Main Method ** ****************/ int main(void) { int count; char symbol[SSIZE]; char cmd[CSIZE]; int v1; int v2; /* Enter commands */ printf("Command? "); fflush(stdout); scanf("%s", cmd); /* Check for command*/ while(strcmp(cmd,"end") != 0) { /* check which function to use */
  • 3. if(strcmp(cmd,"ins") == 0) { scanf("%s", symbol); insert(symbol); } if(strcmp(cmd,"del") == 0) { scanf("%s", symbol); delete(symbol); } else if(strcmp(cmd,"fde")==0){scanf("%d",&count); forced_delete(count); } if(strcmp(cmd,"pst") == 0) { print_stats();} if(strcmp(cmd,"prl") == 0) { print_list(); } if(strcmp(cmd,"pcr") == 0) { scanf("%d %d",&v1,&v2); print_count(v1,v2); } if(strcmp(cmd,"ppr") == 0) { scanf("%s", symbol); print_prefix(symbol);} if(strcmp(cmd,"psu") == 0) { scanf("%s", symbol); print_suffix(symbol);} printf("Command? "); fflush(stdout); scanf("%s", cmd); }// End of While Loop return 0; }// End of main method /*************************** ****** Insert Method ******* ***************************/ void insert(char *sym) { /* Set current node to head, create new new node, and a boolean */ struct node *cur_node = head; struct node *new_node; int in_list = 0; /* If list is empty, then insert in head */ if(cur_node == NULL) { // create memory for head, also test if head equal to NULL head = (struct node *) malloc(sizeof(struct node)); if(head == NULL) { printf("Node allocation failed. "); fflush(stdout); exit(1); } /* insert sym inside head->symbol and change count to one */ strncpy(head->symbol, sym, SSIZE);
  • 4. head->count = 1; head->next = NULL; } /* do this part if list is not empty */ else { // Insert inside of list // Check if sym is in list // Loop though all list while(cur_node != NULL) { // if list is in middle add one count if(strcmp(sym, cur_node->symbol) == 0) { cur_node->count++; in_list++; /***** Sort Method *****/ sort_list(); } //cur_node is bottom of the list right now cur_node = cur_node->next; }// End of while loop /* Do this part if you want to insert in end of list */ if(in_list == 0) { // put cur_node at top of the list cur_node = head; //insert at end of list //go at at end of list and put it in cur_node while(cur_node->next != NULL) { //cur_node is at bottom right now cur_node = cur_node->next; }
  • 5. /* Create new memory and test if new_node is NULL */ new_node = (struct node *) malloc(sizeof(struct node)); if(new_node == NULL) { printf("Node allocation failed. "); fflush(stdout); exit(1); } //link cur_node with new_node and insert inside new_node cur_node->next = new_node; strncpy(new_node->symbol,sym,SSIZE); new_node->count = 1; new_node->next = NULL; } } }// End of insert method /******************************* ******** Delete Method ********* *******************************/ void delete(char *sym) { struct node *cur_node = head; struct node *to_delete; /* Do this if list is empty */ if(cur_node == NULL) { printf("List is empty. "); fflush(stdout); } /* Do this is list is not empty */ else { /* del or minus head node*/ if(strcmp(sym, head->symbol) == 0) { if(head->count > 1)
  • 6. { head->count--; /**** Sort Method ***/ } //if cur_node equal one than remove it else if(cur_node->count == 1) { to_delete = head; head = cur_node->next; free(to_delete); to_delete = NULL; } } //check rest of the list while(cur_node != NULL && cur_node->next != NULL) { //If symbol is inside the list if(strcmp(sym, cur_node->next->symbol) == 0) { // if symbol has more than one count if(cur_node->next->count > 1) { cur_node->next->count--; /******* Sort Method ********/ } //if symbol has one count than del it else if(cur_node->next->count == 1) { to_delete = cur_node->next; cur_node->next = cur_node->next->next; free(to_delete); to_delete = NULL; } } cur_node = cur_node->next; }//End of while loop
  • 7. sort_list(); } }//End of delete method /********************** **Forced Delete Method* ***********************/ void forced_delete(int value) { //Set the struct node cur_node equal to the head struct node *cur_node = head; //If the current node is null return to cmd prompt if(cur_node==NULL) return; //if the the head.next value is null set the head //equal to null as well if((head->next==NULL)) head = NULL; //Else if the given count value of the head is less //than the value stated set the head equal to null else if(head-> count <= value) head = NULL; //Else if the nodes after the head count are less //than the given value set the current.next value //equal to null and traverse through the rest of the list else while(cur_node!=NULL){ if(cur_node-> next-> count <= value) cur_node-> next = NULL; cur_node = cur_node->next; } } /********************** ******Prefix Method**** ***********************/
  • 8. void print_prefix(char pre[SSIZE]){ //Variables declared for the total size of the string. //Num used as a boolean like value where 0 is false and 1 is true. //I declared for use in the for loop int i, size, num; struct node *cur_node = head; size = strlen(pre); //If the head is null return that the list is empty if(head == NULL) printf("Empty!. "); else { //While the head is not null traverse while(cur_node != NULL) { // for loop to compare the full string to the string held in the array // so it can determine if the string is held in the array value // return 0 for false and 1 for true; for(i = 0; i < size; i++) { if(cur_node->symbol[i] != pre[i]){ num = 0; break; }else num = 1; } //If num equals 1(true) print the prefix values of the given string //and it's integer value. //update to the next node; if(num==1) printf("%s %i ", cur_node->symbol, cur_node->count);fflush(stdout); cur_node = cur_node->next; }
  • 9. } } /********************** ******Suffix Method**** ***********************/ void print_suffix(char suf[SSIZE]){ //Variables declard for the size of string, //a boolean type variable, curr to keep track of //the current string value and suf for the given suffix value int i, x , curr, size, num; struct node *cur_node = head; num = 0; //Find the size of the given suffix size = strlen(suf); //If the head is equal to null the list is empty if(head == NULL) printf("Empty! "); else { //While the current node is not null, traverse. while(cur_node != NULL) { x =1; curr = strlen(cur_node->symbol); //Loop through the given string size, if the current //string -1 and the given i value is equal to the suf string //" " " " return true and get the suffix for(i = 0; i < size; i++) { if(cur_node->symbol[curr - x - i] == suf[size - x - i]) num = 1; else
  • 10. { num = 0; break; } } //If num equals 1(true) print the suffix values of the given string //and it's integer value. //update to the next node; if(num==1) printf("%s %i ", cur_node->symbol, cur_node->count);fflush(stdout); cur_node = cur_node->next; } } } /********************** ******Print Stats**** ***********************/ void print_stats() { //Variables declared to find the maximum, minimum, size // total number and the average //Set a struct node *cur_node = to the head of the list int max = 0, min = 0; int size = 0; double num = 0,avg = 0; struct node *cur_node = head; // If the current node is null return all values of 0 if(cur_node==NULL){ printf("Number of nodes: %d ",size);fflush(stdout); printf("Maximum: %d ",max);fflush(stdout); printf("Minimum: %d ",min);fflush(stdout); printf("Average: %d ",avg);fflush(stdout); return; }else
  • 11. //Increment size as we traverse the Linked List Nodes //Number is added to the ammount of cur_node->count for a total number //Increment to the next node of current min = head->count; while(cur_node!=NULL){ size++; if(min>=cur_node->count) min = cur_node->count; num += cur_node->count; cur_node = cur_node->next; } //Calculate the average and set the max = to head->count //which returns the maximum value avg = num/size; max =head->count; printf("Number of nodes: %d ",size);fflush(stdout); printf("Maximum: %d ", max);fflush(stdout); printf("Minimum: %d ",min);fflush(stdout); printf("Average: %.2f ", avg);fflush(stdout); } /********************** ** Print List Method ** ***********************/ void print_list() { struct node *cur_node = head; if(cur_node == NULL) { printf("The list is empty. ");fflush(stdout); fflush(stdout); } else { while(cur_node != NULL) {
  • 12. printf(" %s %d ", cur_node->symbol, cur_node->count);fflush(stdout); fflush(stdout); cur_node = cur_node->next; }//end of while loop printf(" "); fflush(stdout);fflush(stdout); } }//end of print list method /*********************** ** Print Count Method ** ***********************/ void print_count(int x1, int x2) { struct node *cur_node = head; //print this is list is empty if(cur_node == NULL) { printf("The list is empty. "); fflush(stdout); printf(" "); } //print range list else { //loop through list while(cur_node != NULL) { //if its between range than print it if ((cur_node->count >= x1) && (cur_node->count <= x2)) { printf(" %s %d ", cur_node->symbol, cur_node->count); fflush(stdout); } cur_node = cur_node->next; }// End of While Loop printf(" "); fflush(stdout); }
  • 13. }//End of Print Count Method /*********************** ****** Sort Method ***** ***********************/ void sort_list() { struct node *cur_node, *temp; int loop =1; while(loop==1) { temp = cur_node = head; loop = 0; while(cur_node->next != NULL) { //case where the next node has a higher count that the cur_node //the nodes will be swapped around to ensure descending order if(cur_node->next->count > cur_node->count) { // mark that the nodes have been swapped loop = 1; //If the cur_node is equal to the head of the list if(cur_node == head) { //The current node is equal to the current.next value //Set the next temp value equal to next current value //Store the next current value in the temp variable //The head is equal to the temp variable and is the current node cur_node = cur_node->next; temp->next = cur_node->next; cur_node->next = temp; head = temp = cur_node; //Swapping from other positions in the list
  • 14. //The next temp value is equal to the next current value //The next current value equals the next value of the current.next //the current node is equal to the next temp node value }else{ temp->next = cur_node->next; cur_node->next = cur_node->next->next; temp->next->next = cur_node; cur_node = temp->next; } } //If we need to move the temp node value //and it doesn't equal the current node.. //The temp value is equal to the next temp value //Finally update the list to keep going if(temp != cur_node) temp = temp->next; cur_node = cur_node->next; } } }