t nextNULL delete q void listdelatanylocint loc struct node qt qp forint i1

T nextnull delete q void listdelatanylocint loc

This preview shows page 46 - 54 out of 145 pages.

t->next=NULL; delete q; } void list::delatanyloc(int loc) { struct node *q,*t; q=p; for(int i=1; i<loc; i++) { t=q; q=q->next; } t->next=q->next;
Image of page 46
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 47 q->next->prev=q->prev; delete q; } void list::display() { struct node *q; q=p; while(q!=NULL) { cout<<q->data<<"-->"; q=q->next; } cout<<"NULL"; } void main() { list l; cout<<"first node"<<endl; l.append(10); l.display(); cout<<endl; cout<<"complete list"<<endl; l.append(20); l.append(30); l.append(40); l.append(50); l.display(); l.addatbeg(60); cout<<endl<<"complete list"<<endl; l.display(); l.addatanyloc(70,3); cout<<endl<<"complete list"<<endl; l.display(); l.addatend(80); cout<<endl<<"complete list"<<endl; l.display(); l.delatbeg(); cout<<endl<<"ater deleting first node complete list"<<endl; l.display(); l.delatend(); cout<<endl<<"after deleting last node complete list"<<endl;
Image of page 47
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 48 l.display(); l.delatanyloc(3); cout<<endl<<"after deleting 3rd node complete list"<<endl; l.display(); getch(); } Program to implement stack using linked list #include<conio.h> #include<iostream.h> class stack { struct node { int data; struct node *next; }*tos; public: stack(); void push(int); void pop(); void display(); }; stack::stack() { tos=NULL; } void stack::push(int item) { struct node *p; p = new node; p->data = item; p->next = NULL; if(tos!=NULL) { p->next = tos; } tos = p; } void stack::pop()
Image of page 48
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 49 { struct node *q; if(tos==NULL) { cout<<"\nThe stack is Empty"<<endl; } else { q= tos; tos = tos->next; cout<<"\nThe value popped is "<<q->data<<endl; delete q; } } void stack::display() { struct node *p; p= tos; if(tos==NULL) { cout<<"\nNothing to Display\n"; } else { cout<<"\nThe contents of Stack\n"; while(p!=NULL) { cout<<p->data<<endl; p = p->next; } } } void main() { stack s; s.push(10); s.display(); cout<<endl; s.push(20); s.push(30); s.push(40); s.push(50);
Image of page 49
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 50 s.display(); s.pop(); s.display(); s.pop(); s.display(); getch(); } Program to implement queue using linked list #include<conio.h> #include<iostream.h> class queue { struct node { int data; struct node *next; }*front,*rear; public: queue(); void enqueue(int); void dequeue(); void display(); }; queue::queue() { front=rear=NULL; } void queue::enqueue(int item) { struct node *p; p = new node;
Image of page 50
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 51 p->data = item; p->next = NULL; if(front==NULL) { front=p; } if(rear!=NULL) { rear->next = p; } rear = p; } void queue::dequeue() { struct node *q; if(front==NULL) { cout<<"\nThe queue is Empty"<<endl; } else { q= front; front = front->next; cout<<"\nThe value popped is "<<q->data<<endl; delete q; } } void queue::display() { struct node *p; p= front; if(front==NULL)
Image of page 51
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 52 { cout<<"\nNothing to Display\n"; } else { cout<<"\nThe contents of Queue\n"; while(p!=NULL) { cout<<p->data<<" "; p = p->next; } } } void main() { queue q; q.enqueue(10); q.display(); cout<<endl; q.enqueue(20); q.enqueue(30); q.enqueue(40); q.enqueue(50); q.display(); q.dequeue(); q.display(); q.dequeue(); q.display(); q.dequeue(); q.display(); getch(); }
Image of page 52
Lecture notes on Data Structures And Algorithms, By Dilendra Bhatt, Assistant professor, NCIT 53 Chapter 5
Image of page 53
Image of page 54

You've reached the end of your free preview.

Want to read all 145 pages?

  • Fall '19
  • Array data structure

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes