導航:首頁 > 數據分析 > 如何在初始化鏈表後增加數據

如何在初始化鏈表後增加數據

發布時間:2023-01-27 15:43:07

Ⅰ 如何創建一個單項鏈表並存入數據

//一個簡單但完整的單向鏈表
#include <iostream>
#include <string.h>
using namespace std;

struct node //定義結構體
{
char* ch; //存放數據字元串
struct node* next; //指向下一個結點
};

struct node* Create() //新建結點並初始化
{
struct node* n=new struct node;
n->ch=NULL;
n->next=NULL;
return n;
}

int main()
{
struct node *head=NULL,*p=NULL;
char s[100]="\0";
while(1)
{
memset(s,'\0',100);
cin>>s;
if(strcmp(s,"quit")==0) break; //如果輸入的是quit則表示用戶結束輸入
struct node *new_node;
new_node=Create(); //新建結點
new_node->ch=new char[strlen(s)]; //為新建立的結點分配數據字元串存儲空間
strcpy(new_node->ch,s); //把用戶輸入的字元串存儲入新結點中
if(head==NULL) //如果頭結點為空,則把當前新結點當成頭結點
{
head=new_node;
p=head; //當前指向為頭結點
}
else //如果頭結點不為空
{
p->next=new_node; //把上一個結點的next指向新建結點
p=p->next; //當前指向為新結點
}
}
p=head; //重新指向頭結點,以便輸出
while(1)
{
cout<<p->ch<<endl; //輸出數據字元串
if(p->next==NULL) break; //如果當前結點沒有指向下一個結點,則退出
p=p->next; //當前指向下一個結點
}
return 0;
}

Ⅱ C程序中鏈表已經建立,但怎麼輸入數據呢

上機編譯過了,給你提供一個參考.包括鏈表所有的基本知識(創建,插入,刪除,輸出).呵呵.

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p2->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}

struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
}
else
printf("%ld not been found!\n",num);
return(head);
}

struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
n=n+1;
return(head);
}
}

