-->





Translate

Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

doubly linked list queue in c++ source code

Leave a Comment
Doubly or circular linked list in c++ programming example using the queue data structure
This c++ source code of the queue has four main functions.
  1. enqueue
  2. dequeue
  3. show front
  4. show list
It uses the concept of functions, pointer, struct and switch, etc..

#include<iostream>
#include<windows.h>

using namespace std;

//Queue for Double Linklist
struct dlist {
	dlist * prev;
	int data;
	dlist * next;
};
dlist *first, *current, *previos, *tamp;
int dlinklist_counter = 0;
void dlinklist_insert();
void dlinklist_call();
void dlinklist_dequeu();
void dlinklist_show();
void dlinklist_front();

int main()
{
	system("cls");

    dlinklist_call();
   return 0;
}


//function of DOUBLE LINK LIST
void dlinklist_call()
{
dlinklist_start:
	system("cls");
	cout << "\t\t\t\t Welcome in Double linklist Queue";
	int dinput;
	cout << "\n 1- Enqueu \n 2- Dequeu \n 3- show list \n 4- Front\n 5- Exit\n";
	cin >> dinput;
	switch (dinput)
	{
	case 1:
		dlinklist_insert();
		cout << " Number entered \n";
		system("pause");
		goto dlinklist_start;
	case 2:
		dlinklist_dequeu();
		cout << "Number deleted \n ";
		system("pause");
		goto dlinklist_start;
	case 3:
		dlinklist_show();
		goto dlinklist_start;
	case 4:
		dlinklist_front();
		goto dlinklist_start;
	case 5:
		break;
	default:
		cout << " You enter invalid number ";
		system("pause");
		goto dlinklist_start;
	}
}
void dlinklist_insert()
{
	current = new dlist;
	if (dlinklist_counter == 0)
	{
		previos = current;
		first = current;
		current->prev = NULL;
		cout << " Enter Data ";
		cin >> current->data;
	}
	else
	{
		previos->next = current;
		current->prev = previos;
		previos = current;
		cout << " Enter Data ";
		cin >> current->data;
	}
	current->next = NULL;
	dlinklist_counter++;
}
void dlinklist_dequeu()
{
	if (dlinklist_counter == 0)
	{
		cout << " Queue is empty";
		system("pause");
	}
	else
	{
		first = first->next;
		dlinklist_counter--;
	}
}
void dlinklist_show()
{
	if (dlinklist_counter == 0)
	{
		cout << " Queue is empty";
	}
	else
	{
		tamp = first;
		while (tamp->next != NULL)
		{
			cout << " " << tamp->data;
			tamp = tamp->next;
		}
		cout << " " << tamp->data;
	}
	system("pause");
}
void dlinklist_front()
{
	if (dlinklist_counter == 0)
	{
		cout << " Queue is empty";
	}
	else
	{
		cout << " " << first->data;
	}
	system("pause");
}



Program output:
doubly linked list queue in c++
Read More...

queue data structure implementation c++ code using linked list

Leave a Comment
C++ program to implement queue data structure using linked list

It uses struct, pointers, switch and functions concept. Every queue operation has its own function. User selects its desired operation from the menu.
It has the following functions

  1. Enqueue
  2. Dequeue
  3. Show front 
  4. Show All


    #include<windows.h>
    #include<iostream>
    using namespace std;

    // Queue for Linklist
    struct list
    {
     int data;
     list * next;
    };
    list *f, *c, *p, *temp;
    int linklist_counter = 0;
    void linklist_insert();
    void linklist_call();
    void linklist_dequeu();
    void linklist_show();
    void linklist_front();
    int linklist_isempty();

    int main()
    {
     linklist_call();
     return 0;
    }
    //function of LINK LIST
    void linklist_call()
    {
    linklist_start:
     system("cls");
     cout << "\t\t\t\t Welcome in linklist Queue";
     int input;
     cout << "\n 1- Enqueue \n 2- Dequeue \n 3- show list \n 4- Front\n 5- Exit\n";
     cin >> input;
     switch (input)
     {
     case 1:
      linklist_insert();
      cout << " Number entered \n";
      system("pause");
      goto linklist_start;
     case 2:
      linklist_dequeu();
      goto linklist_start;
     case 3:
      linklist_show();
      goto linklist_start;
     case 4:
      linklist_front();
      goto linklist_start;
     case 5:
      break;
     default:
      cout << " You enter invalid number ";
      system("pause");
      goto linklist_start;
     }
    }
    void linklist_insert()
    {
     c = new list;
     if (linklist_counter == 0)
     {
      f = c;
      p = c;
      cout << " Enter data ";
      cin >> c->data;
     }
     else
     {
      p->next = c;
      p = c;
      cout << " Enter data";
      cin >> c->data;
     }
     c->next = NULL;
     linklist_counter++;
    }
    void linklist_dequeu()
    {
     if (linklist_counter == 0)
     {
      cout << " Queue is empty";
      system("pause");
     }
     else
     {
      f = f->next;
      linklist_counter--;
      cout << "Number deleted \n ";
      system("pause");
     }
    }
    void linklist_show()
    {
     int emp = linklist_isempty();
     if (emp)
     {
      temp = f;
      while (temp->next != NULL)
      {
       cout << " " << temp->data;
       temp = temp->next;
      }
      cout << " " << temp->data;
     }
     else
     {
      cout << " Queue is empty";
     }
     system("pause");
    }
    void linklist_front()
    {
     int emp = linklist_isempty();
     if (emp)
     {
      cout << " " << f->data;
     }
     else
     {
      cout << " Queue is empty";
     }
     system("pause");
    }
    int linklist_isempty()
    {
     return(linklist_counter != 0);
    }


Read Also: Array based queue c++ code example
program output:



