?? link(待填空).cpp
字號:
// 這是使用應用程序向導生成的 VC++
// 應用程序項目的主項目文件。
/* 鏈表操作:插入、刪除、輸出、翻轉
用帶表頭的鏈表存放輸入的數據,每讀入一個數,按升序順序插入到鏈表中,鏈表中允許兩個結點有相同值。鏈表的頭結點存放鏈表后面的結點個數,初始化時就生成頭結點(初值為0)。鏈表翻轉是把數據逆序(變成降序),注意,頭結點不動。翻轉后要再翻轉一次,恢復升序后才能插入新元素,否則會出錯。 */
#include <stdio.h>
#include "malloc.h"
#define null 0
struct node
{
float data;
struct node *next;
};
void insert(float aa,struct node *vq)
{
struct node *newnode,*p;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=aa;
p=vq;
while ((p->next!=null)&&(p->next->data<aa))
p = p->next/* 填空1 */;
newnode->next=p->next;
p->next = newnode/* 填空2 */;
vq->data=vq->data+1;
}
void dele(float aa,struct node *vq)
{
struct node *p,*q;
p=vq;
while ((p->next!=null)&&(p->next->data != aa))
p=p->next/* 填空3 */;
if ((p->next==null)||(p->next->data!=aa))
printf("\n%5.1f is not in the link !",aa);
else if (p->next->data==aa)
{
q=p->next;
p->next=q->next/* 填空4 */;
free(q/* 填空5 */);
vq->data=vq->data-1;
}
}
void print(struct node *vq)
{
struct node *p;
printf("\nthe length of link is %4.0f",vq->data);
p=vq->next;
printf("\nthe link is:");
while (p!=NULL/* 填空6 */)
{
printf("%5.1f",p->data);
p = p->next/* 填空7 */;
}
}
void reverse(struct node **vq)
{
struct node *q,*p,*temp;
p = ( *vq )->next;
( *vq )->next = p;
temp = ( *vq )->next;
while( p != null )
{
q = p;
p = p->next;
q->next = ( *vq )->next;
( *vq )->next = q;
}
temp->next = NULL;
}
void main()
{
int mark=1,op;
float aa;
struct node *vq;
vq=(struct node *)malloc(sizeof(struct node));
vq->data=0;
vq->next=null;
while (mark==1)
{
printf("\nWhich kind of operation will you select ? ");
printf("\ninsert---1, delete---2, print---3, reverse---4, exit---0 : ");
scanf("%d",&op);
switch (op)
{
case 0: mark=0;
break;
case 1: printf("\nPlease input the new element:");
scanf("%f",&aa);
insert(aa,vq);
print(vq);
break;
case 2: if (vq->data==0)
printf("\n the link is null !",aa);
else
{
printf("\nPlease input the deleted element:");
scanf("%f",&aa);
dele(aa,vq);
print(vq);
}
break;
case 3: print(vq);
break;
case 4: reverse(&vq);
print(vq);
break;
default:printf(" input error!\n");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -