代做C数据结构作业、代做Clistnode 单链表、多锭表作业


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cot=0,count=0;
typedef struct listnode
{
char ch;
char str[11];
struct listnode * next_deep;
struct listnode *next;
int weight;
int valid;
}listnode;
listnode a[500];
char *SubString(char *str, int StartPostion, int SubstringLength)
{
int stringlen = 0;
int i = 0;
int x = 0;
char *tmp;
stringlen = strlen(str);
tmp = (char *)malloc(sizeof(char)*(SubstringLength + 1));
if ((StartPostion < 0) || (SubstringLength <= 0) || (stringlen == 0) || (StartPostion >= stringlen))
{

strcpy(tmp, "\0");
return tmp;
}
for (i = StartPostion; ((i < stringlen) && (x < SubstringLength)); i++)
{
tmp[x] = str[i];
x++;
}
tmp[x] = '\0';
return tmp;
}
listnode* create1(char ch)
{
listnode* p=&a[cot++];
p->ch=ch;
p->next_deep=p->next=NULL;
p->weight=-1;
p->valid=0;
return p;
}
listnode* create2(char *ch,int w)
{
listnode* p=&a[cot++];
int len=strlen(ch);
for(int i=0;i<len;i++)
p->str[i]=ch[i];
p->str[len]='\0';
p->next_deep=p->next=NULL;
p->weight=w;
p->valid=1;
return p;
}
void insert_deep(listnode* p,char* ch,int w)
{
listnode* q=p->next_deep;
if(!q)
{
p->next_deep=create2(ch,w);
return ;
}
while(p)
{
if(strcmp(p->str,ch)==0)
{
count--;
return ;
}
q=p;
p=p->next_deep;
}
q->next_deep=create2(ch,w);
return;
}
void insert(listnode* p,char c,char* ch,int w)
{
//printf("c=%c ch=%s w=%d\n",c,ch,w);
p=p->next;
while(p)
{
if(p->ch==c)
{
insert_deep(p,ch,w);
//printf("插入了 %s 后面是 %c\n",p->next_deep->str,p->next->ch);
return ;
}
p=p->next;
}
return ;
}
void delet(listnode* p,char ch,char *str)
{
listnode *q;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(strcmp(q->str,str)==0)
{
q->valid=0;
break;
}
q=q->next_deep;
}
return;
}
p=p->next;
}
return ;
}
void query(listnode* p,char ch,char *str,FILE *fp2)
{
listnode *q;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(q->valid==0)
{
q=q->next_deep;
continue;
}
fprintf(fp2,"%d ",q->weight);
printf("%d ",q->weight);
if(strcmp(q->str,str)==0)
{
//fprintf(fp2,"\n");
return ;
}
q=q->next_deep;
}
}
p=p->next;
}
//fprintf(fp2,"\n");
return ;
}
void swap(listnode* p,char ch,char *str,char* s)
{
int len,i;
listnode *q,*k=p;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(q->valid==0)
{
q=q->next_deep;
continue;
}
if(strcmp(q->str,str)==0)
{
q->valid=0;
insert(k,s[0],s,q->weight);
return ;
}
q=q->next_deep;
}
}
p=p->next;
}
return ;
}
void show(listnode* p)
{
listnode* q;
while(p)
{
q=p->next_deep;
printf("%c ",p->ch);
while(q)
{
printf("%s ",q->str);
q=q->next_deep;
}
p=p->next;
printf("\n");
}
}
int main()
{
FILE *fp1;
fp1=fopen("sample_input.txt","r");
FILE *fp2;
fp2=fopen("sample_output.txt","w+");
int len,i,j,k;
listnode *head=(listnode*)malloc(sizeof(listnode));
head->next=head->next_deep=NULL;
listnode *p,*q;
for(i='Z';i>='A';i--)
{
if(!(head->next))
head->next=create1(i);
else
{
p=head->next;
q=create1(i);
head->next=q;
q->next=p;
}
}
for(i='z';i>='a';i--)
{
p=head->next;
q=create1(i);
head->next=q;
q->next=p;
}
//show(head->next);
char str[1000];
char *ptr,*pt,*tp;
fgets(str,1000,fp1);
str[strlen(str)-1]='\0';
len=strlen(str);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(str[j]==' ')
break;
}
//printf("damn it %s\n",SubString(str,i,j-i));
insert(head,str[i],SubString(str,i,j-i),count++);
//show(head->next);
i=j;
}
//show(head->next);
while(fgets(str,1000,fp1))
{
if(strcmp(str,"\n")==0)
continue;
if(strlen(str)==1&&str[0]=='0')
break;
str[strlen(str)-1]='\0';
ptr=strtok(str," ");
pt=strtok(NULL," ");
if(str)
tp=strtok(NULL," ");
if(strcmp(ptr,"del")==0)
delet(head->next,pt[0],pt);
if(strcmp(ptr,"sch")==0)
{
query(head->next,pt[0],pt,fp2);
fprintf(fp2,"\n");
printf("\n");
}
if(strcmp(ptr,"sub")==0)
swap(head->next,pt[0],pt,tp);
}
return 0;
}http://www.6daixie.com/contents/13/1435.html

因为专业,所以值得信赖。如有需要,请加QQ99515681 或邮箱:[email protected] 

微信:codinghelp

猜你喜欢

转载自www.cnblogs.com/java11111/p/9097421.html
今日推荐