Question

Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes. Then, the user will tell us whether

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

C code:

#include<stdio.h>
#include<conio.h>
/* A Linked list node */
struct Node {
int value;
struct Node* after;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_value)
{
/* allocate node */
struct Node* new_node = (struct Node *)malloc(sizeof(struct Node));

/* put in the value */
new_node->value = new_value;

/* link the old list to the new node */
new_node->after = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;

// create linked list 10->13->81->32->29
//passing reference of head so that changes
//made in push function are reflected in main as well
push(&head, 29);
push(&head, 32);
push(&head, 81);
push(&head, 13);
push(&head, 10);

char option;
printf("Odd or Even printing (o/e)? ");
scanf("%c",&option);
//if option is o then we'll print odd nodes
if(option=='o'){
if(head!=NULL){//checking if there are more than single node so that we can start from second node.
head= head->after;
}else{
return;
}
while(head!=NULL&& head->after!= NULL){
printf("%d\t",head->value);
head= head->after->after;
}
}else if(option=='e'){
//handling case of single node in the linked list
if(head!=NULL&& head->after==NULL){
printf("%d\t",head->value);
}else{
while(head!=NULL&& head->after!=NULL){
printf("%d\t",head->value);
head= head->after->after;
}
}
// handling the last remaining element
if(head!=NULL){
printf("%d\t",head->value);
}
}
return 0;
}

Output:

C:\Users\HP\Desktop\CHEGG\linked List.exe Odd or even printing (o/e)? e 10 81 29 Process returned o (exo) execution time : 1.

C:\Users\HP\Desktop\CHEGG\linked List.exe Odd or Even printing (o/e)? O 13 32 Process returned © (@xo) execution time : 4.376

Note:- \t in the print statement is used for tab type space while printing.

Screenshots of the code:-

X linked List.c X #include<stdio.h> #include<conio.h> /* A Linked list node */ struct Node { int value; struct Node* after; /

Start here X linkedList.c X push (Ghead, 29); push (Ghead, 32); push(Ghead, 81); push(Ghead, 13); push(Ghead, 10); 31 32 33 3

If you have any queries, please ask in the comments section.

Thanks

Add a comment
Know the answer?
Add Answer to:
Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes....
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
  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

  • Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include...

    Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> //#define constant values #define MAX_URL_LENGTH 50 #define TRUE 1 #define FALSE 0 //typedef for the Element struct which constains a c string to store a URL in the BrowserList typedef struct { char szURL[MAX_URL_LENGTH]; } Element; //Typedef for a node in the doubly linked list (has next and previous pointers). typedef struct NodeDL { Element element; struct NodeDL *pNext;...

  • do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask...

    do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask the user for the data values in the structure. 3. Follow the example above on how to build linked list. Make sure you ask the user for the data values. 4. Return head. in Main do the following: 1. Declare a pointer to structure of type Node. 2. Call the function buildLinkedList. 3. Display the 3 nodes data values.

  • Question 2 (3 points) Given the following singly linked list (a list with four nodes), what...

    Question 2 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last node when the following operations are executed? head->1111-151->11011->1211 Node curr = head; while(curr next !=null) { curr.item = curritem-head. item; curr = cur next; اسرة Node newNode = new Node(8): curr.next = newNode: 23 O 12 OS O21 Question 3 (3 points) Given a recursive method as shown below, what is the return value for P(5)...

  • 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 *))...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  •                                           &nb

                                                                                                                           The language should be in C Specifications Use this structure and constant for the linked list node: #define MAX_STR_LEN     80 struct link_node { char node_str[ MAX_STR_LEN ]; struct link_node *next; }; Your solution should incorporate the following functions // This function is used to compare two nodes. // The return values are: // -1: n1 < n2 // 0: n1 == n2 // +1: n1 > n2 int compare_node( struct link_node *n1, struct link_node *n2 ); // This function...

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