- inserting
- deleting
insert
كود PHP:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char name[50];
struct node *link;
};
struct node *insert(struct node *p , char *name)
{
struct node *tmp;
if( p == NULL)
{
p=malloc(sizeof(struct node ));
strncpy(p->name , name , sizeof(p->name));
p->link=NULL;
}
else
{
tmp=p;
while(tmp->link != NULL)
tmp=tmp->link;
tmp->link=malloc(sizeof(struct node));
tmp=tmp->link;
strncpy(tmp->name,name,sizeof(tmp->name));
tmp->link=NULL;
}
return p;
}
void print(struct node *p)
{
while(p!=NULL)
{
printf("Name=%s\n",p->name);
p=p->link;
}
}
int len(struct node *p)
{
int i=0;
while(p!=NULL)
{
++i;
p=p->link;
}
return i;
}
struct node *insert_next_to(struct node *p , int node_no , char *name)
{
struct node *t,*t1;
int i=1;
if(p==NULL)
{
fprintf(stderr,"Not Such A list\n");
/*edit:here we should End this functions because this function doesn't like to have a NULL pointer*/
exit(1);
}
if(node_no == 0)
{
t=malloc(sizeof(struct node ));
strncpy(t->name , name , sizeof(t->name));
t->link=p; /* make it point to the first structure in the list*/
p=t; /* make the head point to it*/
}
else
{
if(node_no > len(p))
{
fprintf(stderr,"Error\n");
}
else
{
t=p;
while(i < node_no)
{
i++;
t=t->link;;
}
t1=malloc(sizeof(struct node ));
strncpy(t1->name , name , sizeof(t1->name));
t1->link = t->link;
t->link = t1;
}
}
return p; /* return the list*/
}
int main ( int argc , char **argv )
{
int ch,i=1;
struct node *start=NULL;
if(argc < 2)
{
printf("Usage::%s Strings",argv[0]);
exit(1);
}
while( i < argc)
{
start=insert(start , argv[i]);
i++;
}
printf("Before\n");
print(start);
start=insert_next_to(start,0,"----------Storm---------");
printf("After\n");
print (start);
return 0;
}
كود PHP:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char name[50];
struct node *link;
};
struct node *insert(struct node *p , char *name)
{
struct node *tmp;
if( p == NULL)
{
p=malloc(sizeof(struct node ));
strncpy(p->name , name , sizeof(p->name));
p->link=NULL;
}
else
{
tmp=p;
while(tmp->link != NULL)
tmp=tmp->link;
tmp->link=malloc(sizeof(struct node));
tmp=tmp->link;
strncpy(tmp->name,name,sizeof(tmp->name));
tmp->link=NULL;
}
return p;
}
void print(struct node *p)
{
while(p!=NULL)
{
printf("Name=%s\n",p->name);
p=p->link;
}
}
int len(struct node *p)
{
int i=0;
while(p!=NULL)
{
++i;
p=p->link;
}
return i;
}
struct node *del(struct node *p , int node_no)
{
struct node *prev,*current;
int i=1;
if(p==NULL)
{
printf("No List To Traverse\n");
}
else
{
if(node_no > len(p))
{
printf("Between 1 and %d\n",len(p));
}
else
{
prev=NULL;
current=p;
while(i < node_no)
{
prev=current;
current=current->link;
i++;
}
if(prev==NULL) /* in case of delation at head*/
{
p=p->link;
free(current);
}
else
{
prev->link = current->link;
free(current);
}
}
}
return p;
}
int main ( int argc , char **argv )
{
int ch,i=1;
struct node *start=NULL;
if(argc < 2)
{
printf("Usage::%s Strings",argv[0]);
exit(1);
}
while( i < argc)
{
start=insert(start , argv[i]);
i++;
}
print(start);
printf("\nEnter The Element you want to delete it\n");
scanf("%d",&ch);
start=del(start,ch);
print (start);
return 0;
وبالاخص striker

ااجزء الاخير كان حكايه



تعليق