#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct student
{
int date;
struct student *next;
}stud;
stud *head;
stud *creat() //创建头结点
{
stud *sq;
sq = (stud *)malloc(sizeof(stud));
sq -> next = sq;
cout << " 创建头结点成功!";
cout << endl; //
return sq;
}
void inlist(stud *p) /*初始化链表*/
{
stud *nw; //初始化链表长度
int w;
cout << " 输入初始化长度:";
cin >> w;
for(int i = 0;i < w;i++)
{
nw = (stud *)malloc(sizeof(stud));
p -> next = nw;
p = nw;
cin >> p-> date;
p -> next = head; //
}
p = head -> next;
cout << " 初始化后的链表为:";
while(p != head) //
{
cout << p -> date<<" ";
p = p -> next;
}
cout << endl;
}
int getlongs(stud *q) /*求链表长度*/
{
q = head -> next;
int i = 0;
while(q != head)
{
q = q -> next;
i++;
}
return i;
}
void inputs( stud *q) /*插入函数*/
{
int m,b;
stud *news;
q = head;
cout << "输入要插入的位置:";
cin >> m;
if(m < 1 || m > getlongs(q))
cout << "位置错误!";
else
{
cout << "输入要插入的值:";
cin >> b;
news = (stud *)malloc(sizeof(stud));
news -> next = NULL;
news -> date = b;
for(int i = 0;i < m-1;i++)
q = q -> next;
news -> next = q -> next;
q -> next = news;
}
q = head -> next;
cout << "插入后为:"; /*删除后输出*/
while(q != head)
{
cout << q -> date<<" ";
q = q -> next;
}
cout << endl;
}
void search(stud *q) //查找函数同下searchs(),
{
q = head;
int h; //h为查找的值.
cout << "输入查找的数:";
cin >> h;
int i = 0; //i为结点位置,初始化为0;
int j= getlongs(q); //先求链表长度,后面用到
while(q->next != head)
{
q = q -> next;
i++;
if(q -> date == h)
{
cout << "查找成功! 位置为:" << i << endl;
break;
}
}
//cout<<i<<" "<<"fdhj"<<endl;
if(i == j && q->date != h)
cout << "查找失败" << endl;
}
void deletes(stud *q) /*删除函数*/
{
q = head;
stud *d; //要删除的结点
int k; //要删除的位置
cout << "输入删除的位置:";
cin >> k;
if(k < 1 || k > getlongs(q))
cout << "error";
else
{
for(int j = 0;j<k-1;)
{
q = q->next;
j++;
}
d = q -> next;
q->next = d -> next;
free(d);
}
q = head -> next;
cout << "删除后为:";
while(q != head)
{
cout << q -> date<<" ";
q = q -> next;
}
cout << endl;
}
void searchs(stud *q) //查找函数同上search(),
{
q = head;
int h; //查找的值
cout << "输入查找的数:";
cin >> h;
int i = 0; //i为结点位置,初始化为0
int j = getlongs(q);
while(q -> next != head)
{
q = q->next;
i++;
if( q -> date == h)
{
cout << "查找成功! 位置为:" << i << endl;
break;
}
}
//cout<<i<<" "<<"fdhj"<<endl;
if(i == j && q -> date != h)
cout << "查找失败" << endl;
}
void searchd(stud *q)
{
q = head;
int k; //要查找 的位置
cout << "输入你要查找的位置:";
cin >> k;
if( k < 1 || k > getlongs(q))
cout<<"查找位置错误:"<<endl;
else
for(int i = 0;i < k;i++)
q = q -> next;
cout<<k<<"这个位置上的数为:";
cout << q -> date << endl;
}
void main()
{
int l,j;// i为主菜单控制变量,j为查询子菜单控制变量
stud *p;
head = creat(); //创建头结点
p = head;
//inlist(p);
//inputs(p);
//deletes(p);
//searchs(p);
//o=getlongs(p);
//searchd(p);
//cout<<"现在长度为:"<<o<<" "<<"ok! "<<endl;
cout << " 初始化链表!"<<endl;
inlist(p); //初始化链表
cout << "-----------------------------------------------------------"<<endl;
do
{
cout << "*输入1选择插入 *输入2选择删除 *输入3选择查询 *输入4选择查长度 *输入5退出*"<<endl;
cout << "输入:";
cin >> l;
switch(l)
{
case 1:
inputs(p);
break;
case 2:
deletes(p);
break;
case 3:
cout << "*输入1按值查找 输入2按位置查找*" << endl;
cout << "输入:";
cin >> j;
switch(j)
{
case 1:
searchs(p);
break;
case 2:
searchd(p);
break;
}
break;
case 4:
cout << "现在的长度为:" << getlongs(p) << endl;
break;
case 5:
goto loop; //完成是结束程序
}
cout << endl;
//if(l==5) break;
}while(1);
loop:
cout << "ok!";
}