Question

Must be written and C, and compile with MinGW. Thank you!

am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lis

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**** List.h ****/


/***** List Header File ***/

//Structure Definition
typedef struct node
{
int id;
char *name;
struct node* next;
} node;

//Function prototypes declared here

node getNode(int id, char name);

void print_list(node **head);

int list_len(node **head);

void add_front(node *head, int id, char name);

void add_end(node *head, int id, char name);

void rem_front(node** head);

void rem_end(node** head);

void create_list(node **head);

/**** List.h Ends ****/

/**** List.c ********/

/ Header Inclusion /
#include "Lists.h"

//Function to construct node
node getNode(int id, char name)
{
node* n = (node*)malloc(sizeof(node));

n->id = id;
n->name = strdup(name);
n->next = NULL;

return n;
}

//Fucntuon to print entire list
void print_list(node **head)
{
node ptr = head;

while(ptr)
{
printf("%d %s\n", ptr->id, ptr->name);
ptr = ptr->next;
}
}

//Function to print length of list
int list_len(node **head)
{
int count = 0;

node ptr = head;

while(ptr)
{
count++;
ptr = ptr->next;
}
return count;
}

//Function to add node at front
void add_front(node *head, int id, char name)
{
node* newNode = getNode(id, name);

newNode->next = *head;
*head = newNode;

}

//Function to add node at end
void add_end(node *head, int id, char name)
{
node* newNode = getNode(id, name);

node ptr = head;

if(ptr == NULL)
{
(*head) = newNode;
}
else
{
while(ptr->next)
{
ptr = ptr->next;
}
ptr->next = newNode;
}

}

//Function to remove from front
void rem_front(node** head)
{
if(*head == NULL)
printf("\nUnder Flow");
else
{
node ptr = head;
head = (head)->next;
free(ptr);
}
}

//Function to remove from end
void rem_end(node** head)
{
if(*head == NULL)
printf("Under Flow");
else
{
if((*head)->next == NULL)
{
rem_front(head);
}
else
{
node ptr = head;
node* prev = ptr;

while(ptr->next)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = NULL;
free(ptr);
}
}
}

//Function to create list until stopped
void create_list(node **head)
{

int num;
char ch, str[30];

do
{
printf("Enter ID : ");
scanf("%d", &num);
getchar();
printf("Enter Name : ");
scanf("%s", str);

add_end(head, num, str);

printf("Do yo want to contine (Y/N) : ");
scanf(" %c", &ch);

} while(ch == 'Y' || ch == 'y');

}

/***** List.c Ends *****/

/***** Main.c *********/

/ C language Used /
/ Program to implement Linked List /

//File Inclusion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Linked List file inclusion
#include "Lists.c"

//Driver Function to test
int main()
{
node* list = NULL;

print_list(&list);

// add_front(&list, 10, "AAA");
// add_front(&list, 20, "BBB");
// add_end(&list, 30, "CCC");
// add_end(&list, 40, "DDD");

create_list(&list);

// rem_end(&list);

print_list(&list);


return 0;
}

/*** Main.c Ends ******/
Output:

causersiTusharlDesktoplNew folder.Main.exe Enter ID 23 Enter Name BOB Do yo want to contine (Y/N) Y Enter ID 67 Enter Name PENote: The program is tested with the provided main function and the output is attached. For any query drop a comment.

Add a comment
Know the answer?
Add Answer to:
Am Specification For this assignment, you will write a multi-file C program to define, implement ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables sh...

    Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables should be initialized to reflect this state. This function is already fully implemented. 1. List(const List<Type>& other): Copy constructor for the linked list. This should create an entirely new linked list with the same number of Nodes and the Values stored these Nodes in the same order as seen the...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT