单链表操作

qiaozheng2011 2011-11-23 04:22:04
建一个单链表,实现:
1.依次输入3个数
2.依次输出三个数
例如:
输入1,2,3
输出1,2,3
谢谢大家了,能用简单的比较笨的方法写嘛?我初学数据结构,望高手用比较低级的方法解答。我实在写不出来,看了快两星期了。再次谢谢了。用c语言
...全文
150 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿尼小码 2011-11-23
  • 打赏
  • 举报
回复
我存了一个自己以前写过的错误代码不好意思,
阿尼小码 2011-11-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <malloc.h>

typedef struct stundent
{
char m;
struct stundent *next;
}Student;

void main()
{
Student *head1,*head2,*head3;
register int n;

head1=(struct stundent *)malloc(sizeof(struct stundent));
head2=head1;//指针赋值指向同一内存
for(n=0;n<=2;n++)
{
head3=(Student*)malloc(sizeof(Student));
scanf("%c",&head3->m);
fflush(stdin);
head2->next=head3;
head2=head2->next;
//head2=(struct stundent *)malloc(sizeof(struct stundent)); //这里head2指向队列末元素,给它重新分配空间不是乱套了么
}
head2=head1;
for(n=0;n<=2;n++)
{
head2=head2->next; //队列的头指针没存数据,应该先后移
printf("%c",head2->m);//队列信息
}
printf("\n");
for(n=0;n<=2;n++)
{
head2=head1;
head1=head1->next;
free(head2); //虽然放着不管Windows的GC也会自动清理掉,但C/C++程序员不能这么不负责任哈
}
free(head1);
}
w28050 2011-11-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
int context;
struct node *next;
}linklist;

/*添加节点 尾插法,添加成功返回尾节点,否则返回NULL*/
linklist* createNode(linklist *head)
{
linklist *temp = NULL;

/*头结点合法性检查*/
if(NULL == head)
{
return NULL;
}

temp = head;

/* 获取尾节点 */
while(NULL != temp->next)
{
temp = temp->next;
}
temp->next = (linklist *)malloc(sizeof(linklist));
/* 节点是否申请成功 */
if(NULL != temp->next)
{
temp->next->next =NULL;
return temp->next;
}
else
{
return NULL;
}
}

int main()
{
struct node head;
linklist *linkhead = &head,*temp;

int i =0;

linkhead->next = NULL;

/* 输入 */
while(i < 3)
{
temp = createNode(linkhead);
if(NULL !=temp)
{
scanf("%d",&temp->context);
}

i++;
}

temp = linkhead->next;
/* 输出 */
while(NULL !=temp)
{
printf("%d",temp->context);
temp = temp->next;
}

//释放操作省
}
qiaozheng2011 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dreamwdy 的回复:]
#include <stdio.h>
#include <malloc.h>
struct stundent
{
char m;
struct stundent *next;
};
void main()
{
struct stundent *head1,*head2,*head3;
head1=(struct stundent *)malloc(sizeof(stru……
[/Quote]
还是不对啊兄弟
qiaozheng2011 2011-11-23
  • 打赏
  • 举报
回复
求解释啊
qiaozheng2011 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dreamwdy 的回复:]
#include <stdio.h>
#include <malloc.h>
struct stundent
{
char m;
struct stundent *next;
};
void main()
{
struct stundent *head1,*head2,*head3;
head1=(struct stundent *)malloc(sizeof(stru……
[/Quote]
更不对了,疯了
阿尼小码 2011-11-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <malloc.h>
struct stundent
{
char m;
struct stundent *next;
};
void main()
{
struct stundent *head1,*head2,*head3;
head1=(struct stundent *)malloc(sizeof(struct stundent));
head2=head1;
for(int n=0;n<=2;n++)
{
head3=(struct stundent *)malloc(sizeof(struct stundent));
scanf("%c",head3->m);//输入要插入结点信息
head2=head3;
head2=head2->next;
}
for(n=0;n<=2;n++)
{
printf("%c",head1->m);//队列信息
head1=head1->next;
}
我改了一下,刚才我复制错了你运行看看
  • 打赏
  • 举报
回复
赶紧结贴
  • 打赏
  • 举报
回复

#include <stdio.h>
#include<stdlib.h>
struct listtest{
int number;
struct listtest *next;
};

int main(){

int i = 0;
struct listtest *listhead = NULL, *listtail = NULL, *newlist = NULL;
while( 3 > i ){
newlist = (struct listtest*)malloc(sizeof(struct listtest));
scanf("%d",&newlist->number);
newlist->next = NULL;
if(NULL == listhead){
listhead = newlist;
listtail = newlist;
}else{
listtail->next = newlist;
listtail = newlist;
}
i++;
}

while( NULL != listhead){
newlist = listhead;
printf("%d\n",newlist->number);
listhead = newlist->next;
free(newlist);
}

return 0;
}
qiaozheng2011 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhw_yihui 的回复:]
C/C++ code

#include <iostream>
using namespace std;

struct listtest{
int number;
struct listtest *next;
};

int main(){

int i = 0;
struct listtest *list……
[/Quote]
能改为c语言不?兄弟
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

struct listtest{
int number;
struct listtest *next;
};

int main(){

int i = 0;
struct listtest *listhead = NULL, *listtail = NULL, *newlist = NULL;
while( 3 > i ){
newlist = new listtest;
cin >> newlist->number;
newlist->next = NULL;
if(NULL == listhead){
listhead = newlist;
listtail = newlist;
}else{
listtail->next = newlist;
listtail = newlist;
}
i++;
}

while( NULL != listhead){
newlist = listhead;
cout << newlist->number << endl;
listhead = newlist->next;
delete newlist;
}

return 0;
}
qiaozheng2011 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dreamwdy 的回复:]
上面少些括号自己加,char m改为int m
[/Quote]
不对啊,你能再帮我改改吗?
我改过的程序如下:
#include <stdio.h>
#include <malloc.h>
struct stundent
{
int m;
struct stundent *next;
};
void main()
{
int n;
struct stundent *head1,*head2,*head3;
head1=(struct stundent *)malloc(sizeof(struct stundent));
head2=(struct stundent *)malloc(sizeof(struct stundent));
head1->next=head2;
for(n=0;n<3;n++)
{
head3=(struct stundent *)malloc(sizeof(struct stundent));
scanf("%c",head3->m);
head2=head3;
head2=head2->next;
head2=(struct stundent *)malloc(sizeof(struct stundent));
}
for(n=0;n<3;n++)
{
printf("%d\n",head1->m);
head1=head1->next;
}

}
测试NULL 2011-11-23
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

struct LinkList
{
int data;
struct LinkList *next;
};

struct LinkList* createNode()
{
struct LinkList *node = (struct LinkList *)malloc(sizeof(struct LinkList));

return node;
}

int main(void)
{
struct LinkList *head, *node1, *node2, *node3;
struct LinkList *p = NULL;
int num[3];

head = createNode();
node1 = createNode();
node2 = createNode();
node3 = createNode();

head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = NULL;

for(int i=0; i<3; i++)
{
scanf("%d", &num[i]);
}

node1->data = num[0];
node2->data = num[1];
node3->data = num[2];

p = head->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");

free(node3);
free(node2);
free(node1);
free(head);

return 0;
}
阿尼小码 2011-11-23
  • 打赏
  • 举报
回复
上面少些括号自己加,char m改为int m
阿尼小码 2011-11-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <malloc.h>
struct stundent
{
char m;
struct stundent *next;
};
void main()
{
struct stundent *head1,*head2,*head3;
head1=(struct stundent *)malloc(sizeof(struct stundent));
head2=(struct stundent *)malloc(sizeof(struct stundent));
head1->next=head2;
for(int n=0;n<=2;n++)
{
head3=(struct stundent *)malloc(sizeof(struct stundent));
scanf("%c",head3->m);//输入要插入结点信息
head2=head3;
head2=head2->next;
head2=(struct stundent *)malloc(sizeof(struct stundent)
for(n=0;n<=2;n++)
{
printf("%c",head1->m);//队列信息
head1=head1->next;
}
}
一个简单的队列,LZ慢慢看

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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