Question

The topic of this laboratory exercise a probably one of the most ieceresting ones you willsee in thi·coure. The idea of linking together dynamicaby-located memory block, ae.. dynamic vaniables) into a thain of them, a owerlul coneept, athough not an easy one to wnderstand nor to program. So, buckke your veat belts and strap on your belmets cause we re eoing for a hard ride herE, We bgn and doing it at run tise is a very p You now know how to dynamicaly creace blocks of memory at run time and access them through pointers (the only way to do sel Remember that such blocks of memory are really instantations type. It is juat another way to instantiate variables from the struct tomplate, ecept now we do R dynamicaly, Therefore, it i bet to define the structure that we want this variable to look e, a a new data type ving the typedef operation. From this point on, this is the only acceptable way to delne a variable type that be instantiated dynamicaly, so get uned to it of structures of the struct Knowing how to dynamically alocate a block of memory as an instance of a struct template and asigning values to its members is rather strht-torward. You use two eew functions you just leamed (sizeof and llocto instantiate a dynamic variable, a memben uing the arrow operator, and voilal you have a stnuctured vaniable instance with assign values to its variable itstances you w have created so that you can keep them together in a sensible way, and be able to access each of theve blocks individualiy latce for reading for writing or for delkting One way to do that of course. is to put chem in an array However, that defeuts the whole purpose ef Synamicaly allocating variables at tun time, wihout having to inew how large an areay you will need. So, that is not a practical solution The better way to do it a to link the trusture. There are several variatiom of inked struatures teg ete, but the one we will treat here is the knked &s instances together wing pointers and form a nked linked stackx, linked queUe To visualize a linked ist,ehink of elephants in a cireus where one elephant grabs the tail of the one in front of it with its trunk, and its own tail o in turn grabbed by the elephant o on. Thee elephants are Iinked together, tal to trunk to talinto one powby long chain Same for a linked ist. One block (instance veriablel points to the one behind it, which int points to the one after it, and so on. There is one pointer poiming to the first one caled the heod pointer, and the tai of the lest block in the linked lst points to NULL to indicate the end ofthe kt,Graphica y, 리ooklke this: behind it and

.

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

#include<stdio.h>

struct node

{

int data;

struct node *next;

};

struct node *insert_end(struct node *);

struct node *delete(struct node *);

void display(struct node *);

int main()

{

struct node *start=NULL;

struct node *tmp;

int i;

for(i=0;i<5;i++)

{

tmp=(struct node *)malloc(sizeof(struct node));

tmp->data=4-i;

tmp->next=start;

start=tmp;

}

printf("current list:");

display(start);

start=insert_end(start);

printf("\nnode after insertion at end:");

display(start);

start=delete(start);

}

struct node *insert_end(struct node *start) //inserts node at end of the lis

{

int data;

printf("insert data(node) to be inserted at end");

scanf("%d",&data);

struct node *tmp;

tmp=(struct node *)malloc(sizeof(struct node));

tmp->data=data;

tmp->next=NULL;

struct node *p=start;

if(p==NULL)

{

p->next=tmp;

return p;

}

while(p->next!=NULL)

p=p->next;

p->next=tmp;

return start;

}

struct node *delete(struct node *start) //deletes the desired node

{

int x;

printf("insert the node to be deleted");

scanf("%d",&x);

struct node *tmp=start, *prev;

if(start->data==x)

{

start=start->next;

free(tmp);

return start;

}

while(tmp!=NULL && tmp->data!=x)

{

prev=tmp;

tmp=tmp->next;

}

if(tmp==NULL)

{

printf("node to be deleted is not present in the list\n");

return start;

}

prev->next=tmp->next;

free(tmp);

printf("\nnode after deletion:");

display(start);

return start;

}

void display(struct node *start)

{

struct node *p;

if(start==NULL)

{

printf("Empty\n");

return;

}

p=start;

while(p!=NULL)

{

printf("%d->",p->data);

p=p->next;

}

printf("\n\n");

}

Add a comment
Know the answer?
Add Answer to:
. The topic of this laboratory exercise a probably one of the most ieceresting ones you...
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 this exercise, you will make a struct that contains x and y coordinates: struct point...

    In this exercise, you will make a struct that contains x and y coordinates: struct point {int x; int y;}; The object of this exercise is to make a singly linked list of these point structures. Add one more member called "next" to your point struct. It is a pointer to the next member of the linked list. The next member of the last point struct should point to NULL, which indicates the end of the list. Your program will...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Your assignment is to create and test a class for a queue of objects. You may...

    Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue.   The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field). You should complete the software to implement and test your queue and submit the software along with a project report. Your queue class...

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

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • This lab, for which you may work in groups of two, will require you to use...

    This lab, for which you may work in groups of two, will require you to use a C structure to hold a record of any kind of data, i.e., address book, library of books as with chapter 14, etc. These records will be held in dynamic memory using memory allocation (malloc) to create new memory and the function (free) to release memory created via (malloc). Do not use linked lists for this assignment, we are using dynamic memory allocation to...

  • In this project you will create a console C++ program that will have the user to...

    In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE...

    In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE YOU USE C PROGRAMMING Your MMS will handle all requests of allocation of memory space by different users (one thread per user) …. HINT(You will use Pthreads and Semaphores). Your MMS will provide the user with an interface for making memory requests and also for freeing up memory that is no longer needed by the user. One of the jobs of your memory management...

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