Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Node{
- int val;
- Node * next;
- };
- Node * insertAtTail(int data,Node* head){
- Node * a = new Node();
- a->val = data;
- a->next = NULL;
- if(head==NULL){
- head = a;
- }
- else{
- Node * temp = head;
- while(temp->next!=NULL){
- temp = temp->next;
- }
- temp->next = a;
- }
- return head;
- }
- void printList(Node * head){
- while(head!=NULL){
- cout<<head->val<<" ";
- head= head->next;
- }
- return;
- }
- Node * delNode(Node * head, int d){
- Node * temp = head;
- if(head->val == d){
- head = head->next;
- return head;
- }
- while( temp->next!=NULL && temp->next->val!=d){
- temp = temp->next;
- }
- if(temp->next!=NULL){
- temp->next = temp->next->next;
- }
- return head;
- }
- int main(){
- int n;
- cout<<"How many values you want in list : "<<endl;
- cin>>n;
- Node *head = NULL;
- //5->6->9
- for(int i = 0; i<n ; i++){
- int data;
- cout<<"Enter an integer : "<<endl;
- cin>>data;
- head = insertAtTail(data,head);
- }
- printList(head);
- //5->6->3->8->11
- int d;
- cout<<"Value you want to delete from list : "<<endl;
- cin>>d;
- head = delNode(head,d);
- printList(head);
- //5->6->8->11 (if d is 3)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement