#include<stdio.h>
#define MaxSize 100
typedef char ElemType
typedef struct //定义一个串
{
ElemType data[MaxSize]
int length
}SqString
void StrAssign(SqString &str,char cstr[]) //////////////燃铅///////////
{
int i
for(i=0cstr[i]!='\0'i++)
str.data[i]=cstr[i]
str.length=i
}
int StrLength(SqString s) //串的长度
{
return s.length
}
void DispStr(SqString s) //输出数据
{
int i
if(s.length>0)
{
for(i=0i<s.lengthi++)
{
printf("%c ",s.data[i])
}
}
}
SqString InsStr(SqString s1,int i,SqString s2) //插入子串
{
int j
SqString str
str.length=0
if(i<=0 || i>s1.length+1)
return str
for(j=0j<i-1j++)
str.data[j]=s1.data[j]
for(j=0j<s2.lengthj++)
str.data[i+j-1]=s2.data[j]
for(j=i-1j<s1.lengthj++)
str.data[s2.length+j]=s1.data[j]
str.length=s1.length+s2.length
return str
}
SqString DelStr(SqString s,int i,int j) //删除字符子串
{
int k
SqString str
str.length=0
if(i<=0 || i>s.length || i+j>s.length+1)
return str
for(k=0k<i-1k++)
str.data[k]=s.data[k]
for(k=i+j-1k<s.lengthk++)
str.data[k-j]=s.data[k]
str.length=s.length-j
return str
}
SqString RepStr(SqString s,int i,int j,SqString t) //替换
{
int k
SqString str
str.length=0
if(i<=0 || i>s.length || i+j>s.length+1)
return str
for(k=0k<i-1k++)
str.data[k]=s.data[k]
for(k=0k<t.lengthk++)
str.data[i+k-1]=t.data[k]
for(k=i+j-1k<s.lengthk++)
str.data[t.length+k-j]=s.data[k]
str.length=s.length+t.length-j
return str
}
SqString SubStr(SqString s,int i,int j) //子串
{
SqString str
int k
str.length=0
if(i<=0 || i>s.length || j<0 || i+j-1 >s.length)
return str
for(k=i-1k<i+j-1k++)
str.data[k-i+1]=s.data[k]
str.length=j
return str
}
SqString concat(SqString s,SqString t) /码伏/串连接
{
SqString str
int i
str.length=s.length+t.length
for(i=0i<s.lengthi++)
str.data[i]=s.data[i]
for(i=0i<迟段携t.lengthi++)
str.data[s.length+i]=t.data[i]
return str
}
int main()
{
SqString s,s1,s2,s3,s4
printf("(1) 建立串“abcdefghijklmn”和串“xyz”\n")
StrAssign(s,"abcdefghijklmn")
StrAssign(s1,"xyz")
printf("已建立\n\n")
printf("(2) 输出串s\n")
DispStr(s)
printf("\n\n")
printf("(3) 输出串s的长度\n")
printf("s的长度为 %d\n\n",StrLength(s))
printf("(4) 在串s的第9个字符位置插入串s1而产生串s2\n")
s2=InsStr(s,9,s1)
printf("已插入\n\n")
printf("(5) 输出串s2\n")
DispStr(s2)
printf("\n\n")
printf("(6) 删除串s第2个字符开始的5个字符而产生串s2\n")
s2=DelStr(s,2,5)
printf("已删除\n\n")
printf("(7) 输出串s2\n")
DispStr(s2)
printf("\n\n")
printf("(8) 将串s的第2个字符开始的5个字符替换成串s1而产生串s2\n")
s2=RepStr(s,2,5,s1)
printf("已替换\n\n")
printf("(9) 输出串s2\n")
DispStr(s2)
printf("\n\n")
printf("(10) 提取串s的第2个字符开始的10个字符而产生串s3\n")
s3=SubStr(s,2,10)
printf("已产生新串\n\n")
printf("(11) 输出串s3\n")
DispStr(s3)
printf("\n\n")
printf("(12) 将串s1和串s2连接起来而产生串s4\n")
s4=concat(s1,s2)
printf("已连接\n\n")
printf("(13) 输出串s4\n")
DispStr(s4)
return 1
}
#include <stdio.h>#include <string.h>
main()
{
char s[100]
int k,b
int c
c='A'-'a'
gets(s)
for(int i=1s[i]!='\0'i++)
if(s[i]>='老慎A' &&s[i]<='Z') s[i]-=c
if(s[1]>胡察=s[0])
for(int i=1s[i]!='\0'i++)
if(s[i]<侍做敬s[i-1]) k=1
else
for(int i=1s[i]!='\0'i++)
if(s[i]>s[i-1]) b=1
if(b==1 &&k==1) printf("NO")
else printf("YES")
}
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct Node
{
char data
struct Node * next
}node
void Insert(node* )//插入
void Find(node* )//查找
int Count(node*)//链表长度
void Update(node* )//修改
void Delete(node* )//删除
void Show(node* )//输出
int main()
{
int a
node 肢哪head
head.next = NULL
printf("***********链表的 *** 作************\n\n")
while(1)
{
a = 0
printf("***********请选择您的 *** 作***********\n\n")
printf("1 链表的插入\t 2 链表的查找\t 3 链表的修改\n4 链表的删除\t 5 链表的输出\t 6 退出系统\n")
scanf("%d",&a)
switch(a)
{
case 1:
Insert(&head)
break
case 2:
Find(&head)
break
case 3:
Update(&head)
break
case 4:
Delete(&head)
break
case 5:
Show(&head)
break
case 6:
exit(-1)
break
default :
printf("输入错误!")
break
}
}
return 0
}
int Count(node* head)
{
node* pH = head
int count = 0
while (pH->next != NULL )
{
pH = pH->next
count++
}
return count
}
void Insert(node* head )
{
int which = 0
int i = 0
int j = 1
char ch
node * pH = head
printf("\n1.首插入 2.未插入 3.插入到位置i\n")
printf("请选择:")
scanf("%d",&which)
ch = getchar()
if (which == 1)
{
printf("请输入值:"历逗码)
scanf("%c",&ch)
node * 指运q = (node *)malloc(sizeof(Node))
q->data = ch
q->next = pH->next
pH->next = q
}
else if (2 == which)
{
while (pH->next != NULL)
{
pH = pH->next
}
printf("请输入值:")
scanf("%c",&ch)
node * q = (node *)malloc(sizeof(Node))
q->data = ch
q->next = pH->next
pH->next = q
}
else if ( 3 == which )
{
printf("请输入i的值:")
scanf("%d",&i)
ch = getchar()
if ( (i > 0) && (i <= Count(head) + 1) )
{
printf("i = %d",i)
while (j < i)
{
pH = pH->next
j++
}
printf("请输入值:")
scanf("%c",&ch)
node * q = (node *)malloc(sizeof(Node))
q->data = ch
q->next = pH->next
pH->next = q
}
else
{
printf("i输入错误!\n")
}
}
else
{
printf("选择错误!\n")
}
return
}
void Show(node* pH)
{
printf("链表输出:\n")
if ( pH->next == NULL)
{
printf("链表为空!\n")
return
}
else
{
while ( pH->next != NULL )
{
pH = pH->next
printf("%3c",pH->data)
}
printf("\n输出结束!\n")
}
}
void Find(node* head)
{
int which = 0
int j = 0
int i = 0
char ch
bool is_have = false
node * q = head->next
if ( Count(head) == 0 )
{
printf("链表为空!无法查找.\n")
return
}
printf(" 1.查找内容的位置 2.查找位置的内容\n")
scanf("%d",&which)
ch = getchar()
if (1 == which)
{
printf("请输入要查找的内容:")
scanf("%c",&ch)
while ( q != NULL)
{
j++
if ( q->data == ch)
{
printf("%c是第%d个。\n",ch,j)
is_have = true
}
q = q->next
}
if ( is_have == false )
{
printf("所查找的内容在链表中不存在!")
}
}
else if ( 2 == which )
{
j = 0
printf("请输入要查找的位置:")
scanf("%d",&i)
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法查找。\n")
return
}
while ( q != NULL && j < i-1 )
{
q = q->next
j++
}
printf("内容为:%c",q->data)
}
else
{
printf("选择错误!\n")
}
return
}
void Update(node* head)
{
node * q = head->next
int i = 0
int j = 0
char ch
if ( Count(head) == 0 )
{
printf("链表为空!无法查找.\n")
return
}
printf("请输入要修改的位置:")
scanf("%d",&i)
ch = getchar()
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法修改。\n")
return
}
printf("请输入修该的值:")
scanf("%c",&ch)
while ( q != NULL && j < i-1 )
{
q = q->next
j++
}
q->data = ch
printf("修改成功!\n")
return
}
void Delete(node* head)
{
node * q = head->next
node * p = head
int i = 0
int j = 0
char ch
if ( Count(head) == 0 )
{
printf("链表为空!无法删除.\n")
return
}
printf(" 1.全部删除 2.删除单个\n")
scanf("%d",&i)
ch = getchar()
if ( 1 == i)
{
while( q != NULL )
{
p = p->next
q = q->next
free(p)
}
head->next = NULL
printf("释放成功!\n")
}
else if ( 2 == i )
{
printf("请输入要删除的位置:")
scanf("%d",&i)
ch = getchar()
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法删除。\n")
return
}
while ( q != NULL && j < i-1 )
{
p = p->next
q = q->next
j++
}
p->next = q->next
free(q)
printf("删除成功!\n")
}
else
{
printf("选择错误!\n")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)