write a program to find the sum of the array elements in c
language?
Answer Posted / sasa zezo
please help me i can't understand this program?
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create(int n)
{
struct node *head;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("no memory space");
exit(0);
}
else
{
head->data=n;
head->next=NULL;
}
return head;
}
void print(struct node * p)
{
printf("\n the new linked list is \n");
while(p!=NULL)
{
printf(" %d \t ",p->data);
p = p->next ;
}
printf("\n");
}
struct node * deletelast(struct node * p)
{
struct node *prev,*curr;
curr=p;
prev=NULL;
while(curr->next!=NULL)
{
prev=curr;
curr=curr->next;
}
prev->next=NULL;
free(curr);
return p;
}
struct node * deletefrst(struct node * p)
{
struct node *curr;
curr=p;
p=p->next;
free(curr);
return p;
}
struct node * deletenode(struct node * p , int x)
{
struct node *prev,*curr;
curr=p;
prev=NULL;
while((curr->data!=x)&&(curr->next!=NULL))
{ prev=curr;
curr=curr->next;}
if((curr->next==NULL)&&(curr->data!=x))
{ printf("the element x is not found");
prev->next=NULL;
free(curr);}
else {
prev->next=curr->next;
free(curr);}
return p;
}
int count(struct node * p)
{
struct node *curr;
int i=0;
curr=p;
while(curr!=NULL)
{
curr=curr->next;
i++;
}
return i;
}
struct node * insertfrst(struct node * p , int n)
{
struct node * temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("no memory space");
exit(0);
}
else
{
temp->data=n;
temp->next=p;
p=temp;
}
return temp;
}
struct node *insertlast(struct node * p,int n)
{
struct node *temp,*curr;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("no memory space");
exit(0);
}
curr=p;
while(curr->next!=NULL)
{
curr=curr->next;
}
temp->data=n;
temp->next=NULL;
curr->next=temp;
return p;
}
struct node * insertbefor(struct node * p,int n,int x)
{
struct node *temp,*prev,*curr;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("no memory space");
exit(0);}
curr=p;
prev=NULL;
while((curr->data!=x)&&(curr->next!=NULL))
{ prev=curr;
curr=curr->next;}
if((curr->next==NULL)&&(curr->data!=x))
{ printf("the element x is not found");
temp->data=n;
temp->next=NULL;
curr->next=temp;}
else
{temp->data=n;
temp->next=curr;
prev->next=temp;}
return p;}
void main()
{
struct node * p;
int i;
p=create(20);
print(p);
for( i=1;i<=4;i++)
p=insertfrst(p,i);
print(p);
p=insertlast(p,34);
print(p);
p=insertbefor(p,77,34);
print(p); p=delete¬node(p,34);
print(p);
p=deletelast(p);
print(p);
p=deletefrst(p);
print(p);}
Is This Answer Correct ? | 0 Yes | 2 No |
Post New Answer View All Answers
How does selection sort work in c?
illustrate the use of address operator and dereferencing operator with the help of a program guys plzzz help for this question
#define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); }
Is fortran faster than c?
What is wrong with this program statement? void = 10;
a c code by using memory allocation for add ,multiply of sprase matrixes
explain how do you use macro?
Is it possible to execute code even after the program exits the main() function?
why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???
Why is structure important for a child?
what is a constant pointer in C
Describe newline escape sequence with a sample program?
what is the role you expect in software industry?
How can I open a file so that other programs can update it at the same time?
Is struct oop?