64,654
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = (node*)malloc(sizeof(node));
p = head;
cout << "请输入单链表的数据,以0作为结束标识:" << endl;
while (cycle)
{
cin >> x;
if (x != 0)//以0为标识
{
s = (node *)malloc(sizeof(node));
s->data = x;
p->next = s;
p = s;
}
else cycle = 0;
}
//head = head->next;
p->next = NULL;
cout << endl;
return (head);
}
//求单链表的长度
int length(node *head)
{
int n = 0;
node *p;
p = head->next; //p = head;
while (p != NULL)
{
p = p->next;
n++;
}
return(n);
}
//输出单链表
void print(node *head)
{
node *p; int n;
n = length(head);
cout << "现在,长度为" << n << "的单链表更新为:" << endl;
p = head->next; //p = head;
if (head != NULL)
{
while (p != NULL)
{
cout << p->data << '\t';
p = p->next;
}
}
printf("\n");
}
void RemoveHead(node *head)
{
node *p;
//p = head;
//head = p->next;
p=head->next;
head->next=p->next;
free(p);
}
int main()
{
node *head;
int n;
head = creat();
print(head);
cout << "\n单链表的长度: ";
n = length(head);
cout << n << endl;
cout << "\n移除头元素: ";
RemoveHead(head);
print(head);
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
node *head;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = (node*)malloc(sizeof(node));
p = head;
cout << "请输入单链表的数据,以0作为结束标识:" << endl;
while (cycle)
{
cin >> x;
if (x != 0)//以0为标识
{
s = (node *)malloc(sizeof(node));
s->data = x;
p->next = s;
p = s;
}
else cycle = 0;
}
head = head->next;
p->next = NULL;
cout << endl;
return (head);
}
//求单链表的长度
int length()
{
int n = 0;
node *p;
p = head;
while (p != NULL)
{
p = p->next;
n++;
}
return(n);
}
//输出单链表
void print()
{
node *p; int n;
n = length();
cout << "现在,长度为" << n << "的单链表更新为:" << endl;
p = head;
if (head != NULL)
{
while (p != NULL)
{
cout << p->data << '\t';
p = p->next;
}
}
printf("\n");
}
void RemoveHead()
{
node *p;
p = head;
head = head->next;
//p=head->next;
//head->next=p->next;
free(p);
}
int main()
{
int n;
head = creat();
print();
cout << "\n单链表的长度: ";
n = length();
cout << n << endl;
cout << "\n移除头元素: ";
RemoveHead();
print();
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head,*p,*s;
int x,cycle=1;
head=(node*)malloc(sizeof(node));
p=head;
cout<<"请输入单链表的数据,以0作为结束标识:"<<endl;
while(cycle)
{
cin>>x;
if(x!=0)//以0为标识
{
s=(node *)malloc(sizeof(node));
s->data=x;
p->next=s;
p=s;
}
else cycle=0;
}
head=head->next;
p->next=NULL;
cout<<endl;
return (head);
}
//求单链表的长度
int length(node *head)
{
int n=0;
node *p;
p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
return(n);
}
//输出单链表
void print(node *head)
{
node *p;int n;
n=length(head);
cout<<"现在,长度为"<<n<<"的单链表更新为:"<<endl;
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
cout<<p->data<<'\t';
p=p->next;
}
}
printf("\n");
}
void RemoveHead(node *head)
{
node *p;
p=head;
head=p->next;
//p=head->next;
//head->next=p->next;
free(p);
}
int main()
{
node *head;
int n;
head=creat();
print(head);
cout<<"\n单链表的长度: ";
n=length(head);
cout<<n<<endl;
cout<<"\n移除头元素: ";
RemoveHead(head);
print(head);
return 0;
}
void RemoveHead(node **head)
{
node *p=(*head);
(*head)=p->next;
free(p);
}
void RemoveHead(node *head)
{
node *p;
p=head;
head=p->next;
free(p);
}