用C++完成实验 单链表的建立及基本操作

2024-11-09 10:44:25
推荐回答(2个)
回答1:

#include
#include
/* 定义ElemType为int类型 */
typedef int ElemType;
#define TRUE 1
#define FALSE 0

#define flag -1

/* 单链表的结点类型 */
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode,*LinkedList;

/* 初始化单链表 */
LinkedList LinkedListInit()
{
LinkedList L;
L=(LinkedList)malloc(sizeof(LNode));
L->next=NULL;
return L;
}

/* 清空单链表 */
void LinkedListClear(LinkedList L)
{
L->next=NULL;

}

/* 检查单链表是否为空 */
int LinkedListEmpty(LinkedList L)
{
if (L->next==NULL) return TRUE;
else return FALSE;
}

/* 遍历单链表 */
void LinkedListTraverse(LinkedList L)
{
LinkedList p;
p=L->next;
while (p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
//求链表长度
int LinkedListLength (LinkedList L)
{
LinkedList p;
int j;
p=L->next;
j=0;
while (p!=NULL)
{
j++;
p=p->next;
}
return j;
}
//
LinkedList LinkedListGet (LinkedList L, int i)
{
LinkedList p;
int j;
p=L->next;
j=1;
while (p!=NULL && j {
p=p->next;
j++;
}
if (j==i) return p;
else return NULL;
}

LinkedList LinkedListLocate ( LinkedList L, ElemType x)
{
LinkedList p;
p=L->next;
while ( p!=NULL && p->data != x)
p=p->next;
return p;
}

void LinkedListInsert(LinkedList L, int i, ElemType x)
{
LinkedList pre,p,s;
int j;
pre=L;
j=1;
p=L->next;
while (pre&&j {
pre=p;
p=p->next;
j++;
}
if (pre==NULL)
{
printf("给的i值超过了表长");
exit(0);
}
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
pre->next=s;
s->next=p;
}

void LinkedListDel (LinkedList L,ElemType x)
{
LinkedList pre,p;
int j;
pre=L;
j=1;
p=L->next;
while (p&&p->data!=x)
{
pre=p;
p=p->next;
j++;
}
if (p==NULL)
{
printf("表中没有值为x的结点");
exit(0);
}
pre->next=p->next;
free(p);
}

LinkedList LinkedListCreat( )
{
LinkedList L=LinkedListInit(),p,r;
ElemType x;
r=L;
printf("please input data,input -1 is end\n");
scanf("%d",&x);
while (x!=flag)
{
p=(LinkedList)malloc(sizeof(LNode));
p->data=x;
r->next=p;
r=p;
scanf("%d",&x);
}
r->next=NULL;
return L;
}

int main()
{
int quit=0;
int i;
ElemType e;
LinkedList L;
while (!quit)
{
int d;
printf("please input the operation\n");
printf("1.初始化链表 2.清空链表3.求链表长度4.检查链表是否为空\n");
printf("5.遍历链表 6.从链表中查找元素\n");
printf("7.从链表中查找与给定元素值相同的元素在顺序表中的位置\n");
printf("8.向链表中插入元素9. 从链表中删除元素10.创建一个单链表\n");
printf("其他键退出。。。。。\n");
scanf("%d",&d);
switch (d)
{
case 1:
L=LinkedListInit();
break;
case 2:
LinkedListClear(L);
break;
case 3:
printf("链表长度为:%d\n",LinkedListLength(L));
break;
case 4:
if (LinkedListEmpty(L))printf("true\n");
else printf("false\n");
break;
case 5:
LinkedListTraverse(L);
break;
case 6:
printf("请输入查找的位置.\n");
scanf("%d",&i);
printf("位置是: %d\n",LinkedListGet(L,i));
break;
case 7:
printf("请输入数值.\n");
scanf("%d",&e);
printf("位置是: %d\n",LinkedListLocate(L,e));
break;
case 8:
printf("请输入插入的位置和值.\n");
scanf("%d%d",&i,&e);
LinkedListInsert(L,i,e);
break;
case 9:
printf("请输入删除的数值:\n");
scanf("%d",&e);
LinkedListDel(L,e);
break;
case 10:
L=LinkedListCreat();
break;
default:
quit=1;
}

}
return 0;
}

回答2:

1.整数
# include "iostream.h"
# include "stdlib.h"
# define NULL 0
typedef struct list{
int data;
struct list* next;
}list,*LIST;
void create(LIST& head){//创建链表
LIST p1,p2;
head=p1=p2=(LIST)malloc(sizeof(list));
cout<<"please input a int type num,quit by pressing 0\ndata: ";
cin>>p1->data;
for(;p1->data!=0;){
p2=p1;
p1=(LIST)malloc(sizeof(list));
cout<<"data: ";
cin>>p1->data;
p2->next=p1;
}
p2->next=NULL;
if(head->data==0)
head=NULL;
}
void insert(LIST& head){//把元素插入链表
LIST p;
p=(LIST)malloc(sizeof(list));
cout<<"please input a int type data you want to insert\ndata: ";
cin>>p->data;
p->next=head;
head=p;

}
void Delete(LIST& head){//删除链表元素
LIST p,q;
p=head;
if(head==NULL)
cout<<"list NULL,erro\n";
else{
cout<<"please input a int type num you want to delete\ndata: ";
int a;
cin>>a;
for(;p->data!=a&&p->next!=NULL;p=p->next)
q=p;
if(p->next==NULL&&p->data!=a)
cout< else if(p->data==a)
if(p!=head)
q->next=p->next;
else
head=head->next;
}
}
void SORT(LIST& head){ //链表排序
LIST p,q,s;
if(head==NULL)
cout<<"list is NULL,you have no need to sort it\n";
else{
for(p=head;p!=NULL;p=p->next){
s=p;
for(q=p;q!=NULL;q=q->next)
if(s->data>q->data)
s=q;
int a=p->data;
p->data=s->data;
s->data=a;
}
}
}
void print(LIST head){//输出链表
LIST p;
p=head;
if(head==NULL)
cout<<"list NULL,quit\n";
else{
cout<<"\nhere is the list\n";
for(;p!=NULL;p=p->next)
cout<data<<" ";
cout< }
}
int main(){
LIST head;
create(head);
print(head);
insert(head);
SORT(head);
print(head);
Delete(head);
print(head);

return 0;
}

2.字符
#include
using namespace std;

typedef struct node
{
char data;
struct node *next;
}link;

link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((jnext!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}

link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"< else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}

link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;

while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您查找的数据在第"< j=1;p=p->next;
}

}
if(j!=1)
cout<<"您查找的数据不在线性表中."< return l;
}

link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"< else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}

link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以*结束!"< char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='*')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();

}

L=HEAD;
cout<<"当前输入的线性表为:"< P=L;P=P->next;
if(L!=NULL)
do
{cout<data<<" ";
P=P->next;
}while(P!=NULL);
cout< p=l;
while(p->next!=NULL)
p=p->next;
s=L;
p->next=s->next;
p=l;
return l;
}

link * print(link *l)
{ int i,k;
char ch;
link *p,*q;
cout<<"当前线性表为:"< p=l;p=p->next;
if(l!=NULL)
do
{cout<data<<" ";
p=p->next;
}while(p!=NULL);
cout< cout<<"请选择您要的操作:";
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout< cin>>k;
if(k==1)
{
cout<<"请输入您要插入的数据值:";
cin>>ch;
cout<<"请输入您要插入的位置:";
cin>>i;
p=ins(l,ch,i);
q=print(l);
}
else if(k==2)
{
cout<<"请输入您要查找的数据值:";
cin>>ch;
p=find(l,ch);
q=print(l);
}
else if(k==3)
{
cout<<"请输入您要删除的数据的位置:";
cin>>i;
p=del(l,i);
q=print(l);
}
else if(k==4)
{ p=add(l);
q=print(l);
}
else if(k==0)
;
else
{cout<<"输入错误!"< q=print(l);}
return l;
}

int main()
{
cout<<"请输入一串单字符数据,以*结束!"< char ch;
//link *head;
link *r,*p,*q,*l;
l=(link *)malloc(sizeof(link));
l->next=NULL;
r=l;
ch=getchar();
// getchar();
while(ch!='*')
{
p=(link *)malloc(sizeof(link));
p->data=ch;p->next=NULL;
r->next=p;r=r->next;
ch=getchar();
// getchar();
}
//l=head;
q=print(l);
return 0;

}