設(shè)線性鏈表的存儲結(jié)構(gòu)如下:
struct node
{ELEMTP data; /*數(shù)據(jù)域*/
struct node *next; /*指針域*/
}
試完成下列建立單鏈表的算法。
creat()
{char var;
head=(struct node *)malloc(sizeof(struct node));
head->next= () ;
while((var=getchar())!=‘\n’){
ptr=( struct node *)malloc(sizeof(struct node));
ptr->data= var ;ptr->next=head->next;
head->next= ptr ;
}
}
設(shè)線性鏈表的存儲結(jié)構(gòu)如下:
struct node
{ELEMTP data; /*數(shù)據(jù)域*/
struct node *next; /*指針域*/
}
試完成下列在鏈表中值為x的結(jié)點前插入一個值為y的新結(jié)點。如果x值不存在,則把新結(jié)點插在表尾的算法。
void inserty(struct node *head,ELEMTP x,ELEMTP y)
{s=(struct node *)malloc(sizeof(struct node));
();
if(){s->nexr=head;head=s;}
else {
q=head;p=q->next;
while(p->dqta!=x&&p->next!=NULL){q=p;()}
if(p->data= = x){q->next=s;s->next=p;}
else{p->next=s;s->next=NULL;}
}
}
設(shè)順序存儲的線性表存儲結(jié)構(gòu)定義為:
struct sequnce
{ELEMTP elem[MAXSIZE];
int len; /*線性表長度域*/
}
將下列簡單插入算法補(bǔ)充完整。
void insert(struct sequnce *p,int i,ELEMTP x)
{v=*p;
if(i<1)||(i>v.len+1)printf(“Overflow“);
else {
for(j=v.len;();j- -)();
v.elem[i]= () ;v.len=();
}
}