链表:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
structchain
{
intvalue;
structchain *next;
};
structchain *create()
{
structchain *head, *tail, *p;
intx,i;
head = tail = NULL;
printf("请输入四个整型数据,然后回车:/n");
for(i= 0;i < 4; i++)
{
scanf("%d",&x);
p = (structchain *)malloc (sizeof (structchain));
p->value = x;
p->next = NULL;
if(head == NULL)
head = tail = p;
else
tail = tail ->next = p;
}
returnhead;
}
structchain *inlink(head,a,b)
structchain *head;
inta, b;
{
structchain *p, *q, *s;
s = (structchain *)malloc(sizeof(structchain));
s->value = b;
/*空表插入*/
if(head == NULL)
{
head = s;
s->next = NULL;
}
/*插入s结点作为新表头*/
if(head->value == a)
{
s->next = head;
head = s;
}
else
{
p = head;
/*遍历单链表,寻找数据域值为a的结点*/
while ((p->value != a)&&(p->next != NULL))
{
q = p;
p = p->next;
}
if(p->value == a) //找到数据域为a的结点
{
q->next = s;
s->next = p;
}
/*插入结点s作为表尾*/
else
{
p->next = s;
s->next = NULL;
}
}
return(head);
}
structchain *dellink(head,a)
structchain *head;
inta;
{
structchain *p,*q;
if(head == NULL)
printf("空链表/n");
elseif(head ->value == a)
/*链表的第一个结点即为a结点*/
{
p = head;
head = head->next;
}
else
{
p = head;
while ((p->value != a)&&(p->next != NULL))
/*在链表中搜索数据为a的结点*/
{
q = p;
p = p->next;
}
if(p->value != a)
/*在链表中无数据值为a的结点*/
printf("没有要删除的数据 %d/n",a);
else
{
q ->next = p->next;
free(p);
}
}
return(head);
}
voidmain()
{
structchain *q,*head;
inta, b;
q = create();
head = q;
while(q) //显示链表
{
printf("%d/n",q->value);
q = q->next;
}
printf("请输入新插入的表元数据位于那个数据之前:");
scanf("%d",&a);
printf("/n 请输入要插入的表元数据: ");
scanf("%d",&b);
q = inlink(head,a,b);
head = q;
while(q) //显示链表
{
printf("%d/n",q->value);
q = q->next;
}
printf("请输入要删除表元的数据: ");
scanf("%d",&a);
q = dellink(head,a);
while(q) //显示链表
{
printf("%d/n",q->value);
q = q->next;
}
}
队列:
栈:
栈:
#include <stdio.h>
#include <stdlib.h>
#define Max 100
int *p;
int *tos;
int *bos;
/*添加一个数据放到堆栈对顶端*/
voidpush(inti)
{
if(p > bos)
{
printf("堆栈以满/n");
return;
}
*p = i;
p++;
}
/*丛堆栈顶端取出一个数据*/
intpop(void)
{
p--;
if(p < tos)
{
printf("堆栈下溢/n");
return 0;
}
return *p;
}
voidmain(void)
{
inta,b;
chars[80];
p = (int *)malloc(Max*sizeof(int));
if(!p)
{
printf("分配内存失败");
exit(1);
}
tos = p;
bos = p + Max -1;
printf("请输入第一个数据:/n");
scanf("%d",&a);
push(a);
printf("请输入第二个数据:/n");
scanf("%d",&b);
push(b);
printf("请输入操作符:/n");
scanf("%s",s);
switch (*s)
{
case'+':
a = pop();
b = pop();
printf("结果是a+b = %d/n",(a+b));
push(a+b);
break;
case'-':
a = pop();
b = pop();
printf("结果是a-b = %d/n",(a-b));
push(a-b);
break;
case'*':
a = pop();
b = pop();
printf("结果是a*b = %d/n",(a*b));
push(a*b);
break;
case'/':
a = pop();
b = pop();
printf("结果是a/b = %d/n",(a/b));
push(a/b);
break;
default:
printf("请输入正确操作符/n");
}
}
字符串:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedefstructnode
{
charData[80];
structnode *Next;
}nodetype;
typedefstructhead
{
intNum; /*行号*/
intLen; /*改行字符的个数*/
nodetype *Next;
}headtype;
headtypeHead[MAX];
voidInitial();
intMenuSelect();
voidEnterData();
voidDeleteLine();
voidList();
voidClearBuffer();
main()
{
charchoice;
Initial();
while(1)
{
choice = MenuSelect();
switch (choice)
{
case 1:EnterData();
break;
case 2:DeleteLine();
break;
case 3:List();
break;
case 4:exit(0);
}
}
}
voidClearBuffer()
{
while(getchar()!='/n');
}
voidInitial()
{
inti;
for(i=0;i<MAX;i++)
{
Head[i].Len=0;
}
}
intMenuSelect()
{
inti;
i=0;
printf(" 1. Enter/n");
printf(" 2. Delete/n");
printf(" 3. List/n");
printf(" 4. Exit/n");
while(i<=0||i>4)
{
printf("请输入菜单选择号/n");
scanf("%d",&i);
ClearBuffer();
}
return(i);
}
voidEnterData()
{
nodetype *p,*find();
inti,j,m,LineNumber,k;
charStrBuffer[100];
while(1)
{
printf("输入数据要插入的行号(0~100):/n");
scanf("%d",&LineNumber);
ClearBuffer();
if(LineNumber<0||LineNumber>=MAX)
return;
printf("请输入要插入的数据,以@作为结束符号/n");
i=LineNumber;
Head[i].Num=LineNumber;
Head[i].Next=(nodetype *)malloc(sizeof(nodetype));
p=Head[i].Next;
m=1;
j=-1;
StrBuffer[0]=0;
k=0;
do
{
j++;
if(!StrBuffer[k])
{
scanf("%s",StrBuffer);
k=0;
}
if(j>=80*m)
{
m++;
p->Next=(nodetype *)malloc(sizeof(nodetype));
p=p->Next;
}
p->Data[j%80] = StrBuffer[k++];
}while(p->Data[j%80]!='@');
Head[i].Len = j;
}
}
voidDeleteLine()
{
nodetype *p,*q;
inti,j,m,LineNumber;
while(1)
{
printf("输入要删除的行号(0~100):/n");
scanf("%d",&LineNumber);
if(LineNumber<0||LineNumber>=MAX)
return;
i = LineNumber;
p=Head[i].Next;
m=0;
if(Head[i].Len>0)
{
m=(Head[i].Len-1)/80+1; /*查找改行用到几个链表结点*/
}
for(j=0;j<m;j++)
{
q=p->Next;
free(p);
p=q;
}
Head[i].Len=0;
Head[i].Num=0;
}
}
voidList()
{
nodetype *p;
inti,j,m,n;
for(i=0;i<MAX;i++)
{
if(Head[i].Len>0)
{
printf("第%d行有数据,它们是:/n",Head[i].Num);
n=Head[i].Len;
m=1;
p=Head[i].Next;
for(j=0;j<n;j++)
if(j>=80*m)
{
p=p->Next;
m++;
}
else
printf("%c",p->Data[j%80]);
printf("/n");
}
}
printf("/n");
}
树:
#include <stdio.h>
#include <stdlib.h>
structtree
{
charinfo;
structtree *left;
structtree *right;
};
structtree *root; /*树的第一个结点*/
structtree *construct(structtree *root, structtree *r, charinfo);
voidprint(structtree *r, intl);
intmain(void)
{
chars[80];
root = NULL;
do
{
printf("请输入一个字符:");
gets(s);
root = construct(root,root,*s);
}while(*s);
print(root,0);
return 0;
}
structtree *construct(
structtree *root,
structtree *r,
charinfo)
{
if(!r)
{
r = (structtree *)malloc(sizeof(structtree));
if(!r)
{
printf("内存分配失败!");
exit(0);
}
r->left = NULL;
r->right = NULL;
r->info = info;
if(!root)
returnr;
if(info < root->info)
root->left = r;
else
root->right = r;
returnr;
}
if(info < r->info)
construct(r,r->left,info);
else
construct(r,r->right,info);
returnroot;
}
voidprint(structtree *r, intl)
{
inti;
if(!r)
return;
print(r->left,l+1);
for(i = 0;i < l;++i)
printf(" ");
printf("%c/n",r->info);
print(r->right,l+1);
}
#include <stdio.h>
#define Max 100
voidSetNull(front, rear)
int *front, *rear;
{
*front = 0;
*rear = 0;
}
intEmpty(front,rear)
int *front, *rear;
{
if(*front == *rear)
return(1);
else
return(0);
}
intEnQueue(q,x,front,rear)
intq[];
intx;
int *front,*rear;
{
*rear = (*rear+1) % Max;
if(*front == *rear)
{
printf("队列发生上溢/n");
return(-1);
}
else
{
q[*rear] = x;
return(0);
}
}
intDelQueue(q,y,front,rear)
intq[];
int *y;
int *front,*rear;
{
*front = (*front +1)%Max;
if(*front == *rear)
{
printf("队列发生下溢/n");
return(-1);
}
else
{
*y = q[*front];
return(0);
}
}
voidmain()
{
intq[Max];
intf = 0, r = 0; /*f和r分别对应队列的头和尾在整个队列存储区域的位置*/
inti,x,m,n;
inta;
SetNull(&f,&r); /*清空队列*/
printf("要输入队列的字符个数:/n");
scanf("%d",&m);
printf("输入队列的整型数据:/n");
for (i=0; i<m; i++)
{
i=i;
scanf("%d",&x);
a = EnQueue(q,x,&f,&r);
if(a == -1)
break;
}
printf("要提出队列的字符个数:");
scanf("%d",&n);
printf("输出从队列中提取的数据:/n");
for (i = 0; i<n; i++)
{
if(DelQueue(q,&x,&f,&r) == -1)
break;
printf("%d/n",x);
}
if(Empty(&f,&r) == 1)
printf("队列为空");
else
printf("队列中还有%d个数据",(m-n));
}