Link to home
Start Free TrialLog in
Avatar of Megha Valishetra
Megha Valishetra

asked on

How to Store set of files in Linked list and then Read ,transmit and delete one by one...using C code.

How to add number of files in linked list and do some file operation read, move,delete..

Is it possible to add many files in linked list.. (Single or double linked list.)?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Is it possible to add many files in linked list.. (Single or double linked list.)?
Assume you are referring to file names?

Linked list is only limited by physical resources (memory)

This sounds like a very open ended question

Are you looking for code on how to create a linked list in C - because it seems to me that if you already have the code for creating a list then the adding of filenames into that list is trivial as is iterating over the list and calling some function with each file name to perform an operation.

Can you narrow this down for us a bit?
Avatar of Megha Valishetra
Megha Valishetra

ASKER

Yes adding a file reading from that file is easy... But my question is multiple files together in list......
Still not understanding the problem.

You have a list of files - you iterate over the list  (start at head node and continue to next node etc) for each node you pass the node to function that performs the operation - what am I missing?
#include <stdio.h>
#include <malloc.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
 * Node Declaration
 */
struct node
{
    int value;
    struct node *next;
};
 
snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
void rev_display(snode *);
 
typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;
 
/*
 * Main :contains menu 
 */
 
 int main()
 {
    int ch;
    char ans = 'Y';
 
    while (ans == 'Y'||ans == 'y')
    {
        printf("\n---------------------------------\n");
        printf("\nOperations on singly linked list\n");
        printf("\n---------------------------------\n");
        printf("\n1.Insert node at first");
        printf("\n2.Insert node at last");
        printf("\n3.Insert node at position");
        printf("\n4.Sorted Linked List in Ascending Order");
        printf("\n5.Delete Node from any Position");
        printf("\n6.Update Node Value");
        printf("\n7.Search Element in the linked list");
        printf("\n8.Display List from Beginning to end");
        printf("\n9.Display List from end using Recursion");
        printf("\n10.Exit\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\nEnter your choice");
        scanf("%d", &ch);
 
        switch (ch)
        {
        case 1: 
            printf("\n...Inserting node at first...\n");
            insert_node_first();
            break;
        case 2: 
            printf("\n...Inserting node at last...\n");
            insert_node_last();
            break;
        case 3: 
            printf("\n...Inserting node at position...\n");
            insert_node_pos();
            break;
        case 4: 
            printf("\n...Sorted Linked List in Ascending Order...\n");
            sorted_ascend();
            break;
        case 5: 
            printf("\n...Deleting Node from any Position...\n");
            delete_pos();
            break;
        case 6: 
            printf("\n...Updating Node Value...\n");
            update_val();
            break;
        case 7: 
            printf("\n...Searching Element in the List...\n");
            search();
            break;
        case 8: 
            printf("\n...Displaying List From Beginning to End...\n");
            display();
            break;
        case 9: 
            printf("\n...Displaying List From End using Recursion...\n");
            rev_display(first);
            break;
        case 10: 
            printf("\n...Exiting...\n");
            return 0;
            break;
        default: 
            printf("\n...Invalid Choice...\n");
            break;
        }
        printf("\nYOU WANT TO CONTINUE (Y/N)");
        scanf(" %c", &ans);
    }
    return 0;
 }
 
/*
 * Creating Node
 */
snode* create_node(int val)
{
    newnode = (snode *)malloc(sizeof(snode));
    if (newnode == NULL)
    {
        printf("\nMemory was not allocated");
        return 0;
    }
    else
    {
        newnode->value = val;
        newnode->next = NULL;
        return newnode;
    }
}
 
/*
 * Inserting Node at First
 */
void insert_node_first()
{
    int val;
 
    printf("\nEnter the value for the node:");
    scanf("%d", &val);
    newnode = create_node(val);
    if (first == last && first == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {
        temp = first;
        first = newnode;
        first->next = temp;
    }
    printf("\n----INSERTED----");    
}
 
/*
 * Inserting Node at Last
 */
void insert_node_last()
{
    int val;
 
    printf("\nEnter the value for the Node:");    
    scanf("%d", &val);
    newnode = create_node(val);
    if (first == last && last == NULL)
    {
        first = last = newnode;
        first->next = NULL;
        last->next = NULL;
    }
    else
    {
        last->next = newnode;
        last = newnode;
        last->next = NULL;
    }
 printf("\n----INSERTED----");
}    
 
/*
 * Inserting Node at position
 */
void insert_node_pos()
{
    int pos, val, cnt = 0, i;
 
    printf("\nEnter the value for the Node:");
    scanf("%d", &val);
    newnode = create_node(val);
     printf("\nEnter the position ");
    scanf("%d", &pos);
    ptr = first;
    while (ptr != NULL)
    {
        ptr = ptr->next;
        cnt++;
    }
    if (pos == 1)
    {
        if (first == last && first == NULL)
        {
            first = last = newnode;
            first->next = NULL;
            last->next = NULL;
        }
        else
        {
            temp = first;
            first = newnode;
            first->next = temp;
        }
        printf("\nInserted");
    }
    else if (pos>1 && pos<=cnt)
    {
        ptr = first;
        for (i = 1;i < pos;i++)
        {
            prev = ptr;
            ptr = ptr->next;
        }
        prev->next = newnode;
        newnode->next = ptr;
        printf("\n----INSERTED----");
    }
    else
    {
        printf("Position is out of range");
    }
}
 
/*
 * Sorted Linked List
 */
void sorted_ascend()
{
    snode *nxt;
    int t;
 
    if (first == NULL)
    {
        ISEMPTY;
        printf(":No elements to sort\n");
    }
    else
    {
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            for (nxt = ptr->next;nxt != NULL;nxt = nxt->next)
            {
                if (ptr->value > nxt->value)
                {    
                    t = ptr->value;
                    ptr->value = nxt->value;
                    nxt->value = t;
                }
            }
        }
        printf("\n---Sorted List---");
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            printf("%d\t", ptr->value);
        }
    }
}
 
/*
 * Delete Node from specified position in a non-empty list
 */
void delete_pos()
{
    int pos, cnt = 0, i;
 
    if (first == NULL)
    {
        ISEMPTY;
        printf(":No node to delete\n");
    }
    else
    {
        printf("\nEnter the position of value to be deleted:");
        scanf(" %d", &pos);
        ptr = first;
        if (pos == 1)
        {
            first = ptr->next;
            printf("\nElement deleted");    
        }
        else 
        {
            while (ptr != NULL)
            {
                ptr = ptr->next;
                cnt = cnt + 1;
            }
            if (pos > 0 && pos <= cnt)    
            {
                ptr = first;
                for (i = 1;i < pos;i++)
                {
                    prev = ptr;
                    ptr = ptr->next;
                }
                prev->next = ptr->next;
            }
            else
            {
                printf("Position is out of range");
            }
        free(ptr);
        printf("\nElement deleted");
        }
    }
}
/*
 * Updating Node value in a non-empty list
 */
