线性表单元测验用实训

第1关:顺序表基本操作及应用

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 
 5 typedef int DataType;
 6 struct SeqList{
 7     DataType  *elem;
 8     int Max; //表示线性表最大长度
 9     int curNum;//表示线性表中数据元素的个数
10 } ;
11 
12 
13 typedef  struct SeqList *PseqList;
14 
15 //函数功能:创建一个空的顺序线性表,线性表最多能存放max个数据元素
16 PseqList createNullList(int max)
17 {
18     PseqList head =(struct SeqList*) malloc(sizeof(struct SeqList)) ;
19     head->curNum = 0;
20     head->Max = max ;
21     head->elem = (DataType* )malloc(sizeof(DataType)*max);
22     return head ;
23 }
24 
25 //函数功能: 在顺序线性表表尾插入数据元素
26 void insertTail(PseqList slist, DataType x)
27 {
28     //此处填写代码,实现在顺序表表尾插入数据元素x的功能
29     /********** Begin **********/
30     /*
31     PseqList p= slist;
32     int cnt=0;
33     while (cnt<slist->curNum)
34     {
35         cnt++;
36     }
37    */
38     if (slist->curNum+1<=slist->Max)
39     {
40     slist->elem[slist->curNum]=x;
41     slist->curNum++;
42 
43     }
44     /********** End **********/
45 }
46 
47 //输出线性表中所有数据元素,以空格为分界符
48 void printList(PseqList slist)
49 {
50     for(int i = 0 ;i < slist->curNum ;i++)
51         printf("%d ",slist->elem[i]) ;
52 }
53 
54 void replace(PseqList slist , DataType x , DataType y)
55 {
56     //在此处填写代码,实现将线性表slist中数据元素值为x的替换为值y
57     /********** Begin **********/
58     for (int i = 0; i < slist->Max; ++i) {
59         if (slist->elem[i]==x)
60         {
61             slist->elem[i]=y;
62         }
63     }
64 
65     /********** End **********/
66 }
67 
68 int  main(void){
69     PseqList  L1 ;
70     int n=20, x , i;
71     L1 = createNullList( n);
72     int datanum;
73     scanf("%d",&datanum) ; //输入待插入数据元素个数
74     //读入待插入的数据元素,并插入到线性表尾部
75     for(i =0 ;i < datanum ;i++)   {
76         scanf("%d",&x);
77         insertTail(L1 ,x );
78     }
79 
80     scanf("%d",&x);
81     replace(L1 , x , 999); //将线性表L1中值为x的数据元素替换为999
82     printList(L1);
83 }
View Code

 第2关:链式线性表操作

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 typedef int DataType;
 4 
 5 struct node
 6 {
 7     DataType saleAmount ;//销售数量
 8     struct node *next;//指向下一产品的指针
 9 } ;
10 
11 
12 typedef struct node * pNode;
13 
14 //函数功能: 遍历链表并输出链表中各数据元素的值 
15 void traverseList_link(pNode pllist) //功能是输出线性表pllist的元素,以空格为分界符;
16 {
17     pNode temp = pllist->next ;
18     while(temp != NULL)
19     {
20         printf("%d ",temp->saleAmount);
21         temp = temp->next;
22     }
23 }
24 
25 //创建一个空的链表 
26 pNode  createNullList_link(void  )
27 {
28     struct node* temp = (struct node*)malloc(sizeof(struct node)) ;
29     temp->next = NULL;
30     temp->saleAmount = 0;
31     return temp;
32 }
33 
34 void insert_link(pNode pllist ,DataType x )
35 {
36     pNode temp =  (struct node*)malloc(sizeof(struct node)) ;
37     temp->saleAmount = x ;
38     temp->next = NULL;
39     pNode p = pllist->next , pre = pllist;
40     while(p!=NULL)
41     {
42         pre = p ;
43         p = p->next ;
44     }
45     pre->next = temp ;
46 }
47 
48 
49 void reverseList_link( pNode pllist)
50 {
51     //请在此处填入代码,实现链表逆置功能 
52     /********** Begin **********/
53     if (pllist==NULL||pllist->next==NULL)
54     {
55         return;
56     }
57 pNode p,q,r;
58 p=q=NULL;
59 r=pllist->next;
60 while (r!=NULL)
61 {
62     p=r->next;
63     r->next=q;
64     q=r;
65     r=p;
66 }
67 pllist->next=q;
68 
69     /********** End **********/
70 }
71 
72 int main(void)
73 {
74     pNode pllist = createNullList_link() ;
75     int num ;
76     DataType data;
77     scanf("%d",&num);
78     for(int i = 0;i < num; i++)
79     {
80         scanf("%d",&data);
81         insert_link(pllist, data);
82     }
83     reverseList_link(pllist)    ;
84     traverseList_link(pllist);
85 }
86  
View Code

 第3关:利用栈操作,实现平衡符号判定

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 typedef char DataType;
  6 
  7 //采用链式栈 
  8 struct node{
  9     DataType data;  //数据元素
 10     struct node *next; //指向下一个数据元素的指针
 11 };
 12 
 13 typedef struct node *PtrToNode;
 14 typedef PtrToNode Stack;
 15 
 16 //初始化一个空栈 
 17 Stack createNullStack(void)
 18 {
 19     PtrToNode head = (struct node* )malloc(sizeof(struct node));
 20     head->next = NULL;
 21     return head;
 22 }
 23 
 24 //入栈函数,参数:入栈数据元素,栈头指针
 25 void push(DataType x , Stack s)
 26 {
 27     //请在此处填写代码,实现入栈操作
 28     /********** Begin **********/
 29     //since functionPop is pop from head stack next one
 30     PtrToNode sta = (struct node* )malloc(sizeof(struct node));
 31     sta->data=x;
 32     sta->next=s->next;
 33     s->next=sta;
 34    /* PtrToNode p=s;
 35     while (p->next!=NULL)
 36     {
 37         p=p->next;
 38     }
 39     p->next=sta;
 40 */
 41     /********** End **********/
 42 }
 43 
 44 //判定栈是否为空,若栈为空,则返回1,否则返回0 
 45 int isEmpty(Stack s)
 46 {
 47     //请在此处填写代码,实现判定栈是否为空操作,若栈为空,则返回1,否则返回0
 48     /********** Begin **********/
 49 if (s==NULL)
 50 {
 51     return 1;
 52 
 53 }
 54     return 0;
 55 
 56     /********** End **********/
 57 }
 58 
 59 //出栈,并返回栈顶元素
 60 DataType pop(Stack s)
 61 {
 62     if(isEmpty(s) == 0)
 63     {
 64         DataType x = s->next->data;
 65         PtrToNode p = s->next ;
 66         s->next = p->next ;
 67         free(p) ;
 68         return x ;
 69     }
 70     else
 71         return 0;
 72 }
 73 
 74 void balance( char str[])
 75 {
 76     //请在此处填写代码,实现平衡符号的判定,若判断不匹配, 输出  不匹配, 否则输出  匹配
 77     /********** Begin **********/
 78 int i=0;
 79 Stack head=createNullStack();
 80 while (str[i]!='\0')
 81 {
 82 if (str[i]=='('||str[i]=='{'||str[i]=='[')//left sign push to stack
 83 {
 84     push(str[i],head);
 85 }
 86 
 87 if (str[i]==')'||str[i]=='}'||str[i]==']')//right sign pop from stack
 88 {
 89     if (isEmpty(head))
 90     {
 91         printf("不匹配");
 92         return;
 93     }
 94     DataType d=pop(head);
 95     //match pop
 96     if (str[i]==')')
 97     {
 98         if (d!='(')
 99         {
100             printf("不匹配");
101             return;
102         }
103     }
104     if (str[i]==']')
105     {
106         if (d!='[')
107         {
108             printf("不匹配");
109             return;
110         }
111     }
112     if (str[i]=='}')
113     {
114         if (d!='{')
115         {
116             printf("不匹配");
117             return;
118         }
119     }
120 }
121 i++;
122 }
123 if (isEmpty(head))
124 {
125     printf("不匹配");
126     return;
127 }
128 printf("匹配");
129     return;
130     /********** End **********/
131 }
132 
133 int main()
134 {
135     char str[90] ;
136     gets(str) ;
137     balance(str);
138 }
139  
View Code

 

posted @ 2020-12-01 18:10  ethon-wang  阅读(293)  评论(0编辑  收藏  举报