Write a function to insert a name at the end of a linked list? (C)
I have a linked list:
struct node {
char person[100];
struct node *next;
};
int main() {
struct node *new_node;
new_node=malloc(sizeof(struct node));
printf("Please enter a name:\n");
scanf("%s", );
}
How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm having trouble.
#include<stdio.h>
#include<stdlib.h>
// creating a structure
struct node
{
char person[100];
struct node *next;
} node;
struct node *add_data(struct node *head ,struct node *new)
{
struct node *temp;
new->next=NULL;
// if no nodes are there
if(head==NULL)
head=new;
else
{
// traverse the list till the
end
temp=head;
while(temp->next!=NULL)
temp=temp->next;
// adding list to the queue
temp->next=new;
}
return head;
}
void display(struct node *head)
{
if(head==NULL)
printf("\nNo Elements List is empty");
else
printf("The data is\n");
while(head!=NULL)
{
printf("%s\t",head->person);
head=head->next;
}
}
int main()
{
struct node *new_node,*head;
new_node=malloc(sizeof(struct node));
printf("Please enter a name:\n");
scanf("%s",&new_node->person );
head=add_data(head,new_node);
display(head);
return 0;
}

Write a function to insert a name at the end of a linked list? (C) I...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list { ...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...
Here is a function for some code im working on in C. Right now it accepts 2 different user inputs and stores them as city and distance. What I want is for the user to input only once. For example "Enter the city name and distance from previous city: Arlington 200". I need to separate that string into values for city and distance. I think I need to use tokens but im not sure how. void addCity () { char...
****find_last_node.c
#include <stdio.h>
#include "linked_list.h"
int main(){
struct node *linked_list = NULL;
linked_list = add_to_list(linked_list, 5,
'a');
linked_list = add_to_list(linked_list, 10, 'b');
linked_list = add_to_list(linked_list, 4, 'c');
linked_list = add_to_list(linked_list, 10, 'd');
linked_list = add_to_list(linked_list, 5, 'e');
linked_list = add_to_list(linked_list, 7, 'f');
linked_list = add_to_list(linked_list, 5, 'g');
linked_list = add_to_list(linked_list, 3, 'h');
int search_number;
printf("Enter number you want to search for:");
scanf("%d", &search_number);
struct node *last_node = find_last(linked_list,
search_number);
if (last_node != NULL)
{
printf("Node found: value = %d and...
^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node { int info; struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) { DL *left,*right,*curr; curr = start; if( start == NULL) { printf("\nDoubly...
****Using C and only C****
I have some C code that has the function addRecord, to add a
record to a linked list of records. However, when I run it, the
program exits after asking the user to input the address. See
picture below:
Here is my code:
#include<stdio.h>
#include<stdlib.h>
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};
void addRecord(struct record* newRecord) //Function For Adding
Record at last in a SinglyLinkedList
{
struct record *current,*start,*temp;...
I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() { char** name; name = (char**)malloc(2*sizeof(char*)); name[0] = (char*)malloc(100*sizeof(char)); name[1]...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...