void update_val()
{
    int oldval, newval, flag = 0;
 
    if (first == NULL)
    {
        ISEMPTY;
        printf(":No nodes in the list to update\n");
    }
    else
    {
        printf("\nEnter the value to be updated:");
        scanf("%d", &oldval);
        printf("\nEnter the newvalue:");    
        scanf("%d", &newval);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            if (ptr->value == oldval)
            {
                ptr->value = newval;
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nUpdated Successfully");
        }
        else
        {
            printf("\nValue not found in List");
        }
    }    
}
 
/*
 * searching an element in a non-empty list
 */
void search()
{
    int flag = 0, key, pos = 0;
 
    if (first == NULL)
    {
        ISEMPTY;
        printf(":No nodes in the list\n");
    }
    else
    {
        printf("\nEnter the value to search");
        scanf("%d", &key);
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {
            pos = pos + 1;
            if (ptr->value == key)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nElement %d found at %d position\n", key, pos);
        }
        else
        {
            printf("\nElement %d not found in list\n", key);
        }
    }    
}
/*
 * Displays non-empty List from Beginning to End
 */
void display()
{
    if (first == NULL)
    {
        ISEMPTY;
        printf(":No nodes in the list to display\n");
    }
    else
    {
        for (ptr = first;ptr != NULL;ptr = ptr->next)
        {    
            printf("%d\t", ptr->value);
        }
    }
}
 
/*
 * Display non-empty list in Reverse Order
 */
void rev_display(snode *ptr)
{
    int val;
 
    if (ptr == NULL)
    {
        ISEMPTY;
        printf(":No nodes to display\n");
    }
    else
    {
        if (ptr != NULL)
        {
            val = ptr->value;
            rev_display(ptr->next);
            printf("%d\t", val);        
        }
 
    }
}

Open in new window



here adding elements ....
Like wise adding files..

I am not getting ... How to do....
Where do the file names come from?
they are in some folder.
Ok but what is the process by which those file names get into the program?
- Will the application ask for the folder name,
- Will it be provided as a command line parameter
- or in a configuration file,
- or relative to the executable itself?

You need to help me out here - I can't guess what needs to be done you need to give me all the information so I can help you.
Ok but what is the process by which those file names get into the program? Will the application ask for the folder name, will it be provided a s command line parameter, a configuration file, relative to the executable itself?
I don't know how to get..I am asking you only

But we can give path of file to add

it may works
You are the architect of the program - how do you want it to work - what is this program going to be used for?

Somehow we need to get a list of file names into the program - there are many different ways of doing this - each is specific to a use case for the application.

It is obvious that this is an academic exercise - what was the specification you were given.
Create one linked list of files..
Sort them..
Read , move and delete
Ok so they are leaving it up to you as to how to get the files into the application.

Before we go on - did you write the code you posted above or did you get it from somewhere else? It is important as I need to know where to pitch my answers.
I copied and analyzed but I understood.
You already have the functionality for adding the file names to the list - you use option 2 and capture the file name as the node data.

That gives you your list.

As for doing the operations - how do you envisage that working - a move for example - what is that going to do (move 1 file, move all of them) how do you select the file you want to operate on how do you get the destination of where to move it to - is it a hard coded variable or do you have to ask for it each time.
how do you select the file you want to operate on

by pointer.

now moving thing we think later.

we will check delete operation.


but if i gave node as file name it takes?
You pass the node to the function and then access the file name from the node

But before you do that you need to update create_node to take a string instead of an int
snode* create_node(char * val)
{
    newnode = (snode *)malloc(sizeof(snode));
    if (newnode == NULL)
    {
        printf("\nMemory was not allocated");
        return 0;
    }
    else
    {
        newnode->value = (char *) malloc(strlen(val) + 1);
        if (newnode->value) {
            strcpy(newnode->value, val);
        }
        newnode->next = NULL;
        return newnode;
    }
}

Open in new window

Which in turn requires that you change the insert_node_first / last / pos to accept string input
First add this define to the top of your source or your .h file
#define MAX_FILE_PATH 256

Open in new window

Then change the insert_node function as follows
void insert_node_XXXXXX()
{
    char val[MAX_FILE_PATH];
 
    printf("\nEnter the value for the node:");
    scanf("%s", val);
    newnode = create_node(val);
    ...
}

Open in new window


You will also need to update the update_val() as follows
Read the new string
Delete the old value by doing a free(node->value)
And then re-allocating memory for the new value.

In your delete_pos() function you will have to first free the value before freeing the node.

Once you have these changes your list will now store strings.

There is an obvious flaw in the above code - there are no checks to ensure that the user enters more data than your buffer can hold. There are various techniques around this but as this is an academic exercise don't get bogged down in that for now - just focus on updating the application to be able to read strings instead of integers.
Ok I will change and come to you.

I have 1 doubt.. where should my files stay?
where should my files stay?
At this stage it does not matter. The key goal for now is to get the application working with manual input

What you can do is declare a const variable that stores the path

For example
const char * cstrFilePath = "c:\\projects\\data\\files\\";

Open in new window


Then when you need to operate on the file you could do something like

char szPath[MAX_FILE_PATH]; // assumes you have added the #define MAX_FILE_PATH 256
sprintf(szPath, "%s%s", cstrFilePath, snode->value);

Open in new window

OR
strcpy(szPath, cstrFilePath);
strcat(szPath, snode->value);

Open in new window


Above for illustration only - adapt for use in your code.
where to add this?
The file path - you add to the top of your file as a global.

The other code is used whenever you need to access the file - say to read it or to move it - you use that code to create the path to where the file is. You can then use fopen, unlink etc on the file using that path.
void insert_node_first()
{
	char val[MAX_FILE_PATH];
	//char szPath[MAX_FILE_PATH]; // assumes you have added the #define MAX_FILE_PATH 256
	//sprintf(szPath, "%s%s", cstrFilePath, snode->value);

	printf("\nEnter the value for the node:");
	scanf("%s", val);
	newnode = create_node(val);
	if (first == last && first == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		temp = first;
		first = newnode;
		first->next = temp;
	}
	printf("\n----INSERTED----");
}

Open in new window


check this .. is it ryt?
 and i m getting error -->'create_node': redefinition; different basic types for code i made changes as u said
you need to change create_node() as well - currently it looks like it is still expecting an int - make sure you change the prototype for the function as well

void create_node(char *);

Open in new window


Refer this comment https://www.experts-exchange.com/questions/29015493/How-to-Store-set-of-files-in-Linked-list-and-then-Read-transmit-and-delete-one-by-one-using-C-code.html?anchorAnswerId=42088862#a42088862
/*
* C Program to Implement Singly Linked List using Dynamic Memory Allocation
*/
#include <stdio.h>
#include <malloc.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
struct node
{
	int value;
	struct node *next ;
};
void create_node(char *);
//snode* create_node(char);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
#define MAX_FILE_PATH 256
const char * cstrFilePath = "c:\\projects\\data\\files\\";

typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
	int ch;
	char ans = 'Y';

	while (ans == 'Y' || ans == 'y')
	{
		printf("\n---------------------------------\n");
		printf("\nOperations on singly linked list\n");
		printf("\n---------------------------------\n");
		printf("\n1.Insert node at first");
		printf("\n2.Insert node at last");
		printf("\n3.Insert node at position");
		printf("\n4.Sorted Linked List in Ascending Order");
		printf("\n5.Delete Node from any Position");
		printf("\n6.Update Node Value");
		printf("\n7.Search Element in the linked list");
		printf("\n8.Display List from Beginning to end");
		printf("\n9.Display List from end using Recursion");
		printf("\n10.Exit\n");
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("\nEnter your choice");
		scanf("%d", &ch);

		switch (ch)
		{
		case 1:
			printf("\n...Inserting node at first...\n");
			insert_node_first();
			break;
		case 2:
			printf("\n...Inserting node at last...\n");
			insert_node_last();
			break;
		case 3:
			printf("\n...Inserting node at position...\n");
			insert_node_pos();
			break;
		case 4:
			printf("\n...Sorted Linked List in Ascending Order...\n");
			sorted_ascend();
			break;
		case 5:
			printf("\n...Deleting Node from any Position...\n");
			delete_pos();
			break;
		case 6:
			printf("\n...Updating Node Value...\n");
			update_val();
			break;
		case 7:
			printf("\n...Searching Element in the List...\n");
			search();
			break;
		case 8:
			printf("\n...Displaying List From Beginning to End...\n");
			display();
			break;
		case 9:
			printf("\n...Displaying List From End using Recursion...\n");
			rev_display(first);
			break;
		case 10:
			printf("\n...Exiting...\n");
			return 0;
			break;
		default:
			printf("\n...Invalid Choice...\n");
			break;
		}
		printf("\nYOU WANT TO CONTINUE (Y/N)");
		scanf(" %c", &ans);
	}
	return 0;
}

/*
* Creating Node
*/
//snode* create_node(char val)
void create_node(char *);
{
	newnode = (snode *)malloc(sizeof(snode));
	if (newnode == NULL)
	{
		printf("\nMemory was not allocated");
		return 0;
	}
	else
	{
		newnode->value = (char *)malloc(strlen(val) + 1);
		if (newnode->value) {
			strcpy(newnode->value, val);
		}
		newnode->next = NULL;
		return newnode;
	}
}

/*
* Inserting Node at First
*/
void insert_node_first()
{
	char val[MAX_FILE_PATH];


	printf("\nEnter the value for the node:");
	scanf("%s", val);
	newnode = create_node(val);
	if (first == last && first == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		temp = first;
		first = newnode;
		first->next = temp;
	}
	printf("\n----INSERTED----");
}

/*
* Inserting Node at Last
*/
void insert_node_last()
{
		char val[MAX_FILE_PATH];

		printf("\nEnter the value for the node:");
		scanf("%s", val);
		newnode = create_node(val);
	if (first == last && last == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		last->next = newnode;
		last = newnode;
		last->next = NULL;
	}
	printf("\n----INSERTED----");
}

/*
* Inserting Node at position
*/
	void insert_node_pos()
	{
		int pos, val, cnt = 0, i;

		printf("\nEnter the value for the Node:");
		scanf("%d", &val);
		newnode = create_node(val);
		printf("\nEnter the position ");
		scanf("%d", &pos);
		ptr = first;
		while (ptr != NULL)
		{
			ptr = ptr->next;
			cnt++;
		}
		if (pos == 1)
		{
			if (first == last && first == NULL)
			{
				first = last = newnode;
				first->next = NULL;
				last->next = NULL;
			}
			else
			{
				temp = first;
				first = newnode;
				first->next = temp;
			}
			printf("\nInserted");
		}

	else if (pos>1 && pos <= cnt)
	{
		ptr = first;
		for (i = 1; i < pos; i++)
		{
			prev = ptr;
			ptr = ptr->next;
		}
		prev->next = newnode;
		newnode->next = ptr;
		printf("\n----INSERTED----");
	}
	else
	{
		printf("Position is out of range");
	}
}

/*
* Sorted Linked List
*/
void sorted_ascend()
{
	snode *nxt;
	int t;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No elements to sort\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			for (nxt = ptr->next; nxt != NULL; nxt = nxt->next)
			{
				if (ptr->value > nxt->value)
				{
					t = ptr->value;
					ptr->value = nxt->value;
					nxt->value = t;
				}
			}
		}
		printf("\n---Sorted List---");
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
	int pos, cnt = 0, i;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No node to delete\n");
	}
	else
	{
		printf("\nEnter the position of value to be deleted:");
		scanf(" %d", &pos);
		ptr = first;
		if (pos == 1)
		{
			first = ptr->next;
			printf("\nElement deleted");
		}
		else
		{
			while (ptr != NULL)
			{
				ptr = ptr->next;
				cnt = cnt + 1;
			}
			if (pos > 0 && pos <= cnt)
			{
				ptr = first;
				for (i = 1; i < pos; i++)
				{
					prev = ptr;
					ptr = ptr->next;
				}
				prev->next = ptr->next;
			}
			else
			{
				printf("Position is out of range");
			}
			free(ptr);
			printf("\nElement deleted");
		}
	}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
	int oldval, newval, flag = 0;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to update\n");
	}
	else
	{
		printf("\nEnter the value to be updated:");
		scanf("%d", &oldval);
		printf("\nEnter the newvalue:");
		scanf("%d", &newval);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			if (ptr->value == oldval)
			{
				ptr->value = newval;
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nUpdated Successfully");
		}
		else
		{
			printf("\nValue not found in List");
		}
	}
}

/*
* searching an element in a non-empty list
*/
void search()
{
	int flag = 0, key, pos = 0;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list\n");
	}
	else
	{
		printf("\nEnter the value to search");
		scanf("%d", &key);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			pos = pos + 1;
			if (ptr->value == key)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nElement %d found at %d position\n", key, pos);
		}
		else
		{
			printf("\nElement %d not found in list\n", key);
		}
	}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to display\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

Open in new window


please help ...I am not getting

its showing more errors like void can not assigned to snode...

please edit the code ...
Lines 10 -14 You need to change to the following - we are storing a pointer to a char in the node now
struct node
{
	char * value;
	struct node *next ;
};

Open in new window


Line 189 void insert_node_pos()
You are still capturing an int in the scanf

create_node (line 116) has a ; after it and is now declared type void instead of snode *
void create_node(char *);

Open in new window


Should be for both prototype and declaration
snode * create_node(char *);

Open in new window


Move typdef node snode above prototype of create_node so the compiler knows what snode is.

Here is my updated code - not guaranteeing it works but it compiles
/*
* C Program to Implement Singly Linked List using Dynamic Memory Allocation
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
typedef struct node
{
	char * value;
	struct node *next ;
} snode;


snode* create_node(char *);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
#define MAX_FILE_PATH 256
const char * cstrFilePath = "c:\\projects\\data\\files\\";

snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
	int ch;
	char ans = 'Y';

	while (ans == 'Y' || ans == 'y')
	{
		printf("\n---------------------------------\n");
		printf("\nOperations on singly linked list\n");
		printf("\n---------------------------------\n");
		printf("\n1.Insert node at first");
		printf("\n2.Insert node at last");
		printf("\n3.Insert node at position");
		printf("\n4.Sorted Linked List in Ascending Order");
		printf("\n5.Delete Node from any Position");
		printf("\n6.Update Node Value");
		printf("\n7.Search Element in the linked list");
		printf("\n8.Display List from Beginning to end");
		printf("\n9.Display List from end using Recursion");
		printf("\n10.Exit\n");
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("\nEnter your choice");
		scanf("%d", &ch);

		switch (ch)
		{
		case 1:
			printf("\n...Inserting node at first...\n");
			insert_node_first();
			break;
		case 2:
			printf("\n...Inserting node at last...\n");
			insert_node_last();
			break;
		case 3:
			printf("\n...Inserting node at position...\n");
			insert_node_pos();
			break;
		case 4:
			printf("\n...Sorted Linked List in Ascending Order...\n");
			sorted_ascend();
			break;
		case 5:
			printf("\n...Deleting Node from any Position...\n");
			delete_pos();
			break;
		case 6:
			printf("\n...Updating Node Value...\n");
			update_val();
			break;
		case 7:
			printf("\n...Searching Element in the List...\n");
			search();
			break;
		case 8:
			printf("\n...Displaying List From Beginning to End...\n");
			display();
			break;
/*		case 9:
			printf("\n...Displaying List From End using Recursion...\n");
			rev_display(first);
			break;*/
		case 10:
			printf("\n...Exiting...\n");
			return 0;
			break;
		default:
			printf("\n...Invalid Choice...\n");
			break;
		}
		printf("\nYOU WANT TO CONTINUE (Y/N)");
		scanf(" %c", &ans);
	}
	return 0;
}

/*
* Creating Node
*/
snode* create_node(char * val)
{
	newnode = (snode *)malloc(sizeof(snode));
	if (newnode == NULL)
	{
		printf("\nMemory was not allocated");
		return 0;
	}
	else
	{
		newnode->value = (char *)malloc(strlen(val) + 1);
		if (newnode->value) {
			strcpy(newnode->value, val);
		}
		newnode->next = NULL;
		return newnode;
	}
}

/*
* Inserting Node at First
*/
void insert_node_first()
{
	char val[MAX_FILE_PATH];


	printf("\nEnter the value for the node:");
	scanf("%s", val);
	newnode = create_node(val);
	if (first == last && first == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		temp = first;
		first = newnode;
		first->next = temp;
	}
	printf("\n----INSERTED----");
}

/*
* Inserting Node at Last
*/
void insert_node_last()
{
		char val[MAX_FILE_PATH];

		printf("\nEnter the value for the node:");
		scanf("%s", val);
		newnode = create_node(val);
	if (first == last && last == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		last->next = newnode;
		last = newnode;
		last->next = NULL;
	}
	printf("\n----INSERTED----");
}

/*
* Inserting Node at position
*/
	void insert_node_pos()
	{
		int pos, cnt = 0, i;
		char val[MAX_FILE_PATH];
		printf("\nEnter the value for the Node:");
		scanf("%s", val);
		newnode = create_node(val);
		printf("\nEnter the position ");
		scanf("%d", &pos);
		ptr = first;
		while (ptr != NULL)
		{
			ptr = ptr->next;
			cnt++;
		}
		if (pos == 1)
		{
			if (first == last && first == NULL)
			{
				first = last = newnode;
				first->next = NULL;
				last->next = NULL;
			}
			else
			{
				temp = first;
				first = newnode;
				first->next = temp;
			}
			printf("\nInserted");
		}

	else if (pos>1 && pos <= cnt)
	{
		ptr = first;
		for (i = 1; i < pos; i++)
		{
			prev = ptr;
			ptr = ptr->next;
		}
		prev->next = newnode;
		newnode->next = ptr;
		printf("\n----INSERTED----");
	}
	else
	{
		printf("Position is out of range");
	}
}

/*
* Sorted Linked List
*/
void sorted_ascend()
{
	snode *nxt;
	char * t;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No elements to sort\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			for (nxt = ptr->next; nxt != NULL; nxt = nxt->next)
			{
				if (strcmpi(ptr->value, nxt->value) > 0)
				{
					t = ptr->value;
					ptr->value = nxt->value;
					nxt->value = t;
				}
			}
		}
		printf("\n---Sorted List---");
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
	int pos, cnt = 0, i;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No node to delete\n");
	}
	else
	{
		printf("\nEnter the position of value to be deleted:");
		scanf(" %d", &pos);
		ptr = first;
		if (pos == 1)
		{
			first = ptr->next;
			printf("\nElement deleted");
		}
		else
		{
			while (ptr != NULL)
			{
				ptr = ptr->next;
				cnt = cnt + 1;
			}
			if (pos > 0 && pos <= cnt)
			{
				ptr = first;
				for (i = 1; i < pos; i++)
				{
					prev = ptr;
					ptr = ptr->next;
				}
				prev->next = ptr->next;
			}
			else
			{
				printf("Position is out of range");
			}
			free(ptr);
			printf("\nElement deleted");
		}
	}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
	int flag = 0;
	char newval[MAX_FILE_PATH];
	char oldval[MAX_FILE_PATH];

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to update\n");
	}
	else
	{
		printf("\nEnter the value to be updated:");
		scanf("%s", oldval);
		printf("\nEnter the newvalue:");
		scanf("%s", newval);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			if (stricmp(ptr->value,oldval) == 0)
			{
				if (ptr->value) {
					free(ptr->value);
				}
				ptr->value = (char *) malloc(strlen(newval) + 1);
				strcpy(ptr->value, newval);
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nUpdated Successfully");
		}
		else
		{
			printf("\nValue not found in List");
		}
	}
}

/*
* searching an element in a non-empty list
*/
void search()
{
	int flag = 0, pos = 0;
	char key[MAX_FILE_PATH];
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list\n");
	}
	else
	{
		printf("\nEnter the value to search");
		scanf("%s", key);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			pos = pos + 1;
			if (stricmp(ptr->value,key) == 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nElement %d found at %d position\n", key, pos);
		}
		else
		{
			printf("\nElement %d not found in list\n", key);
		}
	}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to display\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

Open in new window

Thank you For quick responses...I tried code... NULL is showing error
NULL is showing error
This does not make sense to me - I don't know what you are telling me. If there is an error - post it. If the code does no work describe the problem.

I have not tested the code - I have just fixed the bits so that it compiles.

Can I ask what the background is here? Your C skills seem to be minimal - not sure what you are hoping to achieve with this exercise. A linked list tutorial is standard teaching example but you are expected to know how it works and how the code works - and from what I am seeing you don't have either.

Where does this exercise come from?
Sorry . I will find error by myself... ... Actually i am in hurry so I am trying to run code and finishing it off.. My mistake. I ll work with patience ...  Thank you very much for responses
When I run the code it was showing NULL in not defined...I solved it.


other error I got-->
unresolved external symbol _printf referenced in function _delete_pos      

I usually stuck with this type error..What is it?
When I run the code it was showing NULL in not defined...I solved it.
Awesome!
unresolved external symbol _printf referenced in function _delete_pos
What is your platform (OS + Compiler)?

The error is a linking error - it is saying that it found printf in your application but it can't find the library that contains the definition of the function. But it does not make sense because printf is used throughout your program - did this program compile before you made any changes.
Visual studio 2015 GCC compiler.

Ha It ran before only error was void can not assign to snode

but after making changes its showing like this
Can you post your code again.
There are some headers missing so it was  showing error. but its compiling

but in output window I entered path of file.  and when I choose option for display element in list..Its showing some number. See below
---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Sorted Linked List in Ascending Order
5.Delete Node from any Position
6.Update Node Value
7.Search Element in the linked list
8.Display List from Beginning to end
9.Display List from end using Recursion
10.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice1

...Inserting node at first...

Enter the value for the node:C:\\Users\\meghav\\Desktop\\meg\\input.txt

----INSERTED----
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Sorted Linked List in Ascending Order
5.Delete Node from any Position
6.Update Node Value
7.Search Element in the linked list
8.Display List from Beginning to end
9.Display List from end using Recursion
10.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice8

...Displaying List From Beginning to End...
3197272
YOU WANT TO CONTINUE (Y/N)

Open in new window

Look at your display function
void display()
{
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to display\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			// HERE: YOU ARE USING %d (INTEGER) ON A VALUE THAT IS A STRING
			// CHANGE TO %s                    
			printf("%d\t", ptr->value);
		}
	}
}

Open in new window

Yup I changed it to %s.

Its showing full path what I entered. but I have doubt. Its taking only string what I entered ryt? not file. Am I right?
Yes we are building this slowly.

1. Create a list of filenames in the system
2. Build the operations you want to perform on those files. When you invoke one of those options you give it the path, the function then opens the file and displays it, or moves it or deletes it depending on requirement.
where to write file operations ?
where to write file operations ?
Sorry Megha, I don't know what that means - please can you be more expressive in your comments - this is a voluntary effort on my part, I have many questions open and my own work to deal with I don't have time to try and work out what you are asking - I need you to be very detailed and explicit.
Ok I am sorry . I ll try by own if doubt i ll let you know.

Appreciated you replies... Great job. Thank you .. I ll be back soon
Ok I am sorry . I ll try by own if doubt i ll let you know.
Just to be clear - I was not asking you to not ask questions - just to make it clear what you want done.

The reason for my earlier comment is part of helping you with this problem. If you are going to be a developer it is vitally important that you are able to clearly convey instructions and requirements. If you are not clear then the wrong code gets built.

Part of what you need to do is formulate in your mind what you it is you are trying to achieve and then try to express that in writing. This is very important because when you do that - when you take what is in your mind and put it on paper (or a post) it forces you to think about it in a different way and it helps you to solve your problem.

Your application needs to perform certain operations on files - try to describe in words what those operations are.
ya I need to perform some file operation like.

when we put files on linked list then need to read the file... after that sorting list based on its name(filename) or sort based on which file we put first. then remove file from the list.
How are you going to select which file to read?

Once you have solved that - create a function called

void read_file(char * filename)
{
   // Research fopen / fread/ fgets / functions here
  // Open file with fopen and flags rw
  // Start a loop and read file into a buffer using fread
  // output the data to the screen with printf / puts 
  // loop reading N byte chunks (where N is the size of your buffer) until you hit the EOF
  // close the file
}

Open in new window

When you call this function you do so with the pnode->value of the node in the list you want to read.
ok Thank you.. I will work on it. Cu soon ..Hansen
Hai I tried to change code ...

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
typedef struct node
{
	char * value;
	struct node *next;
} snode;


snode* create_node(char *);
void savetofile();
void readfromfile();
//void insert_node_first();
//void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
#define MAX_FILE_PATH 256
const char * cstrFilePath = "c:\\projects\\data\\files\\";

snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
	int ch;
	char ans = 'Y';

	while (ans == 'Y' || ans == 'y')
	{
		printf("\n---------------------------------\n");
		printf("\nOperations on singly linked list\n");
		printf("\n---------------------------------\n");
		printf("\n1.Insert node at first");
		printf("\n2.Insert node at last");
		printf("\n3.Insert node at position");
		printf("\n4.Sorted Linked List in Ascending Order");
		printf("\n5.Delete Node from any Position");
		printf("\n6.Update Node Value");
		printf("\n7.Search Element in the linked list");
		printf("\n8.Display List from Beginning to end");
		printf("\n9.Display List from end using Recursion");
		printf("\n10.Exit\n");
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("\nEnter your choice");
		scanf("%d", &ch);

		switch (ch)
		{
		case 1:
			printf("\n...Inserting node at first...\n");
			insert_node_first();
			break;
		case 2:
			printf("\n...Inserting node at last...\n");
			insert_node_last();
			break;
		case 3:
			printf("\n...Inserting node at position...\n");
			insert_node_pos();
			break;
		case 4:
			printf("\n...Sorted Linked List in Ascending Order...\n");
			sorted_ascend();
			break;
		case 5:
			printf("\n...Deleting Node from any Position...\n");
			delete_pos();
			break;
		case 6:
			printf("\n...Updating Node Value...\n");
			update_val();
			break;
		case 7:
			printf("\n...Searching Element in the List...\n");
			search();
			break;
		case 8:
			printf("\n...Displaying List From Beginning to End...\n");
			display();
			break;
			/*		case 9:
			printf("\n...Displaying List From End using Recursion...\n");
			rev_display(first);
			break;*/
		case 10:
			printf("\n...Exiting...\n");
			return 0;
			break;
		default:
			printf("\n...Invalid Choice...\n");
			break;
		}
		printf("\nYOU WANT TO CONTINUE (Y/N)");
		scanf(" %c", &ans);
	}
	return 0;
}

/*
* Creating Node
*/
snode* create_node(char * val)
{
	newnode = (snode *)malloc(sizeof(snode));
	if (newnode == NULL)
	{
		printf("\nMemory was not allocated");
		return 0;
	}
	else
	{
		newnode->value = (char *)malloc(strlen(val) + 1);
		if (newnode->value) {
			strcpy(newnode->value, val);
		}
		newnode->next = NULL;
		return newnode;
	}
}

/*
* Inserting Node at First
*/
/*void insert_node_first()
{
	char val[MAX_FILE_PATH];


	printf("\nEnter the value for the node:");
	scanf("%s", val);
	newnode = create_node(val);
	if (first == last && first == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		temp = first;
		first = newnode;
		first->next = temp;
	}
	printf("\n----INSERTED----");
}

/*
* Inserting Node at Last
*/
/*void insert_node_last()
{
	char val[MAX_FILE_PATH];

	printf("\nEnter the value for the node:");
	scanf("%s", val);
	newnode = create_node(val);
	if (first == last && last == NULL)
	{
		first = last = newnode;
		first->next = NULL;
		last->next = NULL;
	}
	else
	{
		last->next = newnode;
		last = newnode;
		last->next = NULL;
	}
	printf("\n----INSERTED----");
}
*/

void savetofile()
{
	FILE *fp;
	struct prs *ptrthis;

	if (first == NULL)
	{
		printf("Empty.\n");
		return;
	}

	ptrthis = first;
	if ((fp = fopen("c:\\database.txt", "wb")) == NULL)
	{
		printf("No such file.\n");
		return;
	}
	else
	{
		fwrite(&ptrthis, sizeof(*ptrthis), 1, fp);
		fclose(fp);
		printf("Saved to disk.\n");
		system("PAUSE");
	}
}

void readfromfile()
{
	FILE *fp;
	struct prs temp;
	struct prs *next = NULL;

	fp = fopen("c:\\database.txt", "ab");

	while (fread(&temp, sizeof(temp), 1, fp) == 1)
	{
		struct prs *new = malloc(sizeof *new);

		// link new node into the list
		if (first == NULL) {
			first = new;
		}
		else {
			next->ptrnext = new;
		}

		// copy data
		new->number = temp.number;
		new->ptrnext = NULL;

		// this is now the tail of the list
		next = new;
	}

	fclose(fp);
}
/*
* Inserting Node at position
*/
void insert_node_pos()
{
	int pos, cnt = 0, i;
	char val[MAX_FILE_PATH];
	printf("\nEnter the file for the Node:");
	scanf("%s", val);
	newnode = create_node(val);
	printf("\nEnter the position ");
	scanf("%d", &pos);
	ptr = first;
	while (ptr != NULL)
	{
		ptr = ptr->next;
		cnt++;
	}
	if (pos == 1)
	{
		if (first == last && first == NULL)
		{
			first = last = newnode;
			first->next = NULL;
			last->next = NULL;
		}
		else
		{
			temp = first;
			first = newnode;
			first->next = temp;
		}
		printf("\nInserted");
	}

	else if (pos>1 && pos <= cnt)
	{
		ptr = first;
		for (i = 1; i < pos; i++)
		{
			prev = ptr;
			ptr = ptr->next;
		}
		prev->next = newnode;
		newnode->next = ptr;
		printf("\n----INSERTED----");
	}
	else
	{
		printf("Position is out of range");
	}
}

/*
* Sorted Linked List
*/
void sorted_ascend()
{
	snode *nxt;
	char * t;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No elements to sort\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			for (nxt = ptr->next; nxt != NULL; nxt = nxt->next)
			{
				if (strcmpi(ptr->value, nxt->value) > 0)
				{
					t = ptr->value;
					ptr->value = nxt->value;
					nxt->value = t;
				}
			}
		}
		printf("\n---Sorted List---");
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
	int pos, cnt = 0, i;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No node to delete\n");
	}
	else
	{
		printf("\nEnter the position of value to be deleted:");
		scanf(" %d", &pos);
		ptr = first;
		if (pos == 1)
		{
			first = ptr->next;
			printf("\nElement deleted");
		}
		else
		{
			while (ptr != NULL)
			{
				ptr = ptr->next;
				cnt = cnt + 1;
			}
			if (pos > 0 && pos <= cnt)
			{
				ptr = first;
				for (i = 1; i < pos; i++)
				{
					prev = ptr;
					ptr = ptr->next;
				}
				prev->next = ptr->next;
			}
			else
			{
				printf("Position is out of range");
			}
			free(ptr);
			printf("\nElement deleted");
		}
	}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
	int flag = 0;
	char newval[MAX_FILE_PATH];
	char oldval[MAX_FILE_PATH];

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to update\n");
	}
	else
	{
		printf("\nEnter the value to be updated:");
		scanf("%s", oldval);
		printf("\nEnter the newvalue:");
		scanf("%s", newval);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			if (stricmp(ptr->value, oldval) == 0)
			{
				if (ptr->value) {
					free(ptr->value);
				}
				ptr->value = (char *)malloc(strlen(newval) + 1);
				strcpy(ptr->value, newval);
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nUpdated Successfully");
		}
		else
		{
			printf("\nValue not found in List");
		}
	}
}

/*
* searching an element in a non-empty list
*/
void search()
{
	int flag = 0, pos = 0;
	char key[MAX_FILE_PATH];
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list\n");
	}
	else
	{
		printf("\nEnter the value to search");
		scanf("%s", key);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			pos = pos + 1;
			if (stricmp(ptr->value, key) == 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nElement %d found at %d position\n", key, pos);
		}
		else
		{
			printf("\nElement %d not found in list\n", key);
		}
	}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to display\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%s\t", ptr->value);
		}
	}
}

Open in new window


ofcourse its not working... Need guidance
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
typedef struct node
{
	char * value;
	struct node *next;
} snode;


snode* create_node(char *);
void savetofile();
void readfromfile();
//void insert_node_first();
//void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
#define MAX_FILE_PATH 256
const char * cstrFilePath = "c:\\projects\\data\\files\\";

snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
	int ch;
	char ans = 'Y';

	while (ans == 'Y' || ans == 'y')
	{
		printf("\n---------------------------------\n");
		printf("\nOperations on singly linked list\n");
		printf("\n---------------------------------\n");
		printf("\n1.Insert node at first");
		printf("\n2.Insert node at last");
		printf("\n3.Insert node at position");
		printf("\n4.Sorted Linked List in Ascending Order");
		printf("\n5.Delete Node from any Position");
		printf("\n6.Update Node Value");
		printf("\n7.Search Element in the linked list");
		printf("\n8.Display List from Beginning to end");
		printf("\n9.Display List from end using Recursion");
		printf("\n10.Exit\n");
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("\nEnter your choice");
		scanf("%d", &ch);

		switch (ch)
		{
		case 1:
			printf("\n...save file first...\n");
			savetofile();
			break;
		case 2:
			printf("\n...read from file last...\n");
			readfromfile();
			break;
		case 3:
			printf("\n...Inserting node at position...\n");
			insert_node_pos();
			break;
		case 4:
			printf("\n...Sorted Linked List in Ascending Order...\n");
			sorted_ascend();
			break;
		case 5:
			printf("\n...Deleting Node from any Position...\n");
			delete_pos();
			break;
		case 6:
			printf("\n...Updating Node Value...\n");
			update_val();
			break;
		case 7:
			printf("\n...Searching Element in the List...\n");
			search();
			break;
		case 8:
			printf("\n...Displaying List From Beginning to End...\n");
			display();
			break;
			/*		case 9:
			printf("\n...Displaying List From End using Recursion...\n");
			rev_display(first);
			break;*/
		case 10:
			printf("\n...Exiting...\n");
			return 0;
			break;
		default:
			printf("\n...Invalid Choice...\n");
			break;
		}
		printf("\nYOU WANT TO CONTINUE (Y/N)");
		scanf(" %c", &ans);
	}
	return 0;
}

/*
* Creating Node
*/
snode* create_node(char * val)
{
	newnode = (snode *)malloc(sizeof(snode));
	if (newnode == NULL)
	{
		printf("\nMemory was not allocated");
		return 0;
	}
	else
	{
		newnode->value = (char *)malloc(strlen(val) + 1);
		if (newnode->value) {
			strcpy(newnode->value, val);
		}
		newnode->next = NULL;
		return newnode;
	}
}



void savetofile()
{
	FILE *fp;
	struct prs *ptrthis;

	if (first == NULL)
	{
		printf("Empty.\n");
		return;
	}

	ptrthis = first;
	if ((fp = fopen("C:\\Users\\meghav\\Desktop\\input.txt", "wb")) == NULL)
	{
		printf("No such file.\n");
		return;
	}
	else
	{
		fwrite(&ptrthis, sizeof(*newnode), 1, fp);
		fclose(fp);
		printf("Saved to disk.\n");
		system("PAUSE");
	}
}

void readfromfile()
{
	FILE *fp;
	struct  temp;
	struct prs *next = NULL;

	fp = fopen("C:\\Users\\meghav\\Desktop\\input.txt", "ab");

	while (fread(&temp, sizeof(temp), 1, fp) == 1)
	{
		struct prs *new = malloc(sizeof *newnode);

		// link new node into the list
		if (first == last && last == NULL)
		{
			first = last = newnode;
			first->next = NULL;
			last->next = NULL;
		}
		else
		{
			last->next = newnode;
			last = newnode;
			last->next = NULL;
		}
		// this is now the tail of the list
		next = new;
	}

	fclose(fp);
}
/*
* Inserting Node at position
*/
void insert_node_pos()
{
	int pos, cnt = 0, i;
	char val[MAX_FILE_PATH];
	printf("\nEnter the file for the Node:");
	scanf("%s", val);
	newnode = create_node(val);
	printf("\nEnter the position ");
	scanf("%d", &pos);
	ptr = first;
	while (ptr != NULL)
	{
		ptr = ptr->next;
		cnt++;
	}
	if (pos == 1)
	{
		if (first == last && first == NULL)
		{
			first = last = newnode;
			first->next = NULL;
			last->next = NULL;
		}
		else
		{
			temp = first;
			first = newnode;
			first->next = temp;
		}
		printf("\nInserted");
	}

	else if (pos>1 && pos <= cnt)
	{
		ptr = first;
		for (i = 1; i < pos; i++)
		{
			prev = ptr;
			ptr = ptr->next;
		}
		prev->next = newnode;
		newnode->next = ptr;
		printf("\n----INSERTED----");
	}
	else
	{
		printf("Position is out of range");
	}
}

/*
* Sorted Linked List
*/
void sorted_ascend()
{
	snode *nxt;
	char * t;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No elements to sort\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			for (nxt = ptr->next; nxt != NULL; nxt = nxt->next)
			{
				if (strcmpi(ptr->value, nxt->value) > 0)
				{
					t = ptr->value;
					ptr->value = nxt->value;
					nxt->value = t;
				}
			}
		}
		printf("\n---Sorted List---");
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%d\t", ptr->value);
		}
	}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
	int pos, cnt = 0, i;

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No node to delete\n");
	}
	else
	{
		printf("\nEnter the position of value to be deleted:");
		scanf(" %d", &pos);
		ptr = first;
		if (pos == 1)
		{
			first = ptr->next;
			printf("\nElement deleted");
		}
		else
		{
			while (ptr != NULL)
			{
				ptr = ptr->next;
				cnt = cnt + 1;
			}
			if (pos > 0 && pos <= cnt)
			{
				ptr = first;
				for (i = 1; i < pos; i++)
				{
					prev = ptr;
					ptr = ptr->next;
				}
				prev->next = ptr->next;
			}
			else
			{
				printf("Position is out of range");
			}
			free(ptr);
			printf("\nElement deleted");
		}
	}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
	int flag = 0;
	char newval[MAX_FILE_PATH];
	char oldval[MAX_FILE_PATH];

	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to update\n");
	}
	else
	{
		printf("\nEnter the value to be updated:");
		scanf("%s", oldval);
		printf("\nEnter the newvalue:");
		scanf("%s", newval);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			if (stricmp(ptr->value, oldval) == 0)
			{
				if (ptr->value) {
					free(ptr->value);
				}
				ptr->value = (char *)malloc(strlen(newval) + 1);
				strcpy(ptr->value, newval);
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nUpdated Successfully");
		}
		else
		{
			printf("\nValue not found in List");
		}
	}
}

/*
* searching an element in a non-empty list
*/
void search()
{
	int flag = 0, pos = 0;
	char key[MAX_FILE_PATH];
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list\n");
	}
	else
	{
		printf("\nEnter the value to search");
		scanf("%s", key);
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			pos = pos + 1;
			if (stricmp(ptr->value, key) == 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
		{
			printf("\nElement %d found at %d position\n", key, pos);
		}
		else
		{
			printf("\nElement %d not found in list\n", key);
		}
	}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
	if (first == NULL)
	{
		ISEMPTY;
		printf(":No nodes in the list to display\n");
	}
	else
	{
		for (ptr = first; ptr != NULL; ptr = ptr->next)
		{
			printf("%s\t", ptr->value);
		}
	}
}

Open in new window


I changed code little
and its working
but i have some doubts...
it reads from file and writes to file... but its writing something unknown letter.
i wanted to know what is happening in this code
Not sure which part you are referring to
In the read file though you have this
fp = fopen("C:\\Users\\meghav\\Desktop\\input.txt", "ab");

Open in new window


ab should be rb - a means append - which makes no sense in this context.

Are you trying to read and write the list to a file? Which part is not working?
Yes I am trying read and write
ab should be rb - a means append - which makes no sense in this context.

thank you..
can i do this for multiple files ?
can i do this for multiple files ?
In what way?
by repeating read file  and save file  functions.
reading and saving what - please give me full details of what you want - if it is less than one line I will not read it.
void savetofile()
{
	FILE *fp;
	struct prs *ptrthis;

	if (first == NULL)
	{
		printf("Empty.\n");
		return;
	}

	ptrthis = first;
	if ((fp = fopen("C:\\Users\\meghav\\Desktop\\input.txt", "wb")) == NULL)
	{
		printf("No such file.\n");
		return;
	}
	else
	{
		fwrite(&ptrthis, sizeof(*newnode), 1, fp);
		fclose(fp);
		printf("Saved to disk.\n");
		system("PAUSE");
	}
}

void readfromfile()
{
	FILE *fp;
	struct  temp;
	struct prs *next = NULL;

	fp = fopen("C:\\Users\\meghav\\Desktop\\input.txt", "ab");

	while (fread(&temp, sizeof(temp), 1, fp) == 1)
	{
		struct prs *new = malloc(sizeof *newnode);

		// link new node into the list
		if (first == last && last == NULL)
		{
			first = last = newnode;
			first->next = NULL;
			last->next = NULL;
		}
		else
		{
			last->next = newnode;
			last = newnode;
			last->next = NULL;
		}
		// this is now the tail of the list
		next = new;
	}

	fclose(fp);
}

Open in new window


this above function take 1 file and do  operations ryt...

similar way is it possible to call these functions again and again for different files...?

Is it clear?
Yes of course - depends entirely on use case - in the context of your application you could iterate over the linked list and for each node call the function. You would need to change the function to be able to take a parameter filename that you use to do the read operation on.

Something like

snode * itr = first;
while(itr != NULL) {
    readfromfile(itr->value);
    itr = itr->next;
}
...
void readfromfile(char * pFile)
{
  FILE * fp;
  fp = fopen(pFile, "rb");
  ...
}

Open in new window

I did not get you where to add how it is going to work..


Can u plz explain
Hi
I need to create node for all files?
snode * itr = first;
while(itr != NULL) {
    readfromfile(itr->value);
    itr = itr->next;
}

Open in new window

this i should add at top?

please help me out
waiting for reply
I need to create node for all files?
I am not quite with you - you are looping through a list of files - what do you still need to do?
I am trying iterate files in directory  in linked list... see this code is ...

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include<fcntl.h>
#include<dirent.h>

struct node *head = NULL;
struct node *prev = NULL;
char *full_path = NULL;

DIR *dir;
struct dirent *dp;

dir = opendir ("C:\\Users\\meghav\\Desktop\\meg");
	if (dp)
		while ((dp = readdir(dir)) != NULL)
		{
			if (dp->d_type == DT_REG) {
				struct node *last = (struct node*) malloc(sizeof(struct node));
				full_path = strnew(strlen("C:\\Users\\meghav\\Desktop\\meg") + strlen(dir->d_name);
				strcpy(full_path, "C:\\Users\\meghav\\Desktop\\meg");
				strcat(full_path, dir->d_name);
				last->song_id = open(full_path, O_RDONLY);
				last->name = full_path;
				last->next = NULL;
				if (head == NULL)

					head = last;
				prev = last;

				else

					prev->next = last;
				prev = last;

				//prev.next = &last;
				//prev = last;
				printf("%s\n", dir->d_name);
			}

			closedir(d);

		}
	printf("printing\n");

	struct node *root = head;
	{

		while (root != NULL)
		{
			printf("%s\n", root->name);
			root = root->next;
		}
	}

Open in new window

Ok I see - and? Is it not working?
no Not working
Does it give an error
Does it compile
Does it crash
Does it produce unexpected results
Does it attempt to call its mother ship in Galaxy X12A

I need as much information as you can give me.
These errors I got

Severity	Code	Description	Project	File	Line
Error (active)		expected a declaration	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	39
Error	C2040	'dir': 'int' differs in levels of indirection from 'DIR *'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	14
Error (active)		expected a ')'	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	38
Error (active)		expected a declaration	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	15
Error (active)		expected a declaration	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	43
Error (active)		expected a declaration	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	47
Error (active)		expected a type specifier	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	38
Error (active)		expression must have a constant value	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	46
Error	C2449	found '{' at file scope (missing function header?)	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	47
Error (active)		function call is not allowed in a constant expression	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	14
Error	C2099	initializer is not a constant	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	14
Error	C2099	initializer is not a constant	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	46
Error	C2059	syntax error: ')'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	44
Error	C2059	syntax error: 'if'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	15
Error	C2059	syntax error: 'string'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	44
Error	C2059	syntax error: '}'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	54
Error	C2143	syntax error: missing ')' before 'string'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	44
Error	C2143	syntax error: missing '{' before 'string'	link	c:\users\meghav\documents\visual studio 2015\projects\link\link\source.c	44
Error (active)		variable "dir" is not a type name	link	c:\Users\meghav\Documents\Visual Studio 2015\Projects\link\link\Source.c	38

Open in new window

And you have you attempted to go and resolve those errors?

The code you posted appears to be outside of a function - why is that?
And you have you attempted to go and resolve those errors?

I tried solving but I am not getting
The code you posted appears to be outside of a function - why is that?

I copied it...I dont know.
Unfortunately to be able to solve this problem you will need at least basic C coding skills. If you are just copying and pasting code without understanding what it does you are not learning.

C - code has to run in a function - you can't just type it raw into a file as you have it - you have a good sample of working code in your earlier posts - all with functions - was that all copied and pasted code?
No I tried writing it by own... For 1 file in linked list it was ok. But multiple files in linked list and iterate those is little difficult for me.
so I copied thats it....I ll go through it and get back to you.
With your guidance only i sumup my self in coding.. Thank you
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.