In: Computer Science
This is c++
Create a program where you create, display, search, delete elements within a linked list.
Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others.
Functions to make:
copy : copies element in linked list
destroy all: destroys all elements in linked list
wherethisgoes:user enters element and returns where the element is located
insert sorted: inserts element
create using linked lists with a head pointer and so forth
LINKED LIST:
IT IS A LINEAR DATA STRUCTURE WHERE EACH ELEMENT IS CONSIDER AS A SINGLE OBJECT.IT IS NOT STORED IN CONTAGIOUS LOCATION.HERE THE ELELMENTS ARE LINKED USING POINTERS.EACH NODE OF LIST IS MADE WITH TWO ITEMS:-THE DATA AND THE REFERENCE TO THE NEXT NODE.THE LAST NODE HAS A REFERENCE TO NULL.
C++ PROGRAM:
#include<iostream.h>
struct Node
{
int d;
struct Node *next;
};
struct Node *head=NULL;
void insert sorted(int new_d)
{
struct Node *new_node=(struct Node*) malloc(sizeof(struct Node));
new node->d=new_d;
new node->next=head;
head=new_node;
}
void display()
{
struct Node *ptr;
ptr=head;
while(ptr!=NULL)
{cout<<ptr->d<<" ";
ptr=ptr->next;
}
}
void deletelist()
{
struct Node*current=*head;
Node*next;
while(current!=NULL)
{next=current->next;
free(current);
current=next;
}
*head=NULL;
}
void copy()
{
struct Node*arbit=*head;
Node *next;
copy_node->arbit=copy_node->arbit->next;
copy_node=copy_node->next;
}
insert sorted main()
{insert sorted(1);
insert sorted(2);
insert sorted(8);
insert sorted(3);
insert sorted(10);
cout<<"THE LINKED LIST IS :";
display();
cout<<"deleting linked list:";
deletelist();
cout<<"linked list deleted;
copy();
cout<<"copied ;ist is";
}
}