📄 link.cpp
字号:
// LINK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "link.h"
#include "stdlib.h"
#include<string.h>
//静态数据的初始化
int Node::count=0;
//定义全局变量
bool flag=false;
bool once=false;
//函数的声明
LinkList InitList();
bool ShowList(LinkList );
int ShowLen(LinkList );
bool Add(LinkList ,int );
bool Insert(LinkList ,int ,int);
bool DelItem(LinkList ,int );
int main(int argc, char* argv[])
{
LinkList Link;
int count=0;
char input;
int num=0;
int index=0;
char c[2];
printf("1.初始化链表。\n2.输出链表。\n3.输出当前链表的长度。\n4.在尾部添加链表。\n5.插入元素到链表的某个位置。\n6.删除链表上的某个元素。\n7.退出\n");
// scanf("%d",&input);
while(1)
{
scanf("%c",&input);
switch(input)
{
case '1':
if(flag)
{
printf("已经存在链表!");
}
else
{
// Link=(LinkList )malloc(sizeof(struct Node));
Link=InitList();
printf("操作完成!\n");
flag=true;
}
break;
case '2':
if(flag)
{
if(ShowList(Link))
{
printf("操作完成!\n");
}
else
{
printf("error!\n");
}
}
else
{
printf("链表不存在!\n");
}
break;
case '3':
if(flag)
{
count=ShowLen(Link);
printf("共有%d个节点\n",count);
printf("操作完成!\n");
}
else
{
printf("链表不存在!\n");
}
break;
case '4':
if(flag)
{
printf("请输入数据:\n");
scanf("%d",&num);
if(Add(Link,num))
{
printf("操作完成!\n");
}
else
{
printf("error!\n");
}
}
else
{
printf("链表不存在!\n");
}
break;
case '5':
if(flag)
{
printf("请输入所要插入的数据:\n");
scanf("%d",&num);
printf("请输入所要插入的位置:(1--%d)",Node::count);
scanf("%d",&index);
if(Insert(Link,num,index))
{
printf("操作完成!\n");
}
else
{
printf("out of scope!\n");
}
}
else
{
printf("链表不存在!\n");
}
break;
case '6':
{
if(flag)
{
printf("请输入想要删除的链表项下标:\n");
scanf("%d",&index);
if(DelItem(Link,index))
{
printf("完成操作!\n");
}
else
{
printf("Out of scope!\n");
}
}
else
{
printf("链表不存在!\n");
}
}
break;
case '7':
{
exit(1);
}
default :
{
printf("1.初始化链表。\n2.输出链表。\n3.在尾部添加链表。\n4.插入元素到链表的某个位置。\n5.删除链表上的某个元素。\n");
}
}
}
return 0;
}
//链表初始化
LinkList InitList()
{
LinkList list;
list=new struct Node;
// list->count=0;
Node::count++;
return list;
}
//显示链表长
int ShowLen(LinkList list)
{
int count=0;
for(;list->pNext!=NULL;list=list->pNext)
{
count++;
}
return count;
}
//显示链表所有数据值
bool ShowList(LinkList list)
{
LinkList p=list;
for(p=p->pNext;p!=NULL;p=p->pNext)
{
printf("%d\n",p->nodeItem);
// list=list->pNext;
}
return true;
}
//添加数据项(在链表尾部)
bool Add(LinkList list,int num)
{
pNode node=new struct Node;
node->nodeItem=num;
for(int i=0;i<Node::count-1;i++)
{
list=list->pNext;
}
list->pNext=node;
Node::count++;
return true;
}
//插入数据项(在链表的INDEX位置插入)
bool Insert(LinkList list,int num,int index)
{
pNode node=new struct Node;
index--;
if(index>=Node::count||index<=0)
{
return false;
}
if(list->pNext==NULL)
{
Add(list,num);
return true;
}
for(int i=0;i<index;i++)
{
list=list->pNext;
}
node->pNext=list->pNext;
node->nodeItem=num;
list->pNext=node;
Node::count++;
return true;
}
//删除数据项
bool DelItem(LinkList list,int index)
{
LinkList temp;
index--;
if(index>LinkList->count)
{
return false;
}
for(int i=0;i<index;i++)
{
list=list->pNext;
}
temp=list->pNext;
list->pNext=list->pNext->pNext;
delete temp->pNext;
delete temp;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -