Question

Linked list is a type of data structure. Each item of the list holds a data and a pointer to the next item. Head is a pointer that points to the first element. Tail is the last element of the list, which points to NULL. Linked list can be implemented using structures, consisting of some data types and a pointer. Write a program that creates the linked list given in the following figure and prints out the data in order. For this problem, you dont need to do any scanf. Follow the codes in article 12.8 and 12.9 of textbook and ch-12 slides of class. data next data next data next data next 10 head
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//################################## PGM START ####################################

#include<stdio.h>
#include<stdlib.h>

//structure with a integer value and next pointer
struct node{
   int value;
   struct node *next;
};
//function which cretes a newnode
struct node* create_node(int data){
   struct node* newnode=(struct node*) malloc(sizeof(struct node));
    newnode->value = data;
    newnode->next = NULL;
    return newnode;
}
//function to insert a data to list pointed by head
void insert(struct node **head,int data){
   struct node* n1=create_node(data);
   n1->next=*head;
   *head=n1;
}
//function to print values in list
void printLinkedList(struct node* head){
   while(head->next!=NULL){
       printf("%d --> ",head->value);
       head=head->next;
   }
   printf("%d\n",head->value);
}
//main method
int main(){
   //creatin a head pointer
   struct node* head=NULL;
   //inserting values to list
   insert(&head,1);
   insert(&head,2);
   insert(&head,10);
   insert(&head,3);
  
   //prinitng values in list
   printf("List elements\n");
   printLinkedList(head);
   return 0;
}

//################################# PGM END #######################################

OUTPUT
##########

C:Users IBM ADMIN Desktop Job PGMsLink11.exe List elements 3 -- 10 2 1 Process exited after 0.07856 seconds with return value 0 Press any key to continue.. .

Add a comment
Know the answer?
Add Answer to:
Linked list is a type of data structure. Each item of the list holds a data...
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
  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • I have a Java Data Structures project and we're creating a linked list and an iterator...

    I have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code public class MyLinkedList extends CS20bLinkedList implements Iterable<Integer> {        @Override      public void add(int value){                        Node tail = head;            if(head == null){                        head = new Node(value);             //System.out.println("Very Fun! "+head.value);                        return;                }else{           ...

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

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

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

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

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

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