Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.c
- #include<stdio.h>
- struct listnode {
- int data;
- struct listnode *next;
- };
- typedef struct listnode LN;
- void insert_at_back(LN **hptr, int d);
- void print(LN *head);
- int sum(LN *head);
- int main(){
- LN *head;
- int d;
- head = NULL;
- printf("Enter: ");
- do{
- scanf("%d", &d);
- if(d > 0){
- insert_at_front(&head,d);
- }
- }while(d > 0);
- printf("");
- print(head);
- printf("\nSUM=%d", sum(head));
- return 0;
- }
- void insert_at_front(LN **hptr, int d){
- LN* temp = (LN*)malloc(sizeof(LN));
- temp->data = d;
- temp->next= *hptr;
- *hptr = temp;
- }
- void print(LN *head){
- if(head !=NULL){
- if(head->next==NULL){
- printf("in if\n");
- printf("=%d\n",head->data);
- }
- else{
- printf("fisrt in else \n");
- print(head->next);
- printf("Second in else\n");
- printf("=%d\n",head->data);
- }
- }
- }
- int sum(LN *head){
- int temp=0;
- while(head !=NULL){
- temp+=head->data;
- head = head->next;
- }
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement