0% found this document useful (0 votes)
37 views9 pages

nom: Badache

This program implements a linked list data structure and provides functions to add, delete, and display nodes in the list. The main function allows the user to select an operation like adding a node to the head, tail, or a specific position. Based on the selection, it calls the appropriate function and displays the updated list. It uses a menu loop to allow multiple operations on the list in the same program run.

Uploaded by

ram ezi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

nom: Badache

This program implements a linked list data structure and provides functions to add, delete, and display nodes in the list. The main function allows the user to select an operation like adding a node to the head, tail, or a specific position. Based on the selection, it calls the appropriate function and displays the updated list. It uses a menu loop to allow multiple operations on the list in the same program run.

Uploaded by

ram ezi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

//NOm : BADACHE

//Prénom : Ramzi

//GROUPE : G6

#include<iostream>

 using namespace std;

 struct cell

 {

 int data;  

 cell *next;

 };

 cell* head_creation(int x)

 {

 cell *head=new cell;

 head->next=NULL;

 head->data=x; 

 return head;

 }
 void DISPLAY(cell* head)

 {cell *browser=head;

 cout<<" HEAD";

 while(browser!=NULL) 

 cout<<"-->"<<browser->data;browser=browser->next;

cout<<"-->NULL\n";

cell *add_cells_head(int x,cell *head)

{ cell *new_cell;

new_cell=new cell;

new_cell->data=x;

new_cell->next=head;

head=new_cell;

return head;

 cell* add_cells_end(int x,cell *head)

 {

 cell *new_cell; 

 cell *browser=head;

 while (browser->next!=NULL)
 { browser=browser->next; } 

 new_cell=new cell; 

 new_cell->data=x; 

 new_cell->next=NULL; 

 browser->next=new_cell; 

 return head; 

 }

  cell *delete_cells_head(cell *head)

 {cell *browser=head;

 if(head!=NULL)  

 {

 head=browser->next;

 delete browser; 

 }

 return head;

 }

 cell* delete_cells_end(cell *head)

 {

 cell *current=head,*previous=head; 

 if(head!=NULL)
 {

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

 {

 previous=current;

 current=current->next;

 }

 previous->next=NULL; 

 delete current;

 }

 return head;

 }

 cell* add_cells_position(int x,int position,cell *head)

 {

 cell *new_cell;

 cell *current=head,*previous=head;

 cout<<"ajouter au debut de  cell en position "<<position<<endl;

 for (int i=1;i<position;i++)

 { if((current)!=NULL)

 { previous=current;

 current=current->next;

 }

 }

 new_cell=new cell;

 new_cell->data=x; 
 new_cell->next=current; 

 previous->next=new_cell;

 return head; 

cell* delete_cells_position(int position,cell *head)

 {

 cell *current=head,*previous=head;

 cout<<" impossible "<<position<<endl;

 if(head!=NULL) 

 { for (int i=1;i<position;i++)

 {

 if(current!=NULL)

 {

 previous=current;

 current=current->next;

 }

 }

 previous->next=current->next; 

 if(head==current) 

 head=current->next;

 delete current;

 }

 return head; 
}

 int SIZE(cell* head)

 { cell *browser=head;int i=0;

 if (head!=NULL) 

 { while(browser!=NULL)

 { i++; 

 browser=browser->next;

 }

 }

 return i;

 }

int in()

 {

 int x;

 cout<<"entrer datas de listes\n";

 cin>>x;

 return x;
 }

 int main()

 {

 cell *head=NULL;

 int a,x,position;

 cout<<"cr�er la liste ?: (0 = NO /sinon = YES)\n";

 int answer;

 cin>>answer;

 if(answer!=0)

 { head=head_creation(in()); 

 menu:

 cout<<"votre choix :\n1) ajouter au debut \n2) ajouter a la fin\n3) ajouter au


possition";

cout<<"\n4)supprimer au debut\n5) supprimer a la fin\n6) suprimer en position";

 cin>>a;

 switch(a)
 {case 1:

 cout<<" ajouter au debut de la liste \n";

 head=add_cells_head(in(),head);

 DISPLAY(head);break;

 case 2:

 cout<<"ajouter aa la fin de la liste:\n";

 head=add_cells_end(in(),head);

 DISPLAY(head);break;

 case 3:

 cout<<"ajouter au position :\n";

 cout<<"choier la position:\n";

 cin>>position;

 if(position<=SIZE(head)+1 && position>0)

 head=add_cells_position(in(),position,head);

 else

 cout<<"impossible :\n";

 DISPLAY(head);break;

 case 4:

 cout<<"suprimer au debut de la liste\n";

 head=delete_cells_head(head);

 DISPLAY(head);break;

 case 5:
 cout<<" suprimer a la fin de la iste :\n";

 head=delete_cells_end(head);

DISPLAY(head);break;

 case 6:

 cout<<"suprimer en position : \n";

 cout<<"choisir votre position:\n";

 cin>>position;

 if(position<=SIZE(head) && position>0)

 head=delete_cells_position(position,head);

 else

 cout<<"position n'�xiste pas :\n";

 DISPLAY(head);break;

 case 7:

  DISPLAY(head);break;

 }

 if (a<8 && a>0) goto menu;

 }

 return 0;

 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy