forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fineanmol#1320 from mani9793/master
Linked list sum program of last N nodes added
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct node{ | ||
int data; | ||
struct node *next;// | ||
}*head; | ||
|
||
void insertq(int a[],int m){ | ||
struct node *temp,*newnode; | ||
head=(struct node*)malloc(sizeof(struct node));// | ||
|
||
head->data=a[0]; | ||
head->next=NULL; | ||
|
||
// temp=(struct node*)malloc(sizeof(struct node));; | ||
temp=head; | ||
//head->next=temp; | ||
|
||
for(int i=1;i<m;i++){ | ||
newnode = (struct node*)malloc(sizeof(struct node));// | ||
newnode->data=a[i]; | ||
newnode->next=NULL; | ||
|
||
temp->next=newnode; | ||
// newnode=newnode->next; | ||
temp=newnode; | ||
} | ||
|
||
|
||
} | ||
void printq(struct node *head){ | ||
|
||
struct node*temp; | ||
temp=head; | ||
while(temp!=NULL){ | ||
cout<<temp->data<<"\n"; | ||
temp=temp->next; | ||
} | ||
} | ||
|
||
|
||
int sumOfLastN_Nodes(struct node* head, int n) | ||
{ | ||
// Code here | ||
if(n<=0) | ||
return 0; | ||
struct node*temp = head; | ||
int m=0,sum=0; | ||
while(temp!=NULL){ | ||
m++; | ||
temp=temp->next; | ||
} | ||
cout<<"m...."<<m<<endl; | ||
temp = head; | ||
for(int pos=1;pos<m-n+1;pos++){ | ||
temp=temp->next; | ||
cout<<"head\n"<<"....."<<temp->data<<endl; | ||
} | ||
|
||
while(temp!=NULL){ | ||
sum+=temp->data; | ||
cout<<"sum here,,,"<<sum<<endl; | ||
temp=temp->next; | ||
} | ||
cout<<"sum\n"<<"....."<<sum<<endl; | ||
return sum; | ||
} | ||
|
||
|
||
int main(){ | ||
|
||
int m; | ||
cout<<"enter size\n"; | ||
cin>>m; | ||
int a[m]; | ||
cout<<"enter elements\n"; | ||
for(int i=0;i<m;i++){ | ||
cin>>a[i]; | ||
} | ||
cout<<"enter nth\n"; | ||
int n; | ||
cin>>n; | ||
cout<<"calling insert....\n"; | ||
insertq(a,m); | ||
cout<<"calling print....\n"; | ||
printq(head); | ||
int res=sumOfLastN_Nodes(head,n); | ||
cout<<"print sum....\n"<<res; | ||
return 0; | ||
} | ||
|