ورود

View Full Version : ورودی رشته در لیست پیوندی به زبان c++



mohammad.shakournia
سه شنبه 05 تیر 1397, 18:57 عصر
سلام دوستان خسته نباشید.من با سی پلاس پلاس برنامه لیست پیوندی دو طرفه رو دارم کار میکنم. اما زمانی که ورودی رو به عنوان CHAR تعریف میکنم کار نمیکنه و لطفا راهنمایی کنید.ممنون
#include<iostream>

#include<cstdio>

#include<cstdlib>

/*

* Node Declaration

*/

using namespace std;

struct node

{

int info;

struct node *next;

struct node *prev;

}*start;



/*

Class Declaration

*/

class double_llist

{

public:

void create_list(char value);
void delete_element(char value);
void display_dlist();
void reverse();

double_llist()

{

start = NULL;

}

};
/*

* Main: Conatins Menu

*/

int main()

{

int choice;
char element=' ';

double_llist dl;

while (1)

{

cout<<endl<<"----------------------------"<<endl;

cout<<endl<<"Operations on Doubly linked list"<<endl;

cout<<endl<<"----------------------------"<<endl;

cout<<"1.Create Node"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Reverse"<<endl;
cout<<"5.Quit"<<endl;
cout<<"Enter your choice : ";

cin>>choice;

switch ( choice )

{

case 1:

cout<<"Enter the element: ";

cin>>element;

dl.create_list(element);

cout<<endl;

break;


case 2:

if (start == NULL)

{

cout<<"List empty,nothing to delete"<<endl;

break;

}

cout<<"Enter the element for deletion: ";

cin>>element;

dl.delete_element(element);

cout<<element<<endl;


break;

case 3:

dl.display_dlist();

cout<<element<<endl;

break;


case 4:

if (start == NULL)

{

cout<<"List empty,nothing to reverse"<<endl;

break;

}

dl.reverse();

cout<<endl;

break;

case 5:

exit(1);

default:

cout<<"Wrong choice"<<endl;

}

}

return 0;

}



/*

* Create Double Link List

*/

void double_llist::create_list(char value)

{

struct node *s, *temp;

temp = new(struct node);

temp->info = value;

temp->next = NULL;

if (start == NULL)

{

temp->prev = NULL;

start = temp;

}

else

{

s = start;

while (s->next != NULL)

s = s->next;

s->next = temp;

temp->prev = s;

}

}


/*

* Deletion of element from the list

*/

void double_llist::delete_element(char value)

{

struct node *tmp, *q;

/*first element deletion*/

if (start->info == value)

{

tmp = start;

start = start->next;

start->prev = NULL;

cout<<"Element Deleted"<<endl;

free(tmp);

return;

}

q = start;

while (q->next->next != NULL)

{

/*Element deleted in between*/

if (q->next->info == value)

{

tmp = q->next;

q->next = tmp->next;

tmp->next->prev = q;

cout<<"Element Deleted"<<endl;

free(tmp);

return;

}

q = q->next;

}

/*last element deleted*/

if (q->next->info == value)

{

tmp = q->next;

free(tmp);

q->next = NULL;

cout<<"Element Deleted"<<endl;

return;

}

cout<<"Element "<<value<<" not found"<<endl;

}



/*

* Display elements of Doubly Link List

*/

void double_llist::display_dlist()

{

struct node *q;

if (start == NULL)

{

cout<<"List empty,nothing to display"<<endl;

return;

}

q = start;

cout<<"The Doubly Link List is :"<<endl;

while (q != NULL)

{

cout<<q->info<<" <-> ";

q = q->next;

}

cout<<"NULL"<<endl;

}


/*

* Reverse Doubly Link List

*/

void double_llist::reverse()

{

struct node *p1, *p2;

p1 = start;

p2 = p1->next;

p1->next = NULL;

p1->prev = p2;

while (p2 != NULL)

{

p2->prev = p2->next;

p2->next = p1;

p1 = p2;

p2 = p2->prev;

}

start = p1;

cout<<"List Reversed"<<endl;

}