?? 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>
//靜態(tài)數(shù)據(jù)的初始化
int Node::count=0;
//定義全局變量
bool flag=false;
bool once=false;
//函數(shù)的聲明
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.輸出當(dāng)前鏈表的長度。\n4.在尾部添加鏈表。\n5.插入元素到鏈表的某個位置。\n6.刪除鏈表上的某個元素。\n7.退出\n");
// scanf("%d",&input);
while(1)
{
scanf("%c",&input);
switch(input)
{
case '1':
if(flag)
{
printf("已經(jīng)存在鏈表!");
}
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個節(jié)點(diǎn)\n",count);
printf("操作完成!\n");
}
else
{
printf("鏈表不存在!\n");
}
break;
case '4':
if(flag)
{
printf("請輸入數(shù)據(jù):\n");
scanf("%d",&num);
if(Add(Link,num))
{
printf("操作完成!\n");
}
else
{
printf("error!\n");
}
}
else
{
printf("鏈表不存在!\n");
}
break;
case '5':
if(flag)
{
printf("請輸入所要插入的數(shù)據(jù):\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("請輸入想要刪除的鏈表項下標(biāo):\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;
}
//顯示鏈表所有數(shù)據(jù)值
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;
}
//添加數(shù)據(jù)項(在鏈表尾部)
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;
}
//插入數(shù)據(jù)項(在鏈表的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;
}
//刪除數(shù)據(jù)項
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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -