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
  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • 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 LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object)...

    C LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object) typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial term associated with the object int coef;    // power of the polynomial term associated with the object int power;    // pointer to the next object (to facilitate a doubly linked list) struct Term *next;    // pointer to the previous object (to facilitate a doubly linked list) struct Term...

  • 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;...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

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

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