Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C program which auto-completes suggestions when beginning to type a com

ID: 3792528 • Letter: C

Question

Create a C program which auto-completes suggestions when beginning to type a command and pressing tab. The program needs to read all the files names in a directory given by the user and store them in a data structure made up of a 26-element array of linked lists. A linked list implementation in C is required. Create a linked list for each letter of the alphabet in which you'll store the filenames which begin with that letter in some order. After reading the filenames, the user provides the beginning of a filename they'll like to search. The output should be all the filenames which match that prefix. The program should continue to ask for beginnings of filenames until an empty string is entered.

Explanation / Answer

Code

-----------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
char data[10] ;
struct node *next;
}*head;

node* arrNode[26];
char lettersPosition[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

/*
* Creating Node
*/
node* create_node(char fileName[])
{
node* newnode = (node *)malloc(sizeof(node));
if (newnode == NULL)
{
printf(" Memory was not allocated");
return 0;
}
else
{
strcpy(newnode->data,fileName);
newnode->next = NULL;
return newnode;
}
}

void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%s ",r->data);
r=r->next;
}
printf(" ");
}

void search(char beginningFilePrefix[])
{
node* newnode = NULL;
char str[10];
  
strcpy(str,beginningFilePrefix);
char dirPos = str[0];
node* r = arrNode[lettersPosition[dirPos]];
if(r==NULL){
printf( "No file is avialable in directory");
return;
}else{
while(r!=NULL){
char tempFile[10];
bool isMathched = true;
strcpy(tempFile,r->data);
  
for( int i = 0; i < 1 ; i++){
printf("%c %c",str[i],tempFile[i]);
if(!(str[i] == tempFile[i])){
isMathched = false;
}
}
if(isMathched){
if(newnode == NULL){
newnode = (node *)malloc(sizeof(node));
strcpy(newnode->data,r->data);
}else{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
strcpy(temp->data,r->data);
right=(struct node *)newnode;
while(right->next != NULL){
right=right->next;
}
right->next =temp;
right=temp;
right->next=NULL;
}
}
r=r->next;
}
}
if(newnode==NULL){
printf("File not found with given search critieria");
}else{
printf(" Below are the output or file list which are matched with search criteria ");
while(newnode!=NULL){
printf("%s ",newnode->data);
newnode=newnode->next;
}
}
printf(" ");
}

void displayFromArray(int dirPosition)
{
node* r= arrNode[dirPosition];
if(r==NULL){
printf( "r is null");
return;
}
while(r!=NULL){
printf("%s ",r->data);
r=r->next;
}
printf(" ");
}

void insertIntoArray(char fileName[],int dirPosition){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
strcpy(temp->data,fileName);
  
node* r = arrNode[dirPosition];
if(lettersPosition[dirPosition] == fileName[0]){
printf("directory starts with letter %c cannot hold file name starts with %c",lettersPosition[dirPosition],temp->data);
return;
}
if (r== NULL){
r=temp;
r->next=NULL;
}else{
temp->next=r;
r=temp;
}
arrNode[dirPosition] = r;
}

int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}

void append(char data[])
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
strcpy(temp->data,data);
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}

void add( char val[] )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
strcpy(temp->data,val);
if (head== NULL){
head=temp;
head->next=NULL;
}else{
temp->next=head;
head=temp;
}
}

void addafter(char num[], int loc)
{
int i;
struct node *temp,*left,*right;
right=head;

for(i=1;i<loc;i++){
left=right;
right=right->next;
}
  
temp=(struct node *)malloc(sizeof(struct node));
strcpy(temp->data,num);
left->next=temp;
left=temp;
left->next=right;
  
return;
}

void insert(char num[])
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL){
add(num);
}else{
while(temp!=NULL){
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}

int delete1(char num[])
{
struct node *temp, *prev;
temp=head;
while(temp!=NULL){
if(temp->data==num)
{
if(temp==head){
head=temp->next;
free(temp);
return 1;
}else{
prev->next=temp->next;
free(temp);
return 1;
}
}else{
prev=temp;
temp= temp->next;
}
}
return 0;
}

int main()
{
char dirName;
char fileName[1000];
int i,num;
struct node *n;
head=NULL;
  
printf("Enter the directory name. It should be a character within range [a-z]");
scanf("%c",&dirName);
printf("%c",dirName);
int dirPosition = 0;
for(int i = 0 ; i < sizeof(lettersPosition); i++){
if(lettersPosition[i] == dirName){
dirPosition = i;
break;
}
}
  
printf(" Position of dir letter %d", dirPosition);
  
while(1)
{
printf(" List Operations ");
printf("=============== ");
printf("1.Insert File Name ");
printf("2.Display list of Files ");
printf("3.Search the list of Files in Directory ");
printf("4.Exit ");
printf("Enter your choice : ");
  
if(scanf("%d",&i)<=0){
printf("Enter only an Integer ");
exit(0);
} else {
switch(i)
{
case 1:
printf("Enter the number to insert : ");
scanf("%s",&fileName);
insertIntoArray(fileName,lettersPosition[fileName[0]]);
break;
case 2:   
displayFromArray(lettersPosition[fileName[0]]);
break;
case 3:
char filePattern[1];
printf("Enter your file pattern letter [ Only single character supported.]");
scanf("%s",&filePattern);
search(filePattern);
break;
case 4:   
return 0;
default:
printf("Invalid option ");
}
}
}
return 0;
}

Output description :

This program supports following

1. linked list implementation.

2. 26 size of array of linked-list ( each index is maintained for each alphabat [a-z]). every index will contain linked-list and that linked-list contains all the filenames provided by user for a particular directory.

3. 3 major operation

insertIntoArray() - responsible for inserting filenames for a directory based on letters

displayFromArray() - display all the file names for a particular directory.

search() - search alll the files for a particular directory.

4. Program will propmt for choice ( insert, display, search, exit) or operation.

Let me know if you face any problem to run this program