c++ program for implementing queue using linked list.
linked list queue c++


As a beginner, this code helps the student to clear the concept of the queue using a linked list. It can be used as started code for beginners. find a full  c++ array based queue project.

also read other projects here: c++ simple projects
Read More...

Linked list in C++ project example student record

11 comments
Project introduction and scope
It is a simple c++ project for Student Information System. It gives a simple example of a complete system with the functionality of add record, search record, Update record and delete record using linked list. If students understand basic CRUD (Create, Read, Update and Delete) functionality it would be easy for them to create any Management System using this functionality.

It uses the concept of following topics:
  1. Pointers
  2. Loops
  3. Functions
  4. Linked List
  5. If Else

The Project has 5 major function in menu
  1. Add New record
  2. Search a record
  3. Modify a record
  4. Delete a record
  5. Exit

How it Works
The menu is handled by if else statement. While adding a new record, if any node already exist then new node is connected with it otherwise a new node is created. While searching a node, a pointer searches all the pointers of linked lists to match the required data from start to end. While Modification the data part of the linked list is overwritten with new data and in case of Deletion, the pointers before and after of the node which we need to delete are connected with each other.

#include<conio.h>
#include<iostream>
#include<fstream>
#include<Windows.h>
#include<dos.h>
#include<cctype>
#include<sstream>
#include<string>
using namespace std;
bool check = true;
struct node    //structure of node //
{
 char name[20];
 char discipline[20];
 int rollNo;
 char section;
 node *next;
}*head,*lastptr;

void add()    //Adds record of student//
{
 node *p;
 p=new node;
 cout<<"Enter name of student:"<<endl;
 gets(p->name);
 fflush(stdin);
 cout<<"Enter discipline of student:"<<endl;
 gets(p->discipline);
 fflush(stdin);
 cout<<"Enter Roll Number of student:"<<endl;
 cin>>p->rollNo;
 fflush(stdin);
 cout<<"Enter section of student:"<<endl;
 cin>>p->section;
 fflush(stdin);
 p->next=NULL;

 if(check)
 {
  head = p;
  lastptr = p;
  check = false;
 }
 else
 {
  lastptr->next=p;
  lastptr=p;
 }
 cout<<endl<<"Recored Entered";
 getch();
}
void modify()   //modifies record of student//
{
 node *ptr;
 node *prev=NULL;
 node *current=NULL;
 int roll_no;
 cout<<"Enter Roll Number to search:"<<endl;
 cin>>roll_no;
 prev=head;
 current=head;
 while(current->rollNo!=roll_no)
 {
  prev=current;
  current=current->next;
 }
 ptr=new node;
 fflush(stdin);
 cout<<"Enter name of student:"<<endl;
 gets(ptr->name);
 fflush(stdin);
 cout<<"Enter discipline of student:"<<endl;
 gets(ptr->discipline);
 fflush(stdin);
 cout<<"Enter Roll Number of student:"<<endl;
 cin>>ptr->rollNo;
 fflush(stdin);
 cout<<"Enter section of student:"<<endl;
 cin>>ptr->section;
 fflush(stdin);
 prev->next=ptr;
 ptr->next=current->next;
 current->next=NULL;
 delete current;
 cout<<endl<<"Recored Modified";
 getch();
}
void search()   //searches record of student//
{
 node *prev=NULL;
 node *current=NULL;
 int roll_no;
 cout<<"Enter Roll Number to search:"<<endl;
 cin>>roll_no;
 prev=head;
 current=head;
 while(current->rollNo!=roll_no)
 {
  prev=current;
  current=current->next;
 }
 cout<<"\nname: ";
 puts(current->name);
 cout<<"\nRoll No:";
 cout<<current->rollNo;
 cout<<"\nDiscipline:";
 puts(current->discipline);
 cout<<"\nSection:";
 cout<<current->section;
 getch();
}
void del()    //deletes record of a student//
{
 node *ptr=NULL;
 node *prev=NULL;
 node *current=NULL;
 int roll_no;
 cout<<"Enter Roll Number to Delete:"<<endl;
 cin>>roll_no;
 prev=head;
 current=head;
 while(current->rollNo!=roll_no)
 {
  prev=current;
  current=current->next;
 }
 prev->next = current->next;
 current->next=NULL;
 delete current;
 cout<<endl<<"Recored Deleted";
 getch();
}

int main()
{
 char x;
 cout<<"\t\t ********************************* \t\t\t"<<endl;
 cout<<"\t\t ****STUDENT MANAGEMENT SYSTEM**** \t\t\t"<<endl;
 cout<<"\t\t ********************************* \t\t\t"<<endl;
 cout<<"…………………………………….."<<endl;
 cout<<"…………………"<<endl;
 cout<<"……………………………………………………."<<endl;
 cout<<"\n\nPress Any Key To Continue . . . ."<<endl;
 getch();
 do
 {
  system("cls");
  cout<<"1--->Press '1' to add New record:"<<endl;
  cout<<"2--->Press '2' to search a record:"<<endl;
  cout<<"3--->Press '3' to modify a record:"<<endl;
  cout<<"4--->Press '4' to delete a record:"<<endl;
  cout<<"5--->Press '5' to exit:"<<endl;
  x=getch();
  if(x=='1')
  {
   system("cls");
   add();
  }
  else if(x=='2')
  {
   system("cls");
   search();
  }
  else if(x=='3')
  {
   system("cls");
   modify();
  }
  else if(x=='4')
  {
   system("cls");
   del();
  }
  else if(x=='5')
  {
   exit(0);
  }
  else
  {
  }
 }while(x != 27);
 getch();
}

Sample output
Linked list in C++ project example student record
show project menu



Linked list in C++ project example student information




Code has been tested on code Blocks C++ compiler. 
find more c++ simple projects




Read More...