void main()
{
struct student *head,*stu;
long del_num;
printf("Please input the records:\n");
head=creat();
print(head);
printf("\nPlease input the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("Please input the deleted number:");
scanf("%ld",&del_num);
}
printf("\nPlease input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("&ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("Please input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}

Ⅲ 用C語言編寫程序建立鏈表結構體類型實現鏈表初始化遍歷和插入演算法

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

#define telemtype char
#define ok 1
#define error 0
#define overflow -1

typedef int status;

typedef struct bitnode
{
telemtype data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;

void preordertraverse(bitree T)
{
if(T)
{
printf("%c ",T->data);
preordertraverse(T->lchild);
preordertraverse(T->rchild);
}
}

status createbitree(bitree &T)
{
int ch;
ch=getchar();
if(ch==' ')
T=NULL;
else
{
if(!(T=(bitnode*)malloc(sizeof(bitnode))))
exit(overflow);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
return ok;
}

void prinbtree(bitree T)
{
if(T!= NULL)
{

printf("%c", T->data);
if(T->lchild!=NULL||T->rchild!=NULL)
{
printf("(");
prinbtree(T->lchild);
if(T->rchild!=NULL)
{
printf(",");
}
prinbtree(T->rchild);
printf(")");
}
}
}

int main()
{
bitree T=NULL;

printf("先序輸入二叉樹:\n");
createbitree(T);

printf("先序遍歷二叉樹為:\n");
preordertraverse(T);
printf("\n");

prinbtree(T);
printf("\n");

return 0;
}

我寫的,希望對你有用!

Ⅳ 如何在鏈表的運行程序里輸入數據

內容包括鏈表的創建,增加、刪除節點,鏈表的逆序、排序和銷毀等。 #include #include typedef struct node { int data; node* pNext; }Node; //鏈表的操作,以有頭節點為例,無頭節點類似 Node* head = NULL; //創建鏈表

Ⅳ C++中鏈表初始化

p1=p2=(Cwow *)malloc(sizeof(Cwow));

問題出在這一句,這里只是負責分配一個空間大小sizeof(Cwow)的堆內存給p1.這塊內存的數據是沒有經過初始化的。

所以當你調用p1->name時就會報錯,因為string name的內容是隨機的。

解決辦法有兩個:

  1. 使用new操作符, p1 = p2 = new Cwow;

  2. 在p1=p2=(Cwow *)malloc(sizeof(Cwow));後面增加一句,memset(p1, 0 ,sizeof(Cwow));

建議使用第一種方法,使用new操作符,會自動調用string的默認構造函數。

Ⅵ 誰能幫我講講c語言中的鏈表問題

這個問題本身不是很復雜,但是需要比較大的篇幅。我簡單介紹一下,但願對你有幫助。
typedef
struct
_HList
{
void
*data
struct
_HList
*next;
}
HList,
*PHList;
首先鏈表:
HList
*my_list;
然後初始化鏈表:
my_list
=
(HList
*)calloc(1,
sizeof(HList));
初始化成功以後,就可以往鏈表加數據了,加數據的過程就是在構建一個鏈表
int
list_add(HList
*me,
void
*data)
//將數據加到鏈表頭部
{
if
(me==NULL)
return
-1;
HList
*new_node
=
NULL;
//構建一個新節點
new_node
=
(HList
*)calloc(1,
sizeof(HList));
new_node->data
=
data;//給新節點賦值;
//下面是將節點加到鏈表的頭部
new_node->next
=
me->next;
//保存頭部的先前第一個節點
me->next
=
new_node;
//將第一個節點保存為新創建的節點
return
0;
}
//解決了創建和插入的問題,下面舉例刪除
/*有很多中,比如從頭部刪除,或者從尾部刪除,或者根據指定的鍵值刪除,下面的例子是根據指定的鍵值刪除*/
int
HList_del(HList
*me,
void
*data)
{
if
(me==NULL)
return
-1;
//當然得先判斷鏈表是否有效
HList
*cur_node
=
me->next;
HList
*pre_node
=
me;
/*保存前一個節點,為了刪除時知道前一個節點的位置*/
while
(cur_node!=NULL)
{
if
(memcmp(cur_node->data,
data,
strlen(data)))
//這里的比較只是示例,如果有鍵值,直接比較鍵值最好
{
//如果相等,則在鏈表中去掉該節點
pre_node->next
=
cur_node->next;
//當然,要釋放該節點所佔用的內存,否則會出現內存泄露
free(cur_node->data);free(cur_node)
}
//調整節點的位置
pre_node
=
cur_node;
cur_node
=
pre_node->next;
}
return
0;
}
大概就是這個樣子了,希望對你有幫助

Ⅶ c語言鏈表的初始化,建立,插入,查找,刪除!!急急急!謝謝各位大神了!

好吧,我居然找到了我以前的了。。。
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define N 10
typedef struct node
{
char name[20];
struct node *link;
}stud;
stud * create(int n) //創建有n個節點的鏈表,返回鏈表頭結點
{
stud *p,*h,*s;
if((h=(stud *)malloc(sizeof(stud)))==NULL)//創建頭節點
{
printf("Memory allocation fails!");
exit(0);
}
strcpy( h->name, 「head」 );
h->link=NULL;
p=h;
for(int i=0;i<n-1;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)//依次創建每一個節點
{
printf("Memory allocation fails!");
exit(0);
}
p->link=s;
printf("Please input %d person name:", i+1);
scanf("%s",s->name);
s->link=NULL;
p=s;
}
return(h);
}

stud* search(stud *h, char *x) //查找,h是鏈表頭結點,x是要查的字元串
{
stud *p = h;
while(p!=NULL) //最後一個節點的link
{
if(strcmp(p->name, x)==0)
return(p);
else
p=p->link; //p指向下一個節點
}
if(p==NULL)
printf("There is no such a student!");
return p;
}

stud* Delete(stud *h, char *x) //刪除某個節點,返回鏈表頭結點 //x是要刪除的數據
{
if( h == NULL)
return NULL;

stud * p1, *p2;
p1=h;
p2=h->link;
if (strcmp( p1->name, x ) == 0 )//頭結點特殊處理
{
h = h->link;//頭指針指向第二個元素即可
free(p1);//釋放頭結點
return h;
}

while(p2!=NULL)//p2 代表當前要比較的節點,p1代表當前節點的前一個節點
{
if(strcmp(p2->name, x)==0)
{
p1->link = p2->link;
free(p2);
return h;
}
else
{
p1=p2;
p2=p2->link;//一起往後移
}
}
rerun h;
}

void insert(stud *p) //在p節點後插入一個節點
{
char stuname[20];
stud *s;

if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("Memory allocation fails!");
exit(0);
}
printf("Please input name」 );
scanf("%s",stuname);
strcpy(s->name,stuname);
s->link=p->link;//後邊連接
p->link=s;//前邊連接
}

void freeList(stud* head) //刪除整個鏈表
{    
stud* p;
while( head != NULL)
{
      p = head;
        head = head->link;
        free(p);
    }
}

void printList(stud* head) //列印鏈表中的所有數據
{    
stud* p;
p = head;
int count = 0;
while(p != NULL)
{
        printf("%s\n", p->name);
         p = p->link;
count++;
}
}

int main()
{
int number;
char fullname[20];
stud *head,*searchpoint;

number=N;
head=creat(number);
printf("Input the srearch name:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
insert(searchpoint);
char str[10];
scanf( 「%s」, str );
head = delete( head, str );
printList( head );
freeList( head );
return 0;
}
以下是將鏈表逆置的demo
typedef struct linknode
{
int data;
struct linknode *next;
}node;

//將一個鏈表逆置
typedef struct linknode
{
int data;
struct linknode *next;
}node;//類型定義
node *reverse(node *head)
{
node *p,*q,*r; //q代表當前節點,p代表當前節點的前一個節點,r代表當前節點的下一個節點
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}

閱讀全文

與如何在初始化鏈表後增加數據相關的資料

熱點內容
程序員看什麼書 瀏覽:481
聊城織夢網站怎麼加登錄頁面 瀏覽:547
北京宏岸圖升網路技術有限公司上海分公司 瀏覽:825
手機中無用的文件夾有哪些 瀏覽:535
品管文件名 瀏覽:940
蘋果用一個id通話記錄 瀏覽:417
文件名前綴 瀏覽:998
w10網路撥號651錯誤代碼 瀏覽:801
大數據進階書籍 瀏覽:948
家裡兩台電腦怎麼傳文件 瀏覽:305
網路機頂盒怎麼接電腦 瀏覽:951
文件管理文件夾 瀏覽:891
iphone怎麼存文件 瀏覽:201
linux查找復制文件 瀏覽:631
蘋果系統酷狗文件路徑 瀏覽:269
春天大數據 瀏覽:87
九游app蜀山傳奇怎麼登錄不了 瀏覽:925
lg編程軟體哪個好用 瀏覽:765
面板數據檢驗結果看哪些指標 瀏覽:83
許昌淘客app有哪些 瀏覽:860

友情鏈接