#include <stdio.h>
#include <iostream>
#include "define.h"
using namespace std;
int main(int argc, char *argv[])
{
//创建单循环链表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);
#include <stdio.h>
#include <iostream>
#include "define.h"
using namespace std;
int main(int argc, char *argv[])
{
//创建单循环链表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);
//创建链表,并返回链表数目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);
//计算链表的长度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;
//查找输入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin); //清空标准输入缓冲区,方便下次使用 如果有多个输入语句时 加入fflush用来清空缓冲区
locate(head, x);
//在链表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);
//链表逆序输出
printf("\nOutput the inversing list!\n");
inverselist(head);
deletelist(head);
return true;
}
struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
tail->next = head;
Node head_insert = head;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
if(head_insert==head)
{
break;
}
}
return head;
}
struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
return head;
}
int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
//return(n);
}
void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else
{
flag = 1; //设置标志位进行判断
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}
printlist(head->next);
}
}
int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
{
head=head->next;
n++;
}
if(head==NULL)
return(-1);
else
cout<<"the location of the element is "<<n+1<<endl;
return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;
head = p;
while(head)
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把头指针传过来
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1) //要考虑到插入到头结点和插入到尾结点的不同
{
p->next = head;
head = p;
head_insert = head;
}
else if(i==(len+1)) //多用小括号,防止出错
{
head_insert = head;
int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL; //不能保证tail指针也能传进来 插入与建立链表是两码事
}
else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}
else //第三种情况 插入到中间位置
{
head_insert = head;
while(j<i && head->next!=NULL)
{
head=head->next;
j++;
if(j==i)
{
p->next = head->next;
head->next = p;
}
//else
// cout<<"The parameter of location is incorrect!"<<endl;
}
}
//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}
return 1;
}
void deletelist(node *head)
{
Node p, q;
p = head->next;
while(p!=head) //
{
q = p->next;
free(p); //
p = q;
}
free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}
//创建链表,并返回链表数目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);
//计算链表的长度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;
//查找输入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin); //清空标准输入缓冲区,方便下次使用 如果有多个输入语句时 加入fflush用来清空缓冲区
locate(head, x);
//在链表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);
//链表逆序输出
printf("\nOutput the inversing list!\n");
inverselist(head);
deletelist(head);
return true;
}
struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
tail->next = head;
Node head_insert = head;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
if(head_insert==head)
{
break;
}
}
return head;
}
struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
return head;
}
int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
//return(n);
}
void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else
{
flag = 1; //设置标志位进行判断
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}
printlist(head->next);
}
}
int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
{
head=head->next;
n++;
}
if(head==NULL)
return(-1);
else
cout<<"the location of the element is "<<n+1<<endl;
return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;
head = p;
while(head)
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把头指针传过来
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1) //要考虑到插入到头结点和插入到尾结点的不同
{
p->next = head;
head = p;
head_insert = head;
}
else if(i==(len+1)) //多用小括号,防止出错
{
head_insert = head;
int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL; //不能保证tail指针也能传进来 插入与建立链表是两码事
}
else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}
else //第三种情况 插入到中间位置
{
head_insert = head;
while(j<i && head->next!=NULL)
{
head=head->next;
j++;
if(j==i)
{
p->next = head->next;
head->next = p;
}
//else
// cout<<"The parameter of location is incorrect!"<<endl;
}
}
//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}
return 1;
}
void deletelist(node *head)
{
Node p, q;
p = head->next;
while(p!=head) //
{
q = p->next;
free(p); //
p = q;
}
free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}
#include <iostream>
#include "define.h"
using namespace std;
int main(int argc, char *argv[])
{
//创建单循环链表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);
#include <stdio.h>
#include <iostream>
#include "define.h"
using namespace std;
int main(int argc, char *argv[])
{
//创建单循环链表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);
//创建链表,并返回链表数目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);
//计算链表的长度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;
//查找输入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin); //清空标准输入缓冲区,方便下次使用 如果有多个输入语句时 加入fflush用来清空缓冲区
locate(head, x);
//在链表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);
//链表逆序输出
printf("\nOutput the inversing list!\n");
inverselist(head);
deletelist(head);
return true;
}
struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
tail->next = head;
Node head_insert = head;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
if(head_insert==head)
{
break;
}
}
return head;
}
struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
return head;
}
int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
//return(n);
}
void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else
{
flag = 1; //设置标志位进行判断
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}
printlist(head->next);
}
}
int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
{
head=head->next;
n++;
}
if(head==NULL)
return(-1);
else
cout<<"the location of the element is "<<n+1<<endl;
return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;
head = p;
while(head)
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把头指针传过来
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1) //要考虑到插入到头结点和插入到尾结点的不同
{
p->next = head;
head = p;
head_insert = head;
}
else if(i==(len+1)) //多用小括号,防止出错
{
head_insert = head;
int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL; //不能保证tail指针也能传进来 插入与建立链表是两码事
}
else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}
else //第三种情况 插入到中间位置
{
head_insert = head;
while(j<i && head->next!=NULL)
{
head=head->next;
j++;
if(j==i)
{
p->next = head->next;
head->next = p;
}
//else
// cout<<"The parameter of location is incorrect!"<<endl;
}
}
//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}
return 1;
}
void deletelist(node *head)
{
Node p, q;
p = head->next;
while(p!=head) //
{
q = p->next;
free(p); //
p = q;
}
free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}
//创建链表,并返回链表数目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);
//计算链表的长度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;
//查找输入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin); //清空标准输入缓冲区,方便下次使用 如果有多个输入语句时 加入fflush用来清空缓冲区
locate(head, x);
//在链表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);
//链表逆序输出
printf("\nOutput the inversing list!\n");
inverselist(head);
deletelist(head);
return true;
}
struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
tail->next = head;
Node head_insert = head;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
if(head_insert==head)
{
break;
}
}
return head;
}
struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed!\n"); //申请内存失败
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申请,并释放,成对出现
}
return head;
}
int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
//return(n);
}
void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else
{
flag = 1; //设置标志位进行判断
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}
printlist(head->next);
}
}
int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
{
head=head->next;
n++;
}
if(head==NULL)
return(-1);
else
cout<<"the location of the element is "<<n+1<<endl;
return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;
head = p;
while(head)
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把头指针传过来
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1) //要考虑到插入到头结点和插入到尾结点的不同
{
p->next = head;
head = p;
head_insert = head;
}
else if(i==(len+1)) //多用小括号,防止出错
{
head_insert = head;
int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL; //不能保证tail指针也能传进来 插入与建立链表是两码事
}
else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}
else //第三种情况 插入到中间位置
{
head_insert = head;
while(j<i && head->next!=NULL)
{
head=head->next;
j++;
if(j==i)
{
p->next = head->next;
head->next = p;
}
//else
// cout<<"The parameter of location is incorrect!"<<endl;
}
}
//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}
return 1;
}
void deletelist(node *head)
{
Node p, q;
p = head->next;
while(p!=head) //
{
q = p->next;
free(p); //
p = q;
}
free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}
问题1: 申请多少内存,再释放多少内存,虽然话是这么说,但是一定要亲力亲为
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
//Node p = (Node )malloc(sizeof(Node));
导致节点无法释放,申请的一个结构体,与申请一个结构体指针是不同的
问题2:
sanf函数的使用,要注意
两次使用需要清空缓存区,
下图所示的错误并不是 不能无从下手的!