Untitled
unknown
plain_text
2 years ago
1.3 kB
18
Indexable
#include<iostream> using namespace std; typedef struct node{ int value; struct node* next; }node_t; node* createnode(int val){ node* newnode=(node*)malloc(sizeof(node)); newnode->value=val; newnode->next=NULL; return newnode; } node* insert_athead(node **head, node* nodename){ //(**pointer to a pointer)this directly changes head , address of head is passed nodename->next=(*head); (*head)=nodename; return nodename; } void reversell(node **head){ node *prev=(node*)malloc(sizeof(node)); node *forward=(node*)malloc(sizeof(node)); node *curr=(node*)malloc(sizeof(node)); prev=NULL; curr=*head; while(curr->next!=NULL){ forward=curr->next; curr->next=prev; prev=curr; curr=forward; } *head=curr; } void prinll(node *hed){ node *tmp=(node*)malloc(sizeof(node)); tmp=hed; while(tmp!=NULL){ cout<<tmp->value<<"-"; tmp=tmp->next; } cout<<"\n"; } int main(){ node_t *n1; node* head=NULL; for(int i=3;i<=5;i++){ n1=createnode(i); insert_athead(&head,n1); } prinll(head); reversell(&head); prinll(head); }
Editor is loading...