数据结构单链表、双向链表的基本操作

#include<stdlib.h>
#include<stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;

#define ok 1
#define error 0
#define null 0

typedef struct node//define a structure;
{    int data;
     struct node * next;//pointer to the subsequents
     struct node * front;//precursor point  
     char name;
     int order;
}lnode,*linklist;

void initlist(linklist &head)//initialize hte list
{      head=new lnode;
    head->next=null; 
}

int creat(linklist &head)//creat your linked list;
{    int n,e;
    linklist temp=(linklist)malloc(sizeof(lnode));
    temp=head;
    head->front=temp;
    cout<<"please enter the count of your list:\n";
    cin>>n;
    temp->order=0;
    for(int i=1;i<=n;i++)
    {    linklist p=new lnode;
        cout<<"the data of the "<<i<<"th member in the list :\t";
        cin>>e;
        p->data=e;
        temp->next=p;
        p->next=null;
        p->front=temp;    
        p->order=p->front->order+1;
        temp=p;//Replace the old temp with the new one; Then cycle again;
    } 
    return 1;
}

void printlist(linklist head)//traverse your list
{    linklist p=head->next;
    while(p!=null)
    {    cout<<"the ["<< p->order<<"th] member is :\t"<<p->data<<"\n";
        p=p->next;
    }
    cout<<"\b"<<"finish"<<"\n";    
}

int insertlist(linklist &head,int i,int e)//insert
{    linklist p=head;
    int j=0;
    while(p&&j<i-1)//lookint for the insert position
    {    p=p->next;
        ++j;
    }
    if(!p||j<i-1) return 0;
    linklist temp=new lnode;
    temp->data=e;
    p->next->front=temp;
    temp->next=p->next;
    p->next=temp;
    temp->front=p;
    temp->order=p->order+1;
    if(i!=1)// normal
    {    while(p)
        {    p->order=p->front->order+1;
            p=p->next;
        }
       }
       if(i==1)//special circumstances insert first
       {    p=p->next;
           while(p)
        {    p->order=p->front->order+1;
            p=p->next;
        }        
       }
    return 1;
}    
int insert(linklist &head)    
{    int i,e,flag;
    cout<<"insert the position and element";
    cin>>i>>e;
    flag=insertlist(head,i,e);
    if(flag)
    {    cout<<"success\n";
        printlist(head);
    }
    return 1;
}
        
int deletelist(linklist &head,int i)
{    linklist p=head;
    int j=0;
    while(p->next&&j<=i-1)
    {    p=p->next;
        ++j;
    }
    p->front->next=p->next;
    p->next->front=p->front;
    p=p->next;
    while(p!=null)
    {    p->order=p->front->order+1;
        p=p->next;
    }
}
int deleteuy(linklist &head)
{    int i;
     cout<<"the position : ";
     cin>>i;
     deletelist(head ,i);
     printlist(head);
     return 1;
}

void locate_data_list(linklist head)
{    int i,j=0;
linklist p=head;
    cout<<"the position : ";
    cin>>i;
    while(j<i)
    {     p=p->next;
        j++;
    }
    cout<<"the data which belong to the "<<i<<"th member is"<< p->data<<"\n"; 
}
void locate_order_list(linklist head)
{    int e,i=1;
    linklist p=head;
    cout<<"the element : ";
    cin>>e;
    while(p->data!= e)
    {    p=p->next;
    }
    cout<<"the psition which element is ["<< e<<"] is the "<<p->order<<"th\n";
}

int menu()
{    linklist l;
    initlist(l);
    cout<<"please enter your instructions \n";
    cout<<"a or A : creat,b or B : insert,c or C : delete,d or D : inquire data or order,e or E : output,ctrl+Z : finish\n";
    char choice;
    while(scanf("%c",&choice)!=EOF)
    {    switch(choice)
        {      case 'a':
            case 'A':{creat(l);break;}
            case 'b':
            case 'B':insert(l);break;
            case 'c':
            case 'C':deleteuy(l);break;
            case 'd':locate_data_list(l);
            case 'D':locate_order_list(l); break;
            case 'E':
            case 'e':printlist(l);break;
            default :cout<<"error or the next instructions";break;
        }
    }
    return 1;
}

int main()
{   menu();
    system("pause");    
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值