//Insert in the beginningstructNode{intdata;Node*next;};structNode*head;//global variablevoidInsert(intx){structNode*temp=(Node*)malloc(sizeof(structNode));//or---------Node*temp=newNode();//C++//-------------temp->data=x;temp->next=head;head=temp;}voidPrint(){structNode*temp=head;printf("List is:");while(temp!=NULL){printf("%d ",temp->data);temp=temp->next;}printf("\n");}intmain(){head=NULL;//empty listprintf("How many numbers?");intn,i,x;scanf("%d",&n);for(i=0;i<n;i++){printf("Enter number:");scanf("%d",&x);Insert(x);Print();}}
structNode{intdata;Node*next;};structNode*head;//pointer to the headvoidInsert(intdata,intn);voidPrint();intmain(){head=NULL;//empty listInsert(2,1);//List:2Insert(3,2);//List:2,3Insert(4,1);//list:4,2,3Insert(5,2);//List:4,5,2,3Print();}//-------------------------------------------------------------------------------------------voidInsert(intdata,intn){Node*temp1=newNode();temp1->data=data;temp1->next=NULL;if(n==1){//in case to insert in the beginningtemp1->next=head;head=temp1;return;}Node*temp2=head;//temp2 is an aid for finding the positionfor(inti=0;i<n-2;i++){temp2=temp2->next;}//go to the n-1 nodetemp1->next=temp2->next;temp2->next=temp1;}//-------------------------------------------------------------------------------------------voidPrint(){Node*temp=head;while(temp!=NULL){printf("%d ",temp->data);temp=temp->next;}printf("\n");}
structNode{intdata;Node*next;};structNode*head;//globalvoidInsert(intdata);//insert at the end of the listvoidPrint();//print all the elements in the listvoidDelete(intn);//delete node at position nintmain(){head=NULL;Insert(2);Insert(4);Insert(6);Insert(5);//List:5,6,4,2intn;printf("Enter a position:\n");scanf("%d",&n);Delete(n);Print();}//-------------------------------------------------------------------------------------------voidDelete(intn){Node*temp1=head;inti;if(n==1){head=temp1->next;free(temp1);return;}for(i=0;i<n-2;i++){temp1=temp1->next;}//temp1 points to the (n-1)th nodestructNode*temp2=temp1->next;temp1->next=temp2->next;free(temp2);//delete temp2}//-------------------------------------------------------------------------------------------voidInsert(intx){structNode*temp=newNode();temp->data=x;temp->next=head;head=temp;}voidPrint(){Node*temp=head;while(temp!=NULL){printf("%d ",temp->data);temp=temp->next;}printf("\n");}
voidPrint(structNode*p){//recursion//2 6 5 4if(p==NULL)return;//Exit Recursion, prevent dead loopprintf("%d ",p->data);//First print the value int the nodePrint(p->next);//Recursive call}
voidReversePrint(structNode*q){//recursion//4 5 6 2if(q==NULL)return;//Exit RecursionReversePrint(q->next);//First do a Recursive callprintf("%d ",q->data);//print the value int the node}
structNode{intdata;Node*next;};structNode*Insert(Node*head,intdata){Node*temp=newNode;temp->data=data;temp->next=NULL;if(head==NULL){head=temp;}else{Node*current=head;while(current->next!=NULL){current=current->next;}current->next=temp;}returnhead;}voidPrint(structNode*p){//recursion//2 6 5 4if(p==NULL)return;//Exit Recursionprintf("%d ",p->data);//First print the value int the nodePrint(p->next);//Recursive call}voidReversePrint(structNode*q){//recursion//4 5 6 2if(q==NULL)return;//Exit RecursionReversePrint(q->next);//First do a Recursive callprintf("%d ",q->data);//print the value int the node}intmain(){structNode*head=NULL;//local variable,empty list head=Insert(head,2);head=Insert(head,4);head=Insert(head,6);head=Insert(head,5);Print(head);printf("\n");ReversePrint(head);}
The problem with using the “&” operator: The stack frame of GNN will be reclaimed and even if you have the address of e.g. 50, you won't be able to get it.->It doesn't create anything in the heap.
The only way to access something in heap is through a pointer.
voidInsertAhead(intx){structNode*newNode=GetNewNode(x);//the newNode here is a local variable different from the one in the function GetNewNode,just share the same nameif(head==NULL){//when list is emptyhead=newNode;return;}head->prev=newNode;newNode->next=head;head=newNode;}
voidPrint(){Node*temp=head;printf("Forward:");while(temp!=NULL){printf("%d ",temp->data);temp=temp->next;}printf("\n");}voidReversePrint(){Node*temp=head;if(temp==NULL)return;//empty list,exit//Going to last nodewhile(temp->next!=NULL){temp=temp->next;}//traversing backward using prev pointerprintf("Reverse :");while(temp!=NULL){printf("%d ",temp->data);temp=temp->prev;}printf("\n");}
//using the functions upon…… intmain(){head=NULL;InsertAhead(2);Print();ReversePrint();InsertAhead(3);Print();ReversePrint();InsertAhead(4);Print();ReversePrint();InsertAhead(5);Print();ReversePrint();}