课程设计3--线性链表

// LinkList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "conio.h"
#include "iostream.h"
#include "LList.h"

#define OK       1
#define ERROR    0

int main(int argc, char* argv[])
{
 bool key = true ;//判断操作是否继续
 char str ,opr;//接受输入字符以判断是否继续
 int coutnum=0;//初始化链表长度
 int input;//输入的元素
 int output=0;//输出的元素
 int pos;//要操作的位置
 int t_num = 0;//链表元素的总数
 LinkList L;
 int i=1;
//***********初始化*******************//
 if(!InitLink(L))
   return ERROR;
    AboutAuthor();
 cout << "/n/n/t************************线性链表的操作*************************/n/n";
 cout << "/t初始化链表..../n";
 cout << "/t输入初始链表长度:";
 cin >> coutnum;

 while ( coutnum >0 )
 {
  printf("/n/t输入第%d个元素:" ,i);
  cin >> input;
  if(!ListInsert( L, input ))
   cout << "/n/t插入失败!/n";
  coutnum--;
  i++;
 }

 while(key)
 {
  cout << "/n/n/n/t/t**********************链表操作***********************";
  cout << "/n/t/tPlease select what you want  to do:/n/n/n";
  cout << "/t/tI:插入  D:删除   S:查找  C:计数  P:输出   Q:退出/n";
  cout << "/t/t";
  if(!print_all(L))
   cout << "No record!";
  cout << "/n/t/t*******************************************************";
  cout << "/n/nSelect:";
  cin >> opr;

  switch( opr ){
  case 'i':
  case 'I':
   cout << "选择插入方式:/n/n";
   cout << "/t/t直接插入------按D,/n/t/t按位置插入----按P/n/n";
   cin >> str;
   cout << "Input the value(整形):";
   if( str == 'd'||str == 'D')
   {
    cin >> input;
    if(!ListInsert( L,input ))
     cout << "插入失败!";
   }
   if( str == 'p'||str == 'P' )
   {
    cin >> input;
    cout << "/n输入插入位置:";
    cin >> pos;
    if(!ListInsert_L(L,pos,input))
     cout << "插入失败!";
   }
   break;
  case 'd': 
  case 'D':
  cout << "/n输入要删除的位置:";
  cin >> pos;
  if(!ListDelete_L(L,pos,output))
   cout << "Not the right position!";
   break;
  case 's':
  case 'S':
   cout << "选择查找方式:/n/n";
   cout << "/t/t按元素查找------按E,/n/t/t按位置查找----按P/n/n";
   cin >> str;
   //按元素查找
   if( str == 'e'||str == 'E')
   {
    cout << "/n输入元素:";
    cin >> input;
    if(!SearchLink_Elem( L,pos,input ))
     cout << "No the element!";
    cout << "/n位置是:"<<pos;
   }
   //按位置查找
   if( str == 'p'||str == 'P' )
   {
    cout << "/n输入位置:";
 
    cin >> pos;
    if(!SearchLink_Pos(L,pos,output))
     cout << "No the position!";
    cout << "/n这个元素是:"<<output;
   }
   break;
 
  case 'c':
  case 'C':
   CountLink_L( L, t_num );
   cout << "/n元素总数:"<<t_num;
   break;
 
  case 'p':
  case 'P':
   cout << "选择输出方式:/n/n";
   cout << "/t/t输出某一位置元素------按P,/n/t/t按所有元素----按A/n/n";
   cin >> str;
   if( str == 'p'||str == 'P' )
   {
    cout << "输入元素位置:";
    cin >> pos;
    if(!print_elem(L,pos))
     cout << "No the position!"; 
   }
   if( str == 'a'||str == 'A')
    if(!print_all(L))
     cout << "No record!";
   break;

  case 'q':
  case 'Q':
   key = false;
   break;
  default: cout << "Not the operation!";
  }
 }
 return OK;
}

 

#include "stdafx.h"
#include "malloc.h"


#define OVERFLOW 0
#define NULL     0
#define OK       1
#define ERROR    0

typedef struct LNode
{
 int data;
 struct LNode *next;

}LNode,*LinkList;

void AboutAuthor()
{
 cout << "/n/t/t/t/t/t/t/t/tBy:江江(宋保江)"<<endl;
 cout << "/t/t/t/t/t/t/t/tAdreess:计本043"<<endl;
 cout << "/t/t/t/t/t/t/t/t山东建筑大学"<<endl;
 cout << "/t/t/t/t/t/t/t/tQQ:282719081"<<endl;
 cout << "/t/t/t/t/t/t/t/tTel:13793141761";
}

bool InitLink( LinkList &L )
{
 L = ( LinkList )malloc( sizeof( LNode ) );
 if( !L )   return OVERFLOW;

 L->next =  NULL;
    return OK;
}


bool ListInsert_L( LinkList &L,int pos,int e )
{// 在带头结点的单链表L中第i个数据元素之前插入数据元素e
 int j = 0;
 LinkList p  ;
 LinkList q  ;
 p = L;

 while( p && j < pos - 1 )
 {
  p = p->next;
  ++j;
 }// 寻找第i-1个结点

 // i小于1或者大于表长
 if( !p || j > pos - 1 ) return ERROR;

 q = ( LinkList )malloc( sizeof( LNode ) );// 生成新结点

 // 插入L中
 q->data = e;
 q->next = p->next;
 p->next = q;
 
 return OK;
}

bool ListInsert( LinkList &L,int e )
{
 // 在带头结点的单链表L的最后插入数据元素e
 LinkList p;
 LinkList q; 

 p = L;
 q = ( LinkList )malloc( sizeof( LNode ) );// 生成新结点
  
 while(p->next)
  p = p->next;

 // 插入L链表中
 q->data = e;
 q->next = NULL;
 p->next = q;

 return OK;
}

bool ListDelete_L( LinkList &L,int pos,int &e )
{
 // 在带头结点的单链表L中,删除第i个元素,并由e返回其值

 LinkList p ,q ;
 p = L;
    int j = 0;

 while( p->next && j < pos - 1 )
 {// 寻找第i个结点,并令p指向其前趋
  p = p->next;
  ++j;
 }

 if(!( p->next ) || j > pos - 1) return ERROR;// 删除位置不合理

 q = p->next; p->next = q->next; // 删除并释放结点
 e = q->data; free(q);
 
 return OK;
}

bool CountLink_L( LinkList &L,int &t_num )
{
 LinkList p;
 t_num = 0;
 p = L;

 while( p->next != NULL )
 {
  p = p->next;
  t_num++;
 
 }

 return OK;
}

bool SearchLink_Pos( LinkList &L,int s_pos ,int &e)
{
 //返回给定位置的元素
 LinkList p;

 int j = 0;

 p = ( LinkList )malloc( sizeof( LNode ) );// 生成新结点
 if( !p ) return OVERFLOW;

 p = L;

 while (p && j < s_pos )
 {
  p = p->next; ++j;
 } // 寻找第s_pos - 1个结点

 if(! p || j > s_pos ) return ERROR;// 位置不合理

 e = p->data;

 return OK;
}

bool SearchLink_Elem( LinkList &L,int &e_pos,int e )
{
 //返回给定元素的位置
 LinkList p;
 e_pos = 0;

 p = L;
 p = p->next;

 while (p && p->data != e)
 {
  p = p->next;
  e_pos ++;
 } // 寻找第i-1个结点

 if( p == NULL ) return ERROR;
 ++e_pos;//将e_pos指向与e相同的元素
 return OK;
}

bool print_all( LinkList &L)
{
 LinkList p;
 p = L;
 if( !p ) return ERROR;//没有记录
 
 p = p->next;//指向首节点
 cout << "/n/n/t/t当前链表:/n";
 cout << "/t/t头结点";
 //输出
 while( p )
 {
  cout << "---->";  
  cout <<  p->data ;
  p = p->next;
 }

 return OK;
}

bool print_elem( LinkList &L,int pos )
{
 int e=0;

 if( !SearchLink_Pos( L, pos , e) )
  return ERROR;

 cout << "第" << pos ;
 cout << "个元素为:";
 cout << e ;
 return OK;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值