实验内容:
1、假设在长度大于1的单循环链表中,既无头结点也无头指针。为指向某个结点的指针,试编写算法删除结点*的直接前结点
2、已知由单链表表示的线性表中,含有三类字符的数据元素(如:字母数字和其它字符),设计算法构造三个以循环链表示的线性表,使每一个表中只含同一类的字符,且利用原表中的结点空间作为这三个表的空间。(头结点可以另辟空间)
3.有一双链表,每个结点中除有 prior、data和next域外,还有一切问频度域freq,在链表被启用前,其值均初始化为零。每当在表上进行一次 LOCATE(L, x)运算,元素值为x的结点中freq域的值增1,并使此表中结点保持按frq递减的顺序排列,以便使频繁访问的结点总是靠近表头。设计满足上述要求的LOCATE算法
实验代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STACK_INIT_SIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef char SElemType;
//struct of LNode
typedef struct LNode
{
char data; //data
struct LNode *next; //pointer
}LNode, *LinkList;
//struct of stack
typedef struct
{
SElemType *base; //base pointer
SElemType *top; //top pointer
int stacksize; //max size of stack
}SqStack;
//init of stack
Status initStack(SqStack *s)
{
s->base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); //open a size
if(!s->base) exit(OVERFLOW);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return OK;
}
//insert e into sqstack
Status push(SqStack *s, SElemType e)
{
if(s->top-s->base == s->stacksize) return ERROR; //stack is full
*s->top++=e; //s->top=e; s->top++;
return OK;
}
//get out of stack
Status pop(SqStack *s, SElemType e) //using "e" to return TOP number and delete it
{
if(s->top == s->base) return ERROR; //nothing in the stack
if(e==*(--s->top)) {return OK;}
else {return ERROR;}
}
//func of creating a single linked in the first Question
LinkList create_single_linked(int n)
{
int i; //using for input data
LinkList L_head; //head of the link
L_head=(LinkList)malloc(sizeof(LinkList));
printf("plese input a char:");
scanf("%s",&L_head->data);
L_head->next=NULL;
LinkList temp=L_head;
for(i=1;i<n;i++)
{ //input data into link
LinkList insert_linked=(LinkList)malloc(sizeof(LinkList));
printf("plese input a char:");
scanf("%s",&insert_linked->data);
insert_linked->next=NULL;
//link together
temp->next=insert_linked;
temp=insert_linked;
}
return L_head;
}
//func of printing single linked
Status print_single_linked(LinkList L_head)
{
LinkList temp=L_head;
do{
printf("%c",temp->data);
temp=temp->next;
}while (temp!=NULL);
return OK;
}
//Func of using "whether is symmetry"
Status use_whether_symmetry()
{
int n;
int i; //for push and pull
LinkList head;
SqStack stack;
printf("how many node to create:");
scanf("%d",&n);
//creating a single linked
head=create_single_linked(n);
print_single_linked(head); //print the single linked
printf("\n");
LinkList temp=head;
initStack(&stack);
int half=n/2; //half of numbers of linked
for(i=0;i<half;i++)
{ //push into stack
push(&stack,temp->data);
temp=temp->next;
}
if(n%2!=0) {temp=temp->next;}
int empty; //OK or ERROR
do{
//pop the data
empty=pop(&stack, temp->data);
temp=temp->next;
} while(temp!=NULL);
//Judge whehter is void
if(stack.top==stack.base && empty==OK) {
printf("Yes!!!\n");
}
else {printf("No!!!\n");}
//whether again
char choose;
printf("again?(y/n):");
scanf("%s",&choose);
if(choose=='y') {return OK;}
else {return ERROR;}
}
//Judge 2
Status use_whether_pair()
{
printf("please enter an expression:");
char expression[20];
scanf("%s",expression); //input a string
printf("%s",expression);
//~printf("\n%ld", strlen(expression));
//using stack
SqStack stack;
initStack(&stack);
int i;
for(i=0;i<strlen(expression);i++) //input all data into stack
{
if(expression[i]=='(')
{
push(&stack, expression[i]);
}
if(expression[i]==')')
{
pop(&stack, '(');
}
}
//Judge whether matching
if(stack.top==stack.base) {printf(" is matching!!!\n");}
else {printf(" is no matching!!!\n");}
//whether again
char choose;
printf("again?(y/n):");
scanf("%s",&choose);
if(choose=='y') {return OK;}
else {return ERROR;}
}
//func of whether back to menu
Status whether_toMenu()
{
char choose_two;
printf("back to menu(y/n):");
scanf("%s",&choose_two);
if(choose_two == 'y') {return OK;}
else {return ERROR;}
return OK;
}
//main menu
Status menu()
{
int choose_one;
char choose_two;
do{
printf("\n*********************\n");
printf("1.Whether symmetry\n");
printf("2.Whether pair\n");
printf("3.Queue\n");
printf("4.Quit\n");
printf("**********************\n");
printf("plese input your choose(1~4):");
scanf("%d",&choose_one);
//1.Whether symmetry
if(choose_one == 1)
{
//Judge 1
while(use_whether_symmetry()==1);
choose_two=whether_toMenu();
if(choose_two==1) {continue;}
else {break;}
}
//2.Whether pair
else if(choose_one == 2)
{
//Judge 2
while(use_whether_pair()==1);
choose_two=whether_toMenu();
if(choose_two==1) {continue;}
else {break;}
}
//3.Queue
else if(choose_one == 3)
{
//Judge 3
choose_two=whether_toMenu();
if(choose_two==1) {continue;}
else {break;}
}
//4.Quit
else
{
break;
}
}while(choose_one!=4);
return OK;
}
int main()
{
menu();
return 0;
}