一道关于链表的编程题

old_stone_man_sea 2005-06-09 09:00:29
已知head指向一个带头结点的单向链表,链表中每个结点包含字符数组数据和指向本结构结点的指针。编写函数实现在值为"jone"的结点前插入值为"marit"的结点,若没有值为"jone"的结点,则插在链表最后。
...全文
483 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
keftoxic 2005-06-15
  • 打赏
  • 举报
回复
作业!...............
old_stone_man_sea 2005-06-15
  • 打赏
  • 举报
回复
作业怎么了,你没做过作业吗?都是抄的吧?
eutwin 2005-06-13
  • 打赏
  • 举报
回复
这是最基本的链表结构,随便找一本数据结构的书上都回有介绍的
brianlu 2005-06-13
  • 打赏
  • 举报
回复
#include <iostream.h>

#include <iostream>
using namespace std; --->使用namespace

都可以运行就是了

old_stone_man_sea 2005-06-12
  • 打赏
  • 举报
回复
这个帖子我只发过一次
真不知道为什么某些人会看过三次呢?
old_stone_man_sea 2005-06-12
  • 打赏
  • 举报
回复
开头少了一个.h
而且第二句我看不懂是什么意思
不过还是要谢谢你的热心哦

old_stone_man_sea 2005-06-10
  • 打赏
  • 举报
回复
牛是牛了点,不过答非所问,所以,我是不会给分的啦
foochow 2005-06-10
  • 打赏
  • 举报
回复
mark!!考试了
brianlu 2005-06-10
  • 打赏
  • 举报
回复
随便写了一个,链表自己生成。
大概凑合可以给你交作业

#include <iostream>
using namespace std;

class Node
{
public:
char m_String[10];
Node * next;
public:
Node(char * szSomeString){
if (szSomeString == NULL){
return;
}
strcpy(m_String, szSomeString);
//m_String[9] = '\0';
}
~Node(){}
};

Node * Create()
{
char *szHeadString = "HEAD";

Node *head = new Node(szHeadString);
Node * pN = head;
char * szString = "common123";
char * szSpec = "jone";
for(int i=0; i<5; i++){
if(i == 7){
pN->next = new Node(szSpec);
}else{
pN->next = new Node(szString);
}
pN = pN->next;
}
pN->next = NULL;
return head;
}


void Print(Node * pN)
{
Node *p = pN;
if(p == NULL){
return;
}
while(p!=NULL){
cout<<p->m_String<<'\t';
p=p->next;
}
cout<<endl;
}


void Insert(Node * pN)
{
Node * p = pN;
if(p == NULL){
return;
}
char * szSpec = "jone";
char * szConst = "marit";
while(p->next!=NULL){
char *m = (p->next)->m_String;
if(!strcmp(m, szSpec)){
Node * temp = p->next;
p->next = new Node(szConst);
p->next->next = temp;
break;
}
p = p->next;
}
if(p->next==NULL){
p->next = new Node(szConst);
p->next->next = NULL;
}
}


int main(int argc, char * argv[])
{
Node * head = Create();
Print(head);
Insert(head);
Print(head);
return 0;
}

zdy_8212 2005-06-10
  • 打赏
  • 举报
回复
我看过这个贴子三次了。呵。。
hackdick 2005-06-09
  • 打赏
  • 举报
回复
上面的代码,很不错,有兴趣,虽然好象不是很难,但是有空,我一定详细玩玩,不错,适合我这样的菜鸟!
hackdick 2005-06-09
  • 打赏
  • 举报
回复
我是菜鸟,不过,我看了看题目,感觉应该好象不难的,如果我没看错的话,你去好好的看看书,这道题可以搞定的,要是我看错了,那sorry!
zhousqy 2005-06-09
  • 打赏
  • 举报
回复
/*谁有兴趣一起来丰富这个程序的的功能??*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
#define ELEMTP int

#define v (*p)

struct node
{
ELEMTP data;
struct node *next;
};

struct node *p,*q,*s,*head;
int j=0,i,k;
main()
{
int x,y,cord;
void outlin(struct node *h);
void create();
void insert(struct node *h,int x,int y);
void deletes(struct node *h,int x);
struct node *MaxCompare(struct node *h);
struct node *MinCompare(struct node *h);
int delIterance(struct node *h);
void batchInsert(struct node *h,int x);
void batchDelete(struct node *h,int x,int y);
void Cz(struct node* h);
void Xg(struct node * h);
printf("建立链表,输入-999完成链表: \n");
create();
i=j;
outlin(head);
do{
printf("\n 主菜单 \n");
printf(" 1 插入一个元素 \n");
printf(" 2 删除一个元素 \n");
printf(" 3 升序排序 \n");
printf(" 4 降序排序 \n");
printf(" 5 查找元素 \n");
printf(" 6 修改元素 \n");
printf(" 7 删除重复元素 \n");
printf(" 8 批量加入元素 \n");
printf(" 9 批量删除元素 \n");
printf(" 0 结束程序运行 \n");
printf("-----------------------------------------\n");
printf(" 请输入您的选择(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) ");
scanf("%d",&cord);
switch(cord)
{
case 1:
{
printf("请输入插入的位置 i: ");
scanf("%d",&x);
printf("请输入插入的数据 y: ");
scanf("%d",&y);
insert(head,x,y);
i=j;
outlin(head);
}break;
case 2:
{
printf("x=?");
scanf("%d",&x);
deletes(head,x);
i=j;
outlin(head);
}break;
case 3:
{
printf("链表由大到小是");
s=MaxCompare(head);
j=i;
outlin(s);
//outlin(head);
}break;
case 4:
{
printf("链表由大到小是");
s=MinCompare(head);
j=i;
outlin(s);
}break;
case 5:
{
Cz(head);
outlin(head);
}break;
case 6:
{
Xg(head);
outlin(head);
}break;
case 7:
{
k=delIterance(head);
i=i-k;
j=i;
outlin(head);
}break;
case 8:
{
printf("请输入插入的位置 i: ");
scanf("%d",&x);
batchInsert(head,x);
i=j;
outlin(head);
}break;
case 9:
{
printf("请输入删除的起始位置 i: ");
scanf("%d",&x);
printf("请输入删除的结束位置 y: ");
scanf("%d",&y);
batchDelete(head,x,y);
i=j;
outlin(head);
}break;
case 0:
{
exit(0);
}break;
}
}while(cord<=9&&cord>=0);
}

void outlin(struct node *h)
{
p=h->next;
while(p!=NULL)
{
printf(" data=%4d ",p->data);
p=p->next;
}
printf("\n输出结束\n\n");
}

void deletes(struct node *h,int x)//删除节点
{
p=h;
while(p->next!=NULL&&p->next->data!=x) p=p->next;
if(p->next==NULL)
printf("x 不存在 !");
else
{
q=p->next;
p->next=q->next;
free(q);
--j;
}

}

void insert(struct node *h,int x,int y)
{
s=(struct node*)malloc(sizeof(struct node));
s->data=y;
q=h;p=h->next;
while(p!=NULL&&p->data!=x)
{
q=p;
p=p->next;
}
q->next=s;
s->next=p;
++j;
}

void create()//建立链表
{
int x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
p=head;
printf("x=?");
scanf("%d",&x);
while(x!=-999)
{
s=(struct node*)malloc(sizeof(struct node));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
printf("x=?");++j;
scanf("%d",&x);
}
}

/////////////以下函数由七绝玩家编写/////////////

struct node *MaxCompare(struct node *h)//由大到小排序
{
struct node *t;
int x;
t=h;s=NULL;
while(j!=0)
{
x=t->next->data;
q=t->next;
while(q!=NULL)
{
if(q->data<=x)
x=q->data;
else
x=x;
q=q->next;
}
p=t;
while(p->next!=NULL&&p->next->data!=x)
p=p->next;
q=p->next;
p->next=q->next;
t=p;t=h;
p=q;p->next=s;
s=p;
j--;
}
t->next=s;
head=t;
return (t);
}

struct node *MinCompare(struct node *h)//由小到大排序
{
struct node *t;
int x;
t=h;s=NULL;
while(j!=0)
{
x=t->next->data;
q=t->next;
while(q!=NULL)
{
if(q->data>=x)
x=q->data;
else
x=x;
q=q->next;
}
p=t;
while(p->next!=NULL&&p->next->data!=x)
p=p->next;
q=p->next;
p->next=q->next;
t=p;t=h;
p=q;p->next=s;
s=p;
j--;
}
t->next=s;
head=t;
return (t);
}

int delIterance(struct node *h)//删除重复元素
{
int x,y=0;
--j;
s=h->next;
while(j>0)
{
x=s->data;
p=s;
while(p->next!=NULL)
{
if(p->next==NULL)
{
x=x;
}
else if(p->next->data==x)
{
q=p->next;
p->next=q->next;
free(q);
--j;
++y;
}
else
{
p=p->next;
}
}
s=s->next;
--j;
}
return y;
}

void batchInsert(struct node *h,int x)//批量加入
{
int y=0;
q=h;p=h->next;
while(p!=NULL&&p->data!=x)
{
q=p;
p=p->next;
}
printf("y=?");
scanf("%d",&y);
while(y!=-999)
{
s=(struct node*)malloc(sizeof(struct node));
s->data=y;
q->next=s;
s->next=p;
q=s;
printf("y=?");
scanf("%d",&y);
++j;
}
}

void batchDelete(struct node *h,int x,int y)//批量删除
{
int k=0,w=0;
struct node *t;
p=h;q=h;
while(p->next!=NULL&&p->next->data!=x)
{
++k;
p=p->next;
}
while(q->next!=NULL&&q->next->data!=y)
{
++w;
q=q->next;
}
if(p->next==NULL||q->next==NULL)
printf("输入的位置不正确,请重新开始!");
else if(k<w)
{
s=q->next;
while(p->next!=s)
{
t=p->next;
p->next=t->next;
free(t);
--j;
}

}
else if(w=k)
{
printf("没有删除元素");
}
else
{
s=p->next;
while(q->next!=s)
{
t=q->next;
q->next=t->next;
free(t);
--j;
}
}
}

//////////////七绝玩家编写结束//////////////

//////////////以下函数由lihk编写////////////
void Cz(struct node* h) //查找//
{
struct node * num;int i;
num=head ;
printf("输入您要查找的号码:");
scanf("%d",&i);
while(i!=num->data && num->next!=NULL)
{
num=num->next;
}
if (i==num->data)
printf("号码:%d",num->data);
else
printf("该号码不在链表里.");
}

void Xg(struct node * h) //修改//
{
struct node * num; int i;
num=head;
printf("查找您要修改的号码:");
scanf("%d",&i);
while(i!=num->data&&num->next!=NULL){
num=num->next;}
if (i==num->data)
{
printf(":%d\n",num->data);
printf("输入您要修改的新信息:");
printf("号码:");
scanf("%d",&num->data);
printf("\n修改成功!");
}
else
printf("该号码不在链表里!");
}

//////////////lihk编写结束/////////////////
foochow 2005-06-09
  • 打赏
  • 举报
回复
mark!!怎么铺天盖地的作业
whzeng 2005-06-09
  • 打赏
  • 举报
回复
找一本数据结构或者在google上搜一下。
严老师的数据结构

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