Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Node{
- public:
- int val;
- Node * next;
- };
- Node * insert(Node * head , int x){
- Node * a = new Node();
- a->val = x;
- if(head==NULL){
- head = a;
- }
- else{
- a->next = head;
- head = a;
- }
- return head;
- }
- void printl(Node * head){
- Node * temp = head;
- while(temp!=NULL){
- cout<<temp->val<<" ";
- temp = temp->next;
- }
- cout<<endl;
- }
- Node * del (Node * head , int d){
- Node * temp = head;
- if(head->val==d){
- Node* t = head;
- head = head->next;
- free(t);
- return head;
- }
- while(temp->next!=NULL && temp->next->val != d){
- temp = temp->next;
- }
- if(temp->next==NULL)return head;
- Node* y = temp->next;
- temp->next = temp->next->next;
- free(y);
- return head;
- }
- int main(){
- int n;
- Node * head= NULL;
- cout<<"Enter the size of your linkedlist : \n";
- cin>>n;
- for(int i=0; i<n; i++){
- cout<<"Enter a number : \n";
- int x; //3->2->1
- cin>>x;
- head = insert(head,x);
- }
- printl(head);
- cout<<"Enter a number you want to delete : \n";
- int d;
- cin>>d;
- head = del(head,d);
- printl(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement