LINKLIST IN C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//LINKLIST INSERTION AT THE BEGINNING | |
#include<iostream> | |
#include<conio.h> | |
using namespace std; | |
struct node | |
{ | |
int data; | |
struct node *link;//pointing to next node structure | |
}; | |
struct node* head;//HEAD | |
void inser(int x) | |
{ | |
node* temp=new node;//creating a new node | |
temp->data=x; | |
temp->link=head;//storing NULL if there is not item previously | |
head=temp;//head stores the address of temp; | |
} | |
void display() | |
{ | |
node* temp2=head;//stores the address of head; | |
//cout<<"head"<<head<<"\n"; | |
//cout<<temp2; | |
while(temp2!=NULL) | |
{ | |
cout<<temp2->data<<" "; | |
temp2=temp2->link; | |
} | |
cout<<"\n"; | |
} | |
void delet()//deleting a node from beginning | |
{ | |
node* nd=head;//storing the address of head | |
nd=nd->link;//storing the address of 2nd node | |
head=nd;//head=address of 2nd node | |
} | |
int main() | |
{ | |
int n,i=0,a[10]; | |
cout<<"\nEnter the number of element:\t"; | |
cin>>n; | |
for(i=0;i<n;i++) | |
{ | |
cout<<"\nEnter the element:\t"; | |
cin>>a[i]; | |
inser(a[i]); | |
} | |
cout<<"\nDisplaying element:\n"; | |
display(); | |
cout<<"\nDelete n-1 element:\n"; | |
for(i=0;i<n-1;i++) | |
{ | |
delet(); | |
} | |
cout<<"\nDisplaying elements:\n"; | |
display(); | |
getch(); | |
return 0; | |
} |
Comments
Post a Comment
Thanks for the comment and don't forget to subscribe.