Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node {
- int val;
- struct node *next;
- }*List;
- List init_node(){
- return NULL;
- }
- int est_vide(List l){
- return l==NULL;
- }
- List cree_node(int val){
- List new = (List) malloc(sizeof(List));
- if ( new == NULL ){
- printf("\n ak ghalet");
- return;
- }
- new->next = NUll;
- new->val = val;
- return new;
- }
- void affiche_list(List l){
- if ( l== NULL ){
- printf(" la list est vide , rien d'afficher" );
- return;
- }
- List l2=l;
- while(l2!= NULL){
- printf("%d",l2->val);
- l2=l2->next;
- }
- }
- List suprime_fin(List l){
- List courant,precedent;
- if(l==NULL)
- return NULL;
- if(l->next==NULL){
- free(l);
- return NULL;
- }
- courant= l->next;
- precedent= l;
- while(courant->next !=NULL){
- precedent=precedent->next;
- courant=courant->next;
- }
- precedent->next=NULL;
- free(courant);
- return l;
- }
- List inserer_element_fin ( List l, int e) {
- List nouveau ;
- List s ;
- nouveau = creer_node (e) ;
- if (est_vide(l))
- return nouveau ;
- s= liste;
- while (s -> suivant != NULL)
- s = s -> suivant;
- s -> suivant = nouveau;
- return liste;
- }
- List insire(List l, int n){
- int i,v;
- List l1=l;
- l=init_node();
- for(i=0;i<n;i++){
- printf("\n give me a number: ");
- scanf("%d",&v);
- return inserer_element_fin (l,v);
- }
- List invers(List l){
- List p=p2=l,s;
- s=init_node();
- while(p2->next!=NULL){
- if(est_vide(p)) return NULL;
- while(p->next!=NULL)
- p=p->next;
- if(s == NULL) //////////////here
- s=cree_node(p->val);
- else
- s->next=cree_node(p->val);
- p=suprime_fin(l);
- p2=p2->next;
- }
- return s;
- }
- int main(){
- List l;
- int n=4;
- l=insire(l,n);
- l=invers(l);
- affiche_list(l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement