Code:
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
typedef char elementtype;
typedef struct node{
elementtype element;
node*next;
};
typedef node * list;
typedef list position;
node * make_node(elementtype x){
node *temp=(node*)malloc(sizeof(node));
temp->element=x;
return temp;
}
list makenull_list(){
list temp=(list)malloc (sizeof(list));
temp->next=NULL;
return temp;
}
char empty_list(list s){
return s->next==NULL;
}
void insert_list(elementtype x,position p,list*l){
position t;
t = (node*)malloc(sizeof(node));
t->element = x;
t->next = p->next;
p->next = t;
}
void delete_list(position p,list*l){
position t;
if(p->next !=NULL){
t->next = p->next;
p->next = p->next->next;
free(t);
}
else printf("\nloi! danh sach rong khong the xoa ");
}
void insert_endlist(list s, elementtype x){
node *p=make_node(x);
p->next=s->next;
s->next=p;
}
void pop(list s){
if(!empty_list(s))
{
printf("\n loi! list rong");
node *temp=s->next;
s->next=temp->next;
free(temp);
}
}
void out_list(list s){
list temp=s->next;
while(temp!=NULL){
printf("%d\t", temp->element);
temp = temp->next;
}
}
list input_list(unsigned int n){
list temp = makenull_list();
unsigned int i=0;
elementtype x;
while(i<n)
{
printf("element %d=",i);
scanf("%d", &x);
insert_endlist(temp, x);
i ++;
}
return temp;
}
elementtype sum(list s){
elementtype temp=0;
list p=s->next;
while(p!=NULL)
{
temp+=p->element;
p=p->next;
}
return temp;
}
int test(list s, elementtype x){
list temp = s->next;
while(temp!=NULL)
{
if(temp->element ==x){
return 1;
}
temp = temp->next;
}
return 0;
}
void main(){
clrscr();
unsigned int n;
elementtype x;
printf("n=");
scanf("%d",&n);
list s = input_list(n);
printf("this list:\n");
out_list(s);
printf("\n\n danh sach sau khi khoi tao rong la ");
if(empty_list(l)) printf ("\n\n danh sach rong ");
else printf("\n\n danh sach khong rong ");
for(int i=1; i<=5; i++)
insert_list (i+10,endlist(l),&l);
printf("\n\n danh sach sau khi dua cac so tu 11-15 vao theo thu tu la \n\n ");
print_list(l);
printf("\sum of all values in list: %d", sum(s));
printf("\n input value to find:");
scanf("%d", &x);
if(test (s,x))
printf("this value has in list ");
else
printf("this value don't have int list");
getch();
}