Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Get rid of the c + + stuff in his code so it is just in C ( the code is down below ) .

Get rid of the c++ stuff in his code so it is just in C (the code is down below). Use the linked list code (put it into your IDE) and then do a very simple demo of you using it to build a linked list ( like he did ) Make sure to show your source code and output for that and some comments to explain what your code is doing !!
#include
#include
#include "List.h"
using namespace std;
List::List(){
head = NULL;
curr = NULL;
temp = NULL;
}
void List::AddNode(int addData){
nodePtr n = new node;
n->next = NULL;
n->data = addData;
if(head != NULL){
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr ->next = n;
}
else
{
head = n;
}
}
void List::DeleteNode(int delData){
nodePtr delPtr = NULL;
temp = head;
curr = head;
while(curr != NULL && curr->data != delData){
temp = curr;
curr = curr->next;
}
if(curr == NULL){
cout << delData << "was not in the list
";
delete delPtr;
}
else{
delPtr = curr;
curr = curr->next;
temp->next= curr;
if(delPtr == head){
head = head->next;
temp = NULL;
}
delete delPtr;
cout << "The value" << delData << "was deleted
";
}
}
void List::PrintList(){
curr = head;
while(curr != NULL)
{
cout << curr ->data << endl;
curr = curr->next;
}
}
#include
#include "List.h"
using namespace std;
int main(int argc, char** argv{
List Rachael;
Rachael.AddNode(3);
Rachael.AddNode(5);
Rachael.AddNode(7);
Rachael.PrintList();
Rachael.DeleteNode(5);
Rachael.PrintList();
return 0;
})
#ifndef LIST_H
#define LIST_H
class List {
private:
typedef struct node {
int data;
node* next;
}*nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public: //This is where the functions go
List();
void AddNode(int addData);
void DeleteNode(int delData);
void PrintList();
};
#endif
Linked list demo
#include
#include
#include
typedef struct node {
char *data;
struct node *next;
}* nodePtr;
void printlist(nodePtr l){
nodePtr curr = l;
printf("THE LIST IS:
");
while (curr != NULL){
printf("%s
", curr->data);
curr = curr->next;
}
printf("
");
}
nodePtr addnode(nodePtr head, char *data){
// create a new node
nodePtr n = malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
// first guy in the list
if (head == NULL){
head = n;
return head;
}
// hope along to the last guy
nodePtr curr = head;
while (curr->next != NULL){
curr = curr->next;
}
// glue him on
curr->next = n;
return head;
}
int main()
{
nodePtr head = NULL;
// lets add a few things to the list
head = addnode(head, "Once upon");
printlist(head);
head = addnode(head, "a time");
printlist(head);
head = addnode(head,"in Hollywood");
printlist(head);
// lets construct a string out of some stuff and add it to the list
double x =25.567;
char s[80];
char xstr[80];
strcpy(s,"My lucky number ");
sprintf(xstr,"%.2lf", x);
strcat(s, xstr);
head = addnode(head, s);
printlist(head);
return 0;
}
Another example of Linked List Demo
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef struct node {
char val;
struct node * next;
} node_ptr;
void add(node_ptr * head, char val){
node_ptr * current = head;
while (current->next != NULL){
current = current->next;
}
/* now we can add a new variable */
current->next =(node_ptr *) malloc(sizeof(node_ptr));
current->next->val = val;
current->next->next = NULL;
}
void print_list(node_ptr * head){
node_ptr * current = head;
while (current != NULL){
printf("%c
", current->val);
current = current->next;
}
}
int main()
{
node_ptr * head = NULL;
head =(node_ptr *) malloc(sizeof(node_ptr));
if (head == NULL){
return 1;
}
head->val ='A';
head->next = NULL;
add(head,'B');
add(head,'C');
add(head,'D');
print_list(head);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Icdt 88 2nd International Conference On Database Theory Bruges Belgium August 31 September 2 1988 Proceedings Lncs 326

Authors: Marc Gyssens ,Jan Paredaens ,Dirk Van Gucht

1st Edition

3540501711, 978-3540501718

More Books

Students also viewed these Databases questions

Question

2. What are the components of IT infrastructure?

Answered: 1 week